Introduction: How to Interface Ultrasonic Sensor (HCSR04) to Arduino Uno
Ultrasonic sensors are much more efficient than other obstacle detection sensors. Ultrasonic Sensors relatively inexpensive and also the ultrasound emitters and detectors are portable without the need for complex circuitry.Real time assistance and artificial vision along with dedicated obstacle detection system is provided.
Here TechPonder helps you in interfacing Ultrasonic sensor to Arduino.
Hardware requirements;
1) Arduino UNO
2)Ultrasonic Sensor
3)Buzzer
4)Bread Board
Step 1: Hardware Connections
Hardware connections:-
Arduino Ultrasonic sensor
5v - > Vcc
gnd -> Gnd
8th Pin -> Trig pin
7th pin -> Echo Pin
Arduino Buzzer
9th pin -> +ve pin
Gnd -> -ve Pin
Step 2: Program and Results
// UltraSonic Sensor interfacing to Arduino .
int buzzer = 9;
int triggerPin = 7; //triggering on pin 7
int echoPin = 8; //echo on pin 8
void setup()
{
Serial.begin(9600); //we'll start serial comunication, so we can see the distance on the serial monitor Serial.println("Tech Ponder's UltraSonic Sensor Tutorial");
pinMode(triggerPin, OUTPUT); //defining pins
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer,LOW); }
void loop()
{ int duration, distance; //Adding duration and distance
digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
delay(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
delay(1000);
Serial.print(distance); //printing the numbers
Serial.print("cm"); //and the unit
Serial.println(" "); //just printing to a new line
if (distance < 35)
{
digitalWrite(buzzer,HIGH);
Serial.println("Buzzer On");
}
digitalWrite(buzzer,LOW);
}
Results are shown on Serial Monitor.
For every second Arduino calculates the distance using Ultrasonic sensor. When the distance is less than 35cm arduino detects the threshold value and the buzzer is on. it can be used as an obstacle detector. When the obstacle approaches distance less the 35cm it alerts the user.
Thanks,
TechPonder.