How to Create a Distance Measuring System using Arduino UNO R3

Introduction 

 
Nowadays, measurement systems are used in motor vehicles like cars, buses, and trucks for auto-braking systems to avoid road accidents and to assist drivers when parking their vehicles. Industries are also using this system in machines like robots, forklifts, trolleys, etc., for moving materials. This operation is done with the help of a distance measurement system.
 
In this article, I am going to explain how to create a distance measuring system using Arduino UNO R3.
 
About Ultrasonic Sensor
 
The basics of ultrasonic sensors are like a RADAR system. This sensor contains one transmitter and one receiver drum. If the transmitter sends a small pulse of signal when the signal reaches any object it returns back to the receiver. We are calculating the distance using the traveling time of the signal in the air. This is a working principle of the ultrasonic sensor.
 
 
Prerequisites for developing this system:
 
Hardware requirements
 
Hardware  Nos 
Arduino UNO  1
Ultrasonic Sensor (HC-SR04)  1
LCD crystal display  1
Potentiometer 10k ohm   1
Breadboard  -
220-ohm Resistor  1
Scale (30 CM)  1
Connecting wires  - 
 
Software requirements:
  • Arduino IDE (For uploading code to Arduino) 
There are three stages for developing this system:
  1. Make the hardware connection
  2. Write & upload code to Arduino
  3. Testing our developed system
Stage 1: Make the hardware connection
 

Step 1
 
First, we can connect the LCD crystal display with the Arduino UNO board using jumper wires. I am using a breadboard to extend power terminals for making this system.
Refer to the below LCD crystal pin diagram for making an easy connection with Arduino.
 
 
 
Place a 10K ohm potentiometer near the LCD crystal display. It can adjust the contrast of the LCD screen and create a power connection from Arduino +5V and GND right and left end of the potentiometer. Make a connection from the center terminal of the potentiometer to VEE (contrast control) LCD crystal display. (Refer to the connection diagram)
 
Pin Configuration
 
Arduino                               Breadboard                                      LCD Crystal Display
Pin 4 ---------------------------------------------------------------------------> Data pin 4
Pin 5 ---------------------------------------------------------------------------> Data pin 5
Pin 6 ---------------------------------------------------------------------------> Data pin 6
Pin 7 ----------------------------------------------------------------------------> Data pin 7
Pin 9 ----------------------------------------------------------------------------> Register select
Pin 8 ----------------------------------------------------------------------------> Enable
+ 5V -----------------> Potentiometer Right Terminal---------------------->Potentiometer Centre terminal ----> VEE (Contrast Control) LCD crystal
+5V ---------------------------> 220-ohm Resistor ----------------------------> LED +5V pin [LCD Crystal]
GND ------------------------------------------------------------------------------->Potentiometer Left terminal, Ground pin & LED - Ground pin [Connect to both pin]
 
Step 2
 
Next, Connect Ultrasonic Sensor to Arduino board as per below pin Configuration.
 
Pin Configuration
 
Ultrasonic Sensor                                                         Arduino
Vcc ---------------------------------------------------------------> + 5V
ECHO -------------------------------------------------------------> 2
TRIG ---------------------------------------------------------------> 3
GND ----------------------------------------------------------------> GND
 
Stage 2 [Write & upload code to Arduino]
 
In this step, write code for Arduino using the Arduino IDE.
  1. Download Arduino software using this link here.
  2. Install and open an Arduino IDE in your PC/Laptop
  3. Connect your Arduino board to your computer using a USB data cable
  4. Copy and paste below the code into the Arduino IDE
    1. #include "LiquidCrystal.h"    
    2. #define HCSR04_PIN_TRIG  3    
    3. #define HCSR04_PIN_ECHO 2    
    4. #define LCD_PIN_RS  9    
    5. #define LCD_PIN_E 8    
    6. #define LCD_PIN_DB4 4    
    7. #define LCD_PIN_DB5 5    
    8. #define LCD_PIN_DB6 6    
    9. #define LCD_PIN_DB7 7    
    10. #define HCSR04_PIN_TRIG 3    
    11. #define HCSR04_PIN_ECHO 2    
    12.     
    13. float time=0,measure1=0;    
    14.     
    15. LiquidCrystal lcd(LCD_PIN_RS,LCD_PIN_E,LCD_PIN_DB4,LCD_PIN_DB5,LCD_PIN_DB6,LCD_PIN_DB7);    
    16.     
    17. void setup() {    
    18.  Serial.begin(9600);    
    19.  lcd.begin(16, 2);    
    20.  pinMode(HCSR04_PIN_TRIG,OUTPUT);    
    21.  pinMode(HCSR04_PIN_ECHO,INPUT);    
    22.   lcd.print("C#corner");    
    23.  lcd.setCursor(0,1);    
    24.  lcd.print("Measuring system");    
    25.  delay(2000);    
    26.     
    27. }    
    28.     
    29. void loop() {    
    30.   lcd.clear();    
    31.  digitalWrite(HCSR04_PIN_TRIG,LOW);    
    32.  delayMicroseconds(2);    
    33.  digitalWrite(HCSR04_PIN_TRIG,HIGH);    
    34.  delayMicroseconds(10);    
    35.  digitalWrite(HCSR04_PIN_TRIG,LOW);    
    36.  delayMicroseconds(2);    
    37.  time=pulseIn(HCSR04_PIN_ECHO,HIGH);    
    38.  measure1=time*340/20000;    
    39.  lcd.clear();    
    40.  lcd.print("Distance:");    
    41.  lcd.print(measure1);    
    42.  lcd.print("cm");    
    43.  lcd.setCursor(0,1);    
    44.  lcd.print("Distance:");    
    45.  lcd.print(measure1/100);    
    46.  lcd.print("m");    
    47.  delay(1000);    
    48. }    
Save and upload this code to the Arduino UNO board. After Code upload is completed, the device can start to measure distance.
 
Stage 3 [Test our developed system]
 
Our developed distance measurement system can get ready to measure.
 
 
 
In the below diagram, I am creating a block in front of the sensor between 5 CM. It can measure using any scales. Measurements are working perfectly.
 
 
 

Conclusion

 
In this article, we studied how to Create Distance Measuring System using Arduino UNO R3.