Distance Measuring Using UltraSonic Sensor In LCD Display

Introduction


In this article, I will explain how to measure the distance from any object. It can be easily displayed in the LCD Screen. We can use it on the front door to view the distance of people.

Parts of List
  • Arduino UNO
  • Ultrasonic Sensor (HC-SR04)
  • BreadBoard
  • LCD Screen
  • HookUp Wires.
Ultrasonic Sensor (HC-SR04)
 
url
 
Figure 1: Ultrasonic Sensor(HC-SR04)
  • It is used to produce a high-frequency sound effect.
  • It can measure the signal from the sender to the receiver.
  • The echo to be determined from the object.
Where ever we can fix the Ultrasonic Sensor(HC-SR04)
  • Hospital for medicine
  • Industry
  • Home etc.
LCD Screen
 
Figure 2: LCD Screen
  • LCD is Liquid Crystal Display (LCD).
  • It can have the flat-panel or the electronic display
  • It is used in hospitals, showrooms, buses, railway stations, airports, etc.
Connections

Connection from the Ultrasonic Sensor (HC-SR04) to the Arduino board

In the sensor it has the following four pins; they can work under the process of the receiving and the sending of the signal.
  • Vcc in the Ultrasonic Sensor can be connected to the Arduino board of the 5V of Vcc.
  • Triger in the Ultrasonic Sensor can be connected to the Arduino board of the digital pin 12.
  • Echo pin the Ultrasonic Sensor can be connected to the Arduino board of the digital pin 08.
  • Gnd in the Ultrasonic Sensor can be connected to the Arduino board of the Gnd.
Connection from LCD Screen to the Arduino board

The LCD screen can have the following four pins.
  • The Vcc can be connected to the vcc of the Arduino board.
  • The Gnd can be connected to the Gnd of the Arduino board.
  • The SDA can be connected to the Arduino board of the analog pin A4 .
  • The SCL can be connected to the Arduino board of the analog pin A5.
Before uploading the program add the library:
  • Liquid crystal
  • I2CAddr0x3F
Programming
  1. #define trigPin 12    
  2. # define echoPin 8    
  3. //Flashing LED on Arduino board      
  4. # define LEDPin 13    
  5. //LCD      
  6. # include < Wire.h >     
  7. #include < LCD.h >     
  8. #include < LiquidCrystal_I2C.h >     
  9. #define I2C_ADDR 0x3F     
  10. // Define I2C Address where the PCF8574A is      
  11. # define BACKLIGHT_PIN 3    
  12. # define En_pin 2    
  13. # define Rw_pin 1    
  14. # define Rs_pin 0    
  15. # define D4_pin 4    
  16. # define D5_pin 5    
  17. # define D6_pin 6    
  18. # define D7_pin 7    
  19.     
  20. int n = 1;    
  21. LiquidCrystal_I2Clcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);    
  22. void setup()    
  23. {    
  24.     Serial.begin(9600);    
  25.     pinMode(trigPin, OUTPUT); //The transmit pin of the ultrasonic sensor      
  26.     pinMode(echoPin, INPUT); //The receive pin of the ultrasonic sensor      
  27.     pinMode(LEDPin, OUTPUT); //The LED of the Arduino      
  28.     lcd.begin(20, 4);    
  29.     //Size of LCD      
  30.     // Switch on the backlight      
  31.     lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);    
  32.     lcd.setBacklight(HIGH);    
  33.     lcd.home(); // go home      
  34. }    
  35. void loop()    
  36. {    
  37.     int duration, distance;    
  38.     digitalWrite(trigPin, HIGH);    
  39.     delayMicroseconds(100);    
  40.     digitalWrite(trigPin, LOW);    
  41.     duration = pulseIn(echoPin, HIGH);    
  42.     distance = (duration / 2) / 29.1;    
  43.     lcd.clear();    
  44.     lcd.setCursor(0, 0);    
  45.     lcd.print("Distance from object");    
  46.     lcd.setCursor(0, 1);    
  47.     lcd.print(distance);    
  48.     lcd.print("cm");    
  49.     if (distance >= 15)    
  50.     {    
  51.         lcd.setCursor(0, 4);    
  52.         lcd.print("Safe Zone :)");    
  53.         digitalWrite(LEDPin, HIGH);    
  54.         delay(500);    
  55.         digitalWrite(LEDPin, LOW);    
  56.         delay(500);    
  57.     }    
  58.     else    
  59.     {    
  60.         lcd.clear();    
  61.         lcd.setCursor(0, 0);    
  62.         lcd.print("STEP AWAY!!!");    
  63.         lcd.setCursor(0, 1);    
  64.         lcd.print("STEP AWAY!!!");    
  65.         lcd.setCursor(0, 2);    
  66.         lcd.print("STEP AWAY!!!");    
  67.         lcd.setCursor(0, 3);    
  68.         lcd.print("STEP AWAY!!!");    
  69.         digitalWrite(LEDPin, HIGH);    
  70.         delay(50);    
  71.         digitalWrite(LEDPin, LOW);    
  72.         delay(50);    
  73.         digitalWrite(LEDPin, HIGH);    
  74.         delay(50);    
  75.         digitalWrite(LEDPin, LOW);    
  76.         delay(50);    
  77.         digitalWrite(LEDPin, HIGH);    
  78.         delay(50);    
  79.         digitalWrite(LEDPin, LOW);    
  80.         delay(50);    
  81.         digitalWrite(LEDPin, HIGH);    
  82.         delay(50);    
  83.         digitalWrite(LEDPin, LOW);    
  84.         delay(50);    
  85.         digitalWrite(LEDPin, HIGH);    
  86.         delay(50);    
  87.         digitalWrite(LEDPin, LOW);    
  88.         delay(50);    
  89.         digitalWrite(LEDPin, HIGH);    
  90.         delay(50);    
  91.         digitalWrite(LEDPin, LOW);    
  92.         delay(50);    
  93.         digitalWrite(LEDPin, HIGH);    
  94.         delay(50);    
  95.         digitalWrite(LEDPin, LOW);    
  96.         delay(50);    
  97.         digitalWrite(LEDPin, HIGH);    
  98.         delay(50);    
  99.         digitalWrite(LEDPin, LOW);    
  100.         delay(50);    
  101.         digitalWrite(LEDPin, HIGH);    
  102.         delay(50);    
  103.         digitalWrite(LEDPin, LOW);    
  104.         delay(50);    
  105.         digitalWrite(LEDPin, HIGH);    
  106.         delay(50);    
  107.         digitalWrite(LEDPin, LOW);    
  108.         delay(50);    
  109.         digitalWrite(LEDPin, HIGH);    
  110.         delay(50);    
  111.         digitalWrite(LEDPin, LOW);    
  112.         //That really long repeat is to allow the LED pin on the Arduino      
  113.     }    
  114. }   
    Explanation

    In this case, it can send and receive a signal from the ultrasonic sensor, when the object is near it can be displayed in the LCD screen.

    When the object is near it can detect the SAFE ZONE message in the LCD screen.

    When the object is a way it can detect the STEP AWAY message in the LCD screen.

    Output

     
    Figure 3:Output
     
    Read more articles on Arduino: