Challenge

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)

Topic: The implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04)

Aim of the project:

In this project “The implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04)”.

Hardware Requirements:

  1. Arduino UNO board
  2. USB cable connecter for Arduino UNO
  3. 16X2 LCD Screen
  4. I2C Controller
  5. Ultra Sonic Sensor HC-SR04
  6. Jumper wires Male/Female

Software requirements:

  1. Arduino software(Arduino IDE)

    Explanation:

    The ultrasonic sensor emits a high-frequency sound pulse and calculates the distance depending upon the time taken by the echo signal to travel back after reflecting from the desired target. The speed of sound is 341 meters per second in air. After the distance is calculated, it will be displayed on the LCD display.

    Pin Configuration of ULTRASONIC SENSOR:

    16X2 LCD SCREEN PIN DETAILS:

    1. LCD PIN 1-GND
    2. LCD PIN 2-VCC
    3. LCD PIN 3-VEE
    4. LCD PIN 4-RS (Register Select)
    5. LCD PIN 5-RW (Read/Write)
    6. LCD PIN 6-E (Enable)
    7. LCD PIN 7 to PIN 14-Data Bits
    8. LCD PIN 15-LED Positive
    9. LCD PIN 16-LED Negative

I2C SERIAL INTERFACE CONTROLLER:

CIRCUIT CONNECTION:

HARDWARE CONNECTION SETUP:

CODING:

#include <Wire.h> // Include the header Files

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define trigPin 9 //Trig pin connected to pin 9

#define echoPin 8 //Echo pin connected to pin 8

void setup()

{

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

lcd.begin();

lcd.backlight();

lcd.clear();

lcd.setCursor(4,0);

lcd.print("Skill Lync");

delay(1500);

lcd.setCursor(0,0); //Set LCD cursor

lcd.print("Target Distance:"); //Print the given Message

}

void loop()

{

long duration, distance; // Initialize duration and distance

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

lcd.setCursor(0,1); //Set cursor(0,1)

lcd.print(" "); //Print blanks to clear the row

lcd.setCursor(0,1); //Set Cursor again (0,1)

lcd.print(distance); //Print measured distance

lcd.print(" cm"); //Print units.

delay(400); //pause to let things settle

}

OUTPUT:

Distance Measureed-4cm

Distance Measureed-8cm

Distance Measureed-10cm

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.