UltrasonicSensor | Arduino UNO | LCD

by AndronikosKostas in Circuits > Arduino

768 Views, 3 Favorites, 0 Comments

UltrasonicSensor | Arduino UNO | LCD

Ultrasonic Sensor HY-SRF05 | Arduino UNO | LCD | Calculating Distance

In this project we will cover the following subjects:

1.We will implement the wiring for the ultrasonic sensor HY-SRF05, the LCD in order to get the signals that we want and then process them by programming the Arduino UNO.

2.Taking advantage of the time it takes for a sound wave to travel from the transmitter to a surface and back to the receiver, we will be able to calculate the distance between the surface and the sensor.

3.Finally we will print the result to the LCD.

Components

components01.jpg
components03.jpg
IMG-4993.JPG

So, in order to get started, we will need some components :

- An Arduino UNO and a USB type: A male to B male.

- A LCD 19x4, 5V (16 pins).

- A IIC/I2C module (16 pins / 4 pins ) in order to minimise the wiring between LCD and Arduino UNO.

- An ultrasonic sensor, and in my case, I have the HY-SRF05 ( 5 pins, we only use four).

- 4 x Male-to-female cables that will be hooked up to the I2C module.

- 4 x Male-to-male cables that are essential for the HY-SRF05. - About 6 to 7 helping Male-to-male cables.

That it depends on you :

- 2 x 170 points Breadboard ( or use a breadboard with 830 points e.g. )

Optional :

- A 9V battery and the corresponding connector with a DC plug in the end, which has to be compatible with the DC power port of Arduino UNO.

Programming the Arduino UNO

The concept :

We will apply a 10us pulse to the ultrasonic sensor, which will enable it and the transmission process will start. The sensor, in turn, will send automatically 8 pulses at a frequency 40kHz.Concurrently, the echo pin will be set to HIGH (the sensor will send a pulse to the Arduino UNO). When the receiver (R) detects the ultrasonic waves, the echo pin will be set to LOW (automatically) . After that, with the help of pulseIn(), we will calculate how much time was taken for the echo pin to went from HIGH to LOW. In other words, we will calculate the time the waves were travelling from T to R . Finally, due to the fact that Usound = 343m/s in dry air at 20 degrees Celsius, we will calculate the distance using the formula :

d = Usound * t / 2 . where t the time which will be returned from pulseIn() .

Important :

Before you compile the code press Ctrl + shift + I and search for: LiquidCrystal I2C (by Frank de Brabander) library and install it.

// 26/03/2021
// HY-SRF05 Ultrasonic Distance Sensor.
// Calculating the distance between the sensor and a surface.

#include <LiquidCrystal_I2C.h> 

#define PinToTrig 12  // digital pin 12 of Arduino UNO.
#define PinToEcho 8   // digital pin 8 of Arduino UNO.
#define BitRate 9600  // 9600 bps.
float duration ;
float distance ;
LiquidCrystal_I2C lcd(0x27,19,4); // creating a LiquidCrystal_I2C object.
                                  // The dafault addresses of I2C module are either 0x27 or 0x3F.This address is essential for communication.
                                  // 19 columns , 4 lines.
                                  
void setup() {
 pinMode(PinToTrig , OUTPUT);
 pinMode(PinToEcho , INPUT); //sending data to the ARDUINO --> input.
 Serial.begin(BitRate);      // Starting of the serial communication , sets the data rate to 9600 bps
 
 lcd.init();                //initialization of the LCD screen.
 lcd.print("Distance(cm)=" ) ;
}

void loop() {
  
  distance = calculatingDistance();
  lcd.backlight();
  if(distance > 150)        //distance > 150 cm // remember this sensor read values up to 4000 cm.Think about it like a simple filter.
    return ;
  lcd.setCursor(13,0);      //The counting of setCursor() begins from 1 and we can go up to 20
  lcd.print(distance) ;
  delay(500);              //500ms delay
  Serial.println(distance);//If you don't have a LCD .
  clearLCDLine(13,0,6);     //DIY clear function for the LCD.
                           
}
float calculatingDistance(){
  digitalWrite(PinToTrig, LOW); 
  delayMicroseconds(2);
  //Practically, the following script enables the sensor...it is the trigger !
  //
  digitalWrite(PinToTrig,HIGH);  
  delayMicroseconds(10);
  digitalWrite(PinToTrig,LOW);
  //
  //After that, 8 pulses  at 40kHz automatically are emmitted through air .
  //Concurrently the Echo pin is setted to HIGH. (we can take advantage of that and use it like a clock).
  
  duration = pulseIn(PinToEcho,HIGH); //  if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH.It will fo LOW when the receiver detects the waves.
                                      // The value that will be returned by this function (by default in us) is equal to the time the wave is travelling from transmitter , back to the receiver.
                                      
  distance = duration * 0.01715;      // s = t/2 * u , where u : the speed of sound in dry air at 20 degrees Celsious -> 343 m/s .
                                      // duration is in us or 10^(-6) s.
                                      // multiply by 100 to get the distance in cm .
  return distance ; 
}

 void clearLCDLine(int start ,int line, int numOfBlocksIwantToErase)
{               
        lcd.setCursor(start,line);
        for(int i = 0; i < numOfBlocksIwantToErase; i++) 
        {
                lcd.print(" ");
        }
}<br>

Wiring

IMG-4979.jpg
IMG-4980.jpg
IMG-4981.jpg

I2C-LCD wiring:

Pin GND(I2C) goes to GND

Pin VCC(I2C) goes to 5V

Pin SDA(I2C) goes to A4 (analog pin of Arduino)

Pin SCL(I2C) goes to A5 (analog pin of Arduino)

HY-SRF05 wiring:

Pin GND goes to GND

Pin VCC goes to 5V

Pin TRIG goes to 12 (digital pin of Arduino)

Pin ECHO goes to 8 (digital pin of Arduino)

Ready!

IMG-4994 (1).jpg