Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
rudraksh2008
Published

Ultrasonic sensor interfacing with Arduino

By this project you can connect sonar or ultrasonic sensor with your Arduino and measure distance with Arduino and print it on a display

BeginnerFull instructions provided3,354
Ultrasonic sensor interfacing with Arduino

Things used in this project

Hardware components

Male to female wires
×1
Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
I2C LCD display
×1

Software apps and online services

Arduino IDE

Story

Read more

Schematics

I2C LCD with Arduino Connection

You can see this picture and can connect LCD with Arduino and also connect ultrasonic sensor as given in the image

By this you can connect Ultrasonic sensor with arduino and also connect LCD with Arduino!

Connect your sensor with Arduino and you can see the layout on your I2C LCD screen

Code

Here is the code by which you can print distance on your I2C LCD screen

C/C++
Please type the code 2 to 3 times so you can get familiar with this code
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 column and 4 rows

int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin

float duration_us, distance_cm;

void setup() {
  lcd.init();               // initialize the lcd
  lcd.backlight();
  pinMode(trigPin, OUTPUT); // config trigger pin to output mode
  pinMode(echoPin, INPUT); 
  lcd.clear();              // config echo pin to input mode
}

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;
  

  lcd.setCursor(0, 0);
  lcd.print("DISTANCE IN CM-");
  lcd.setCursor(0, 1);
  lcd.print(distance_cm);

  delay(10);

}
  

Credits

rudraksh2008

rudraksh2008

4 projects • 1 follower

Comments