Project tutorial
Detecting Obstacles and Warning - Arduino and Ultrasonic

Detecting Obstacles and Warning - Arduino and Ultrasonic

This is tutorial to help you understand ultrasonic and buzzer and go deeper into learning Arduino, follow these steps and give me feedback.

  • 71,760 views
  • 40 comments
  • 110 respects

Components and supplies

About this project

This is tutorial to help you understand ultrasonic and buzzer and go deeper into learning Arduino, follow these steps and give me feedback please.

I'm making it to help my cousin in his school project.

Step 1: Prepare your material and tools

1. Test Board

2. Ultrasonic sensor

3. Arduino cable

4. +5V buzzer

5. Male to male pins

6. Arduino uno board

Tools and materials are shown in the pictures.

Step 2: Connect your circuit

Connect the Buzzer positive terminal to the Arduino pin 2 and the negative terminal to the Gnd. Connect the VCC pin of ultrasonic to +5v pin and the Gnd to the ground.

Connect trig pin to pin 10 and echo pin to pin 9.

The pictures shows you the connection.

Step 3: Load the code to your Arduino

You can find code in codebender.cc here, it is well described and commented. This is copy of the code any way.

/*
This code should work to get warning cross the buzzer when something be closer than 0.5 meter
Circuit is ultrasonic sensor and buzzer +5v and Arduino uno is used
a_atef45@yahoo.com
www.zerosnones.net
+201153300223
*/
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
}
// Waiting 60 ms won't hurt any one
delay(60);
}

Step 4: Try it

Watch this video for it working, record you video and show me, give me your feedback and i love to answer your questions.

Code

Untitled fileArduino
/*
	This code should work to get warning cross the buzzer when something be closer than 0.5 meter
	Circuit is ultrasonic sensor and buzzer +5v and Arduino uno is used
	a_atef45@yahoo.com
	www.zerosnones.net
	+201153300223
*/
// Define pins for ultrasonic and buzzer
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;

void setup()
{
	pinMode(trigPin, OUTPUT); // trig pin will have pulses output
	pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
	pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}

void loop()
{
	// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
	int duration, distance;
	// Output pulse with 1ms width on trigPin
	digitalWrite(trigPin, HIGH); 
	delay(1);
	digitalWrite(trigPin, LOW);
	// Measure the pulse input in echo pin
	duration = pulseIn(echoPin, HIGH);
	// Distance is half the duration devided by 29.1 (from datasheet)
	distance = (duration/2) / 29.1;
	// if distance less than 0.5 meter and more than 0 (0 or less means over range) 
    if (distance <= 50 && distance >= 0) {
    	// Buzz
    	digitalWrite(buzzPin, HIGH);
    } else {
    	// Don't buzz
    	digitalWrite(buzzPin, LOW);
    }
    // Waiting 60 ms won't hurt any one
    delay(60);
}
Codebender

Schematics

ultrasonic%20with%20buzzer_bb.png
Ultrasonic%20with%20buzzer bb

Comments

Similar projects you might like

Ultrasonic Security System

Project tutorial by Ivan

  • 18,649 views
  • 19 comments
  • 53 respects

Ultrasonic Map-Maker using an Arduino Yun

Project tutorial by Satyavrat Wagle

  • 52,759 views
  • 37 comments
  • 155 respects

Simple Programmable Robotic Arm

Project showcase by Ryan Chan

  • 51,872 views
  • 67 comments
  • 152 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 260,262 views
  • 42 comments
  • 281 respects

Portable Arduino Temp/Humidity Sensor with LCD

Project tutorial by ThothLoki

  • 153,300 views
  • 115 comments
  • 185 respects

Arduino Sonar

Project tutorial by Tony ZHANG

  • 71,478 views
  • 33 comments
  • 137 respects
Add projectSign up / Login