# Using a SR04

59,894

5

4

## Introduction: Using a SR04

Hi There! Today i will show you how to use a SR04 Sensor like in the Video above on my robot Wobot!

How a SR04 Sensor works,

In the program, we want to calculate the distance of an object in front of the ultrasonic sensor. This sensor can send a "ping" at a given moment and receive the ping bouncing back on an object at another given moment.
A ping is nothing but a sound that is inaudible for the human hear and this is why this sensor is called "ultrasonic".
The sensor send a ping at a time t1 and receive the bouncing ping at a time t2.
Knowing the speed of sound, the time difference Δt = t2 - t1 can give us an idea of the distance of an object.

Example, if Δt = 500 microseconds, we know it took 250 microseconds for the ping to hit an object and another 250 microseconds to come back.
The approximate speed of sound in dry air is given by the formula:
c = 331.5 + 0.6 * [air temperature in degrees Celsius]
At 20°C, c = 331.5 + 0.6 * 20 = 343.5 m/s
If we convert the speed in centimetres per microseconds we get:
c = 343.5 * 100 / 1000000 = 0.03435 cm/ss
The distance is therefore, D = (Δt/2) * c
or D = 250 * 0.03435 = 8.6 cm

Instead of using the Speed of Sound, we can also use the "Pace of Sound".
The Pace of Sound = 1 / Speed of Sound = 1 / 0.03435 = 29.1 ss/cm
In this case the equation to calculate the distance become: D = (Δt/2) / Pace of sound
and for the example above: D = 250 / 29.1 = 8.6 cm

Lets start! Here is the code!
_______________________________________________________________________________________________
// include the library code:
#include
#define trigPin 0
#define echoPin 7

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 1, 5, 4, 3, 2);

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
lcd.print("Wobot         :D");
}
else {
lcd.print(distance);
lcd.print(" cm ");
}
delay(500);
}
________________________________________________________________________________________________
This One will hook up to your 16X2 LCD and print the distance

hook Up LCD to      RS = Pin 2 E= Pin 1 D4 = Pin 5 D5 = Pin4 D6= Pin 3 D7=Pin 2

SR04 Trigger  to Pin 0
SR04 Echo to Pin 7

I hope this Helps!

this code does not use the LCD, but prints the CM readings in the Serial monitor under Tools on the Arduino program loader.
________________________________________________________________________________________
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
*/

#define trigPin 0
#define echoPin 7

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
_____________________________________________________________________________________________

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.