Distance measurement is a part of the day to day life. Sometimes, the size of a room needs to be measured before shifting and adjusting furniture into it or the dimensions of a table need to be measured for selecting it for study. Measurement of distance is something that is often done in daily life. Usually, for measuring distances, a measuring tape is used. However, the use of measuring tape has certain disadvantages.
First, it requires two persons to hold the measuring tape for taking a reading. Secondly, measuring tape requires careful alignment along the dimensions under measurement and the reading need to be taken carefully. Even sometimes measuring destination is not properly reachable or approachable. Considering these common disadvantages of the use of measuring tape, the idea of an ultrasonic distance measurer came up as a viable electronic solution for the problem.
The electronic distance measurer designed in this project is built on Arduino Pro Mini, small and portable Arduino board popular for making handheld gadgets and it uses HC-SR04 ultrasonic sensor that can detect distances up to 4 meters. The range of the sensor used in the project is enough for day to day uses. The only limitation of the device is that being based on an ultrasonic sensor which measures distance by the reflection of the sound waves, it requires any corner or reflecting surface at the other end as a reference for distance measurement. So the device would not be much useful for measuring distances over open edges.
The distance reading is displayed on an LCD display. The Arduino sketch manages to handle the ultrasonic sensor, take readings, display readings on LCD and handles user input through a switch. The Arduino sketch is written using Arduino IDE and burnt to the board using the same.
Components Required –
1) Arduino Pro Mini 5V/16 MHz
2) HC-SR04 Ultrasonic Sensor
3) 16X2 character LCD
4) 1.5 V Batteries – 4
5) Tactile switch – 1
6) 10K Pot
Block Diagram –
Fig. 1: Block Diagram of Arduino based Ultrasonic Distance Measurement Device
Circuit Connections –
Fig. 2: Prototype of Arduino based Portable Ultrasonic Distance Measurement Device
The gadget is built on Arduino Pro Mini. The 5V 16 MHz Arduino Pro Mini is used for the project as other modules used in the device require 5V DC for their operation. If 3.3 V Arduino Pro Mini would have been used, then additional power circuit to step down the supply voltage would have been required. That is why 5V Arduino board has been taken.
The Arduino board is the central unit controlling the entire functionality of the project. The ultrasonic sensor and the LCD display are interfaced to the Arduino board. The sensor and the display module have the following circuit connections with the Arduino –
HC-SR04 Ultrasonic Sensor – The ultrasonic sensor is connected to pins 9 and 8 of the Arduino board. The ultrasonic sensor has four pins – Ground (Pin 1), Echo (Pin 2), Trigger (Pin 3) and Trigger. The VCC and ground pins are connected to common VCC and Ground respectively. The Echo pin is connected to pin 8 of the Arduino board while Trigger pin is connected to pin 9 of the Arduino board. The ultrasonic sensor works on the principle of echo of sound waves.
When a HIGH pulse of 10 u sec is passed to the trigger pin of the sensor, it transmits eight 40 KHz waves of HIGH Sonic Pulse shots back to back. A High pulse signal is out from the echo pin as the ultrasonic wave is transmitted. This wave when collides with an obstacle, it is reflected back and detected by the sensor. On detecting the wave again, the High pulse signal from the echo pin of the sensor is terminated. The signal received from the echo pin is analog in nature. The distance from the obstacle can be measured by measuring the high time of the echo pin. This is the time between the transmission and reflection back of the sonic wave. The distance is given by the formulae –
Test distance = (high level time × velocity of sound (340M/S)) / 2
The time multiplied by velocity is divided by 2 as the time taken is for the sonic wave to reach obstacle and return back. Therefore the distance measurement in cm can be given by the formulae –
Test distance = (high level time × velocity of sound (340M/S)) / 2
= (high level time(microsecond) × velocity of sound (340M/S)) / 2
= high level time x 340/2000000 m
= high level time x 34000/2000000 cm
= high level time x 34/2000 cm
The ultrasonic sensor outputs the high pulse from pin 2 which is detected at the pin 8 of the Arduino Board. The program code measures the pulse duration and digitize it to a distance value using the formulae stated above.
16X2 LCD – The 16X2 LCD display is used to display the measured distance. It is connected to the Arduino board by connecting its data pins to pins 4 to 7 of the Arduino board. The RS and E pins of the LCD are connected to pins 2 and 3 of the Arduino board respectively. The RW pin of the LCD is grounded.
Fig. 3: Table listing circuit connections between Arduino Uno and Character LCD
The standard open-source library for interfacing LCD with Arduino board is used in the project. The library works as expected and needs no changes or modifications.
Tactile switch – A tactile switch is connected to pin 10 of the Arduino board. The user has to press this switch whenever, the distance reading has to be taken, only then the device will measure distance. The switch is connected between the Arduino pin and the VCC. The interfaced Arduino pin by default has ground connection.
Power Supply – The power to the circuit is supplied by a series of four 1.5 V batteries. The output of the series battery is 6V in total that is enough to drive the circuit even after the resistive losses. Any nickel cadmium, Lithium-ion or Lithium-Polymer battery with 5V output can also be used.
Fig. 4: Circuit Diagram of Power Supply for Arduino based Portable Ultrasonic Distance Measurement Device
How the Circuit Works?
Fig. 5: Image of Arduino based Portable Ultrasonic Distance Measurement Device
The functioning of the circuit is quite simple. The user has to press the push button for distance measurement. As the user presses the button, a HIGH logic is received at pin 10 of the Arduino board which by default has been receiving LOW logic. As the HIGH logic is detected at pin 10, the Arduino board activates the ultrasonic sensor. For reading data from the ultrasonic sensor, first a pulse has to be sent to the trigger pin of the sensor and then analog voltage is read from the echo pin.
The analog voltage is converted to digital reading using in-built ADC channel and converted to distance measurement using the formulae stated above. The digitized reading is stored in a variable and displayed on LCD display in a formatted string. The read distance measurement remains on the display until user prompts for another reading or the device is switched off. When user again presses the button, the LCD display is cleared and the distance is again measured and displayed as already described.
Check out the project code for learning how the arduino board detects pressing of the button, takes reading from ultrasonic sensor and display reading on the LCD display.
Programming Guide –
First of all, the Arduino sketch loads the required libraries like the liquid crystal library for the LCD interfacing. The variables are declared and assigned Arduino pins according to the circuit connections. A variable is declared to store the duration of the echo pulse and a variable is declared to hold the distance reading.
The setup() function is called in which, the baud rate for the serial communication with the LCD module is set to 9600 baud per second. The Arduino pin connected to the trigger pin of the ultrasonic sensor is set digital output while the Arduino pins connected to the push button and the echo pin of the ultrasonic sensor are set digital input. The LCD display is initialized using begin() method on the LCD object and some initial messages are flashed on it. The setup() function runs only for once after the Arduino board is powered on.
The loop() function is called which iterates infinitely. The loop() function detects if the push button is pressed by checking the status of pin 10. If the switch has been pressed, the trigger pin of the ultrasonic sensor is first set to LOW and then a HIGH pulse for 10 microseconds is passed to the pin. The pin is again set to LOW after 10 Microseconds.
The pulse duration from the echo pin is measured using the pulseIn() function and the distance reading is stored in a variable. The device is set to measure distances up to 300 cm. If the distance detected by the device is within limit, it is displayed on LCD otherwise an error message showing “Out of Range” is displayed on the LCD screen. This is all logic implemented by the Arduino sketch for Ultrasonic Distance Measurement Project.
Project Source Code
###
//Program to #include <LiquidCrystal.h> LiquidCrystal lcd(2, 3, 4, 5, 6, 7); int sw = 10; int outputPin = 9; // set you output pin int inputPin = 8; // set your input pin long distance; long cm; void setup(){ Serial.begin(9600); // set your baud rate pinMode(outputPin, OUTPUT); pinMode(inputPin,INPUT); pinMode(sw, OUTPUT); lcd.begin(16, 2); lcd.setCursor(0, 0); lcd.print("ENGINEERS GARAGE"); lcd.setCursor(0, 2); lcd.print("Distance="); } void loop() { int SW=digitalRead(sw); digitalWrite(outputPin, LOW); delayMicroseconds(2); digitalWrite(outputPin, HIGH); delayMicroseconds(10); digitalWrite(outputPin, LOW); distance = pulseIn(inputPin, HIGH); //it now initalizes before setup to speed up the process cm= distance/58; //I don't like reusing the same variable name, it probably works fine though, //but i don't really trust it, besides you have plenty of memory for it. if (SW == HIGH) { if (cm <= 200 || cm >= 0) { Serial.println(cm); delay(500); lcd.setCursor(10, 2); lcd.print(cm); lcd.print(" cm"); delay(100); if (cm > 300) { lcd.setCursor(10, 2); // lcd.print(cm+1); lcd.print("O of R "); // out of range delay(100); } } } Serial.println(cm); delay(500); // this will slow down your readings to 2 per second it should help when watching the Serial terminal // if this helps then start to shrink it down to 100 then 10 then 1, maybe even go to microsecond delays or remove completely if it stops freezing }
###
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.