Challenge

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

Overview of the Project:

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

Goals:

  • Measuring the distance of an object using Ultrasonic Distance Sensor (HC-SR04)
  • Usage of Input Capture Mode

Key Highlights of the Project:

  • Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
  • Measuring Pulse width using Input Capture Mode

Deliverables of this project:

  • The distance travelled by a wave to the object for which we want to measure the distance using Ultrasonic Distance Sensor (HC-SR04).
  • Measuring Pulse width using Input Capture Mode of Timer 1

Materials:-

  • Arduino Uno R3
  • Ultrasonic sensor
  • LCD display (16x2)
  • 10k potentiometer
  • Jumper wire

Connections:-

Vss = Arduino GND

VDD = Arduino 5V

V0 = Potentiometer center pin

RS = Digital pin 1

RW = Arduino GND

E = Digital pin 2

D4 = Arduino digital pin 3

D5 = Arduino digital pin 4

D6 = Arduino digital pin 5
D7 = Arduino digital pin 6

A = Arduino 5V

K = Arduino GND

Ultrasonic GND = Arduino GND

Ultrasonic 5V = Arduino 5V

Ultrasonic Echo = Arduino digital pin 8

Ultrasonic trig = Arduino digital pin 9

Potentiometer left pin = Arduino 5V

Potentiometer right pin = Arduino GND

Code :-


#include <LiquidCrystal.h> // includes the LiquidCrystal Library

LiquidCrystal lcd(1, 2, 3, 4, 5, 6); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 8;
const int echoPin = 9;
long duration;
int distanceCm, distanceInch;

void setup() {

lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.setCursor(4,0);
lcd.print("Ultrasonic");
lcd.setCursor(6,1);
lcd.print("Sensor");
delay(1000);
lcd.clear();
}
void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration * 0.01330 / 2;

lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
delay(10);
}

Diagram :-

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.