Ultrasonic Sensor HC-SR04 With Arduino
by theorycircuit in Circuits > Arduino
5532 Views, 30 Favorites, 0 Comments
Ultrasonic Sensor HC-SR04 With Arduino

See more at:
https://theorycircuit.com/arduino-projects/interface-ultrasonic-sensor-hc-sr04-with-arduino/
Facebook link: https://www.facebook.com/pages/Theorycircuit/82511...
The HC-SR 04 is famous ultrasonic range sensor, and its very easy to use with many microcontrollers. This article will give you idea about to interface ultrasonic sensor with Arduino board.
Make Connections


Here the circuit constructed for to identify the range limit with warning beep sound. This circuit have minimum components such as arduino development board, ultrasonic sensor HC-SR 04 and a buzzer.
Arduino Code
/*HC-SR04 Ping distance sensor
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 9
Trig to Arduino pin 8
Buzzer +ve to Arduino pin 13 and GND to GND
Original code sourced from theorycircuit.com
Some code and wiring inspired by arduino.cc
*/
#define trigPin 8
#define echoPin 9
#define Buzzer 13
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Buzzer, OUTPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 30)
{
digitalWrite(Buzzer,HIGH); //less than 30cm then buzzer will produce beep sound
}
else {
digitalWrite(Buzzer,LOW);
}
if (distance >= 200 || distance <= 0)
{
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
/* See more at: http://www.theorycircuit.com/DoItYourself/Electro...
*/