Digital Distance Measuring Device using Ultrasonic sensor and Arduino Board with code

 

Distance Measuring using Ultrasonic Sensor with Circuit Diagram



Materials Requirements:


  • Arduino board (Uno, Nano, or Mega)
  • Ultrasonic sensor module HC-SR04
  • Jumper wires
  • 16x2 LCD display module
  • 10K Ohm Potentiometer
  • 220 Ohm resistor
  • USB cable for Arduino programming and powering


Also Check - Distance Measuring using Ultra-sonic Sensor HC-SR04  using  Arduino Uno Microcontroller


Circuit Connection Explanation

  1. Connect the Vcc pin of the ultrasonic sensor to the 5V pin of the Arduino board.
  2. Connect the GND pin of the ultrasonic sensor to the GND pin of the Arduino board.
  3. Connect the Echo pin of the ultrasonic sensor to digital pin 2 of the Arduino board.
  4. Connect the Trig pin of the ultrasonic sensor to digital pin 3 of the Arduino board.
  5. Connect the Register Select (RS) pin of the LCD display module to digital pin 4 of the Arduino board.
  6. Connect the Enable (EN) pin of the LCD display module to digital pin 5 of the Arduino board.
  7. Connect the data pin D4, D5, D6, and D7 pins of the LCD display module to digital pins 6, 7, 8, and 9 of the Arduino board.
  8. Connect the Read/Write (RW) pin of the LCD display module to the GND pin of the Arduino board.
  9. Connect the Vcc pin of the LCD display module to the 5V pin of the Arduino board through the 10k potentiometer. The middle pin of the potentiometer should be connected to the V0 pin (Contrast) of the LCD display module.
  10. Connect the 3rd pin of the 10k ohms potentiometer to the GND pin of the Arduino board.
  11. Connect Led+ (Anode) of LCD display to 5v pin of Arduino board with 220ohms resistor in series.
  12. Connect Led- (Cathode)of LCD display to GND of Arduino board. 

Note: Don't give directly the LED+ (Anode) of LCD Display 5 volts of the Arduino board; it will damage the LCD board because the current flow will be higher, so add a 220-ohm resistor in series.

Code Explanation

We have to include the <LiquidCrystal.h library, which allows us to control the LCD display module or download it separately if don't have it in your Arduino  IDE.

1
#include <LiquidCrystal.h>


We need to create an instance of the LiquidCrystal class and specify the pins to which the LCD display module is connected as per the circuit diagram and the pin connected to the board.


1
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);


We need to declare two variables for the sensor to store the digital pins to which the ultrasonic sensor's Trig and Echo pins are connected.

1
2
int trigPin = 3;
int echoPin = 2;


For the setup() function, we need to initialize the LCD display module and set the Trig and Echo pins of the ultrasonic sensor as output and input, respectively

1
2
3
4
5
void setup() {
  lcd.begin(16, 2);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}


For the loop() function, we need to first set the Trig pin to LOW, wait for 2 microseconds, set it to HIGH for 10 microseconds, and then set it to LOW again to generate an ultrasonic pulse from the ultrasonic sensor.

1
2
3
4
5
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);


Then use the pulseIn() function to measure the duration of the pulse and calculate the distance in centimeters based on the speed of sound 29.1 microseconds per centimeter.

1
2
duration = pulseIn(echoPin, HIGH);
cm = (duration / 2) / 29.1;


Finally, we are printing the ultrasonic sensor distance on the LCD display and waited for 1 second before repeating the process again.

1
2
3
4
5
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(cm);
lcd.print(" cm     ");
delay(1000);


LCD Display with distance detected by Ultrasonic Sensor


Also Check -  Controlling two Ultrasonic Sensor with Arduino Uno with code | HC-SR04 module control using Arduino with code


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
#include <LiquidCrystal.h>

LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
int trigPin = 3;
int echoPin = 2;
long duration, cm;

void setup() {
  lcd.begin(16, 2);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  cm = (duration / 2) / 29.1;
  lcd.setCursor(0, 0);
  lcd.print("Distance: ");
  lcd.print(cm);
  lcd.print(" cm     ");
  delay(1000);
}



Thank You!
Have a Great Day!