3. Ultrasonic Sensor Principle
• Generate an inaudible sound wave (40 KHz,
human’s cannot hear it, human auditory system is
capable of up to 20 KHz )
• Transmit the sound in open
• Wait for a reflection from near by objects
• Measure the time taken to make te onward
journey and return journey of the sound wave.
• Calculate the distance
3
4. Ultrasonic Sensor HC-SR-04 Module
• HC SR-04 Module
4
Pin1 (Vcc): This pin provides a +5V
power supply to the sensor.
Pin2 (Trigger): This is an input pin,
used to initialize measurement by
transmitting ultrasonic waves by
keeping this pin high for 10us.
Pin3 (Echo): This is an output pin,
which goes high for a specific time
period and it will be equivalent to the
duration of the time for the wave to
return back to the sensor.
Pin4 (Ground): This is a GND pin used
to connect to the GND of the system.
5. Salient Features of SR04
• Power supply: 5V
• Operating current: 15m
• Quiescent Current < 2mA
• Dimension: 45mm
x20mmx15mm
• Pulse width Trigger: 10uS
5
• Resolution: 3 mm
• Measuring angle: 30
deg
• Effectual Angle: < 15
Deg
• Operating frequency
40 KHZ
8. Distance Calculation
• Speed of Sound (S) = 340 m/s= 0.034 cm /us
• Time = Distance / Speed
• Distance = Time X Speed
• Distance = (Time X (0.034 cm/us) )
• (Travelled by the sound wave)
• Distance to Object = (Time X (0.034 cm/us) ) / 2
• Note: divided by 2 is for round trip distance
8
10. Arduino Sketch
// defines pins numbers
const int trigPin = 13;
const int echoPin = 12;
// defines variables
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
10
11. Cont…
void loop()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(2000);
}
11