In this tutorial, I will be able to show you the way to create an ultrasonic distance meter using Arduino. This project uses an HC-SR04 distance sensor and atmega328P or the other Atmega chip which is programmed using an Arduino programming kit, and the project is predicated on Arduino. But our final Circuit is a standalone circuit without the attached Arduino programmer.
Must Read Ultrasonic Sound Generator
Project
Circuit Diagram
Components Required
- HC-SR04 Ultrasonic Sensor
- Arduino
- 16*2 LCD Display
- Jumper Wires
- Breadboard
- 5V Power Supply
Circuit Connection
LCD display to Arduino Nano setup connection
- Connect LCD pins 1, 3, 5,16 to the GND pin of Arduino Connect LCD pin 4 to the
- pin D7 of ArduinoConnect LCD pin 6 to pin D6 of ArduinoConnect LCD pin 11 to
- pin D5 of ArduinoConnect LCD pin 12 to pin D4 of ArduinoConnect LCD pin 13 to
- pin D3 of ArduinoConnect LCD pin 14 to pin D2 of Arduino
- Connect LCD pins 2, and 15 to the VCC(+5V) pin of the Arduino
- Connect HC-SR04 VCC pin to +5V pin of Arduino
- Connect HC-SR04 GND pin to GND pin of Arduino
- Connect the Trig pin to pin D9 of the Arduino
- Connect the Echo pin to pin D8 of the Arduino
Working Principle of Ultrasonic Distance Meter
In this ultrasonic distance meter working principle, we can send a brief ultrasonic pulse at the trigger, and if any obstacles are there, the reflected received ultrasonic signal is converted to an electrical signal as an echo. If no obstacle is detected, the output pin will provide a 38ms high-level signal.
Here, we calculate the gap between the ultrasonic distance meter and any object before it. At t1 we send the trigger signal, and at t2 we get the echo signal
dt = (t2-t1)
The exact time taken by the sound to succeed in the item is: dt/2
Now, the Normal speed of sound is: 340 m/s
And after converting it to centimetres per microsecond it gives: 0.034049 cm/microsecond
The actual distance travelled by the sound or the gap between the sensor and also the object is:
Object = (dt/2) * 0.034049
By using this formula we can get the output of the actual distance of an object from an ultrasonic distance meter.
Application of Ultrasonic Distance Meter
- To measure the distance of a specific object.
- Used in radar systems.
- Used in industrial processes.
Arduino Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #define trigPin 7 #define echoPin 8 #include <LiquidCrystal.h> LiquidCrystal lcd(7, 6, 5, 4, 3, 2); void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); lcd.begin(16, 2); lcd.print("Electro"); lcd.setCursor(0, 1); lcd.print("Gadget"); delay(2000); lcd.clear(); lcd.print("Ultrasonic"); lcd.setCursor(0, 1); lcd.print("Distnance Meter"); delay(2000); lcd.clear(); lcd.print("Designed by:"); lcd.setCursor(0, 1); lcd.print("Electro Gadget"); } void loop() { int timetaken, dist; lcd.clear(); lcd.print("Distance in cm:"); digitalWrite(trigPin, HIGH); delayMicroseconds(1000); digitalWrite(trigPin, LOW); timetaken = pulseIn(echoPin, HIGH); dist = (timetaken/2) * 0.034049 ; if (dist >= 300 || dist <= 0) { lcd.setCursor(0, 1); lcd.print("Out Of Range"); } else { lcd.setCursor(0, 1); lcd.print(dist); } delay(500); } |