Exp 5
Exp 5
Exp 5
3d class.
Control Lab.
Experiment 5
Ultrasonic sensor
Introduction
Ultrasonic emits an ultrasound at 40 000 Hz which travels through the air and if
there is an object or obstacle on its path It will bounce back to the module.
Considering the travel time and the speed of the sound you can calculate the
distance.
Hardware:-
In this experiment we will use the HC-SR04 Ultrasonic Module. The HC-SR04
Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the
VCC pins of the module needs to be connected to the Ground and the 5 volts pins
on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin
on the Arduino Board.
In order to generate the ultrasound you need to set the Trig on a High State for 10
µs. That will send out an 8 cycle sonic burst which will travel at the speed sound
and it will be received in the Echo pin. The Echo pin will output the time in
microseconds the sound wave traveled.
For example, if the object is 10 cm away from the sensor, and the speed of the
sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u
seconds. But what you will get from the Echo pin will be double that number
because the sound wave needs to travel forward and bounce backward. So in order
Elect. Eng. Dep.
3d class.
Control Lab.
to get the distance in cm we need to multiply the received travel time value from
the echo pin by 0.034 and divide it by 2.
Software:-
First you have to define the Trig and Echo pins. In this case they are the pins
number 9 and 10 on the Arduino Board and they are named trigPin and echoPin.
Then you need a Long variable, named “duration” for the travel time that you will
get from the sensor and an integer variable for the distance.
In the setup you have to define the trigPin as an output and the echoPin as an Input
and also start the serial communication for showing the results on the serial
monitor.
In the loop first you have to make sure that the trigPin is clear so you have to set
that pin on a LOW State for just 2 µs. Now for generating the Ultra sound wave we
have to set the trigPin on HIGH State for 10 µs. Using the pulseIn() function you
have to read the travel time and put that value into the variable “duration”. This
function has 2 parameters, the first one is the name of the echo pin and for the
second one you can write either HIGH or LOW. In this case, HIGH means that
the pulsIn() function will wait for the pin to go HIGH caused by the bounced
sound wave and it will start timing, then it will wait for the pin to go LOW when
the sound wave will end which will stop the timing. At the end the function will
return the length of the pulse in microseconds. For getting the distance we will
multiply the
The following program read the distance of obstacle and print it on serial monitor.
1. const int trigPin = 9;
2. const int echoPin = 10;
3. long duration;
4. int distance;
5.
6. void setup() {
7. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
8. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
9. Serial.begin(9600); // Starts the serial communication
10. }
11. void loop() {
12. digitalWrite(trigPin, LOW);
13. delayMicroseconds(2);
14.
15. // Sets the trigPin on HIGH state for 10 micro seconds
16. digitalWrite(trigPin, HIGH);
17. delayMicroseconds(10);
18. digitalWrite(trigPin, LOW);
19.
20. // Reads the echoPin, returns the sound wave travel time in microseconds
21. duration = pulseIn(echoPin, HIGH);
22.
23. // Calculating the distance
24. distance= duration*0.034/2;
25. Serial.print("Distance: ");
26. Serial.println(distance);
27. }
Libraries can be used to facilitate dealing with ultrasonic. Among these libraries is
newping.
The projects
Proj 1: Design MCU system to find distance of an object and print the distance on
LCD with 3 seconds update period?
Proj 2: Design MCU system to find distance of an object and print the distance on
LCD, when the distance is less than 20 cm, a green led is turn on and when the
distance become less than 12 cm, a red led should be turned on and when it is less
than 10 cm, a warning sound generated from buzzer is turned on.