Components Used
- Arduino Uno or Pro Mini
- Ultrasonic sensor Module
- 16x2 LCD
- Scale
- Bread board
- 9 volt battery
- Connecting wires
Ultrasonic Sensor Module
Ultrasonic sensor HC-SR04 is used here to measure distance in range of 2cm-400cm with accuracy of 3mm. The sensor module consists of ultrasonic transmitter, receiver and the control circuit. The working principle of ultrasonic sensor is as follows:
High level signal is sent for 10us using Trigger.
The module sends eight 40 KHz signals automatically, and then detects whether pulse is received or not.
If the signal is received, then it is through high level. The time of high duration is the time gap between sending and receiving the signal.
Distance= (Time x Speed of Sound in Air (340 m/s))/2
Timing Diagram
The module works on the natural phenomenon of ECHO of sound. A pulse is sent for about 10us to trigger the module. After which the module automatically sends 8 cycles of 40 KHz ultrasound signal and checks its echo. The signal after striking with an obstacle returns back and is captured by the receiver. Thus the distance of the obstacle from the sensor is simply calculated by the formula given as
Distance= (time x speed)/2.
Here we have divided the product of speed and time by 2 because the time is the total time it took to reach the obstacle and return back. Thus the time to reach obstacle is just half the total time taken.
Ultrasonic Sensor Arduino Circuit Diagram
The circuit diagram for arduino and ultrasonic sensor is shown above to measure the distance. In circuit connections Ultrasonic sensor module’s “trigger” and “echo” pins are directly connected to pin 18(A4) and 19(A5) of arduino. A 16x2 LCD is connected with arduino in 4-bit mode. Control pin RS, RW and En are directly connected to arduino pin 2, GND and 3. And data pin D4-D7 is connected to 4, 5, 6 and 7 of arduino.
First of all we need to trigger the ultrasonic sensor module to transmit signal by using arduino and then wait for receive ECHO. Arduino reads the time between triggering and Received ECHO. We know that speed of sound is around 340m/s. so we can calculate distance by using given formula:
Distance= (travel time/2) * speed of sound
Where speed of sound around 340m per second.
A 16x2 LCD is used for displaying distance.
Arduino Ultrasonic Sensor Code for Distance Measurement
In code we read time by using pulseIn(pin). And then perform calculations and displayed result on 16x2 LCD by using appropriate functions.
Code
#include <LiquidCrystal.h>
#define trigger 18
#define echo 19
LiquidCrystal LCD (2,3,4,5,6,7);
float time = 0, distance = 0;
void setup ()
{
lcd.begin (16,2);
pinMode (trigger, OUTPUT);
pinMode (echo, INPUT);
lcd.print ("Ultra sonic");
lcd.setCursor (0,1);
lcd.print ("Distance Meter");
delay (2000);
lcd.clear ();
lcd.print ("Mechatronicslab");
delay (2000);
}
void loop ()
{
lcd.clear ();
digitalWrite (trigger, LOW);
delayMicroseconds (2);
digitalWrite (trigger, HIGH);
delayMicroseconds (10);
digitalWrite (trigger, LOW);
delayMicroseconds (2);
time = pulseIn (echo, HIGH);
distance = time * 340/20000;
lcd.clear ();
lcd.print ("Distance:");
lcd.print (distance);
lcd.print ("cm");
lcd.setCursor (0,1);
lcd.print ("Distance:");
lcd.print (distance / 100);
lcd.print ("m");
delay (1000);
}
No comments