Ultrasonic Sensor HC-SR04

SR04 Ultrasonic Distance Sensor is a popular and low cost solution for non-contact distance measurement function.  This module includes ultrasonic transmitter, ultrasonic receiver and its control circuit.
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2 cm to 400 cm or 1” to 13 feet. It comes complete with ultrasonic transmitter and receiver module.

SENSOR PIN ARDUINO CONNECTION
Vcc 5V DC
TRIGGER Trigger pin D9
ECHO Echo Pin (output) D12
GND Ground with Circuit

FEATURES:
1. Quiescent Current: <2mA.
2. Working Current: 15mA.
3. Effectual Angle: <15°.
4. Ranging Distance: 2cm – 400 cm/1″ – 13ft.
5. Resolution: 0.3 cm
6. Measuring Angle: 30 degree.
7. Trigger Input Pulse width: 10uS.
8. Dimensions: 45mm x 20mm x 15mm.
9. Power Supply:+5V DC.
OPERATION:
The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object. An ultrasonic sensor generates the high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver
Provide TRIGGER signal, atleast 10μS High Level (5V) pulse.
The module will automatically transmit eight 40KHz ultrasonic burst.
If there is an obstacle in-front of the module, it will reflect the ultrasonic burst.

If the signal is back, ECHO output of the sensor will be in HIGH state (5V) for a duration of time taken for sending and receiving ultrasonic burst. Pulse width ranges from about 150μS to 25mS and if no obstacle is detected, the echo pulse width will be about 38ms.
HC-SR04 WITH ARDUINO:
In this project the ultrasonic sensor reads and writes the distance in the serial monitor.

Upload the following code to your Arduino IDE.
/*
* Code for Ultrasonic Sensor HC-SR04
*/
#define trigPin 3
#define echoPin 2
#define led 5
#define led2 6
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); – Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4)
{
// This is where the LED On/Off happens digitalWrite(led,HIGH);
// When the Red condition is met, the Green LED should turn off digitalWrite(led2,LOW);
}
else
{ digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0)
{
Serial.println(“Out of range”);
}
else
{
Serial.print(distance);
Serial.println(” cm”);
}
delay(500);
}
For Arduino IDE Software: Download Here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.