HC-SR04 + LCD + Arduino: Ultrasonic Rangefinder Display

by UnicornClockworks in Circuits > Arduino

2763 Views, 7 Favorites, 0 Comments

HC-SR04 + LCD + Arduino: Ultrasonic Rangefinder Display

Slide01.jpg
Slide16.jpg

Hello World!

This is Unicorn Clockworks and here, we are going to make an Ultrasonic measuring tool with a display. This HC-SR04 ultrasonic emitter and receiver can measure anywhere from 2cm to 200cm or 1inch to 13 feet, while being accurate up to 3mm. Since it uses ultrasonic waves are a non-contact form of measuring distances, it is perfect for applications like home renovation for measuring the distance to the ceiling or to the wall without having to grab your measuring tape!

Like my previous posts, this is compatible with any microcontrollers with Arduino core support, without any modifications to the code or circuit.

Materials

Slide02.jpg

  • Arduino or any Arduino-compatible microcontrollers
  • HC-SR04 Ultrasonic Sensor
  • LCD display
  • Potentiometer
  • Jumper Wires
  • Breadboard

LCD Connections

Slide03.jpg
Slide04.jpg
Slide05.jpg
Slide06.jpg
Slide07.jpg
Slide08.jpg
Slide09.jpg
Slide10.jpg

I've written a complete generalized Instructable on this recently, so you can check this out: https://www.instructables.com/id/Temp-1/

Alternatively, read on and I still show you everything in detail here:


1a

  • Connect the Brown wire (pin 16) to the GND pin on the Arduino
  • Connect the Red wire (pin 15) to the 3.3V VCC pin on the Arduino
  • Connect the Orange, Yellow, Green, Blue (pins 14-11) to pins 2 to 5 on the Arduino

2

  • Connect the White wire (pin 1 on the LCD) to the common ground (pin 16)
  • Connect the Grey wire (pin 2 on the LCD) to the common VCC source (pin 15)
  • Connect the Purple wire to the signal pin of the potentiometer (pin 2 on the potentiometer)

3

  • Connect the Purple wire (pin 1 on the potentiometer) to the common ground (pin 16)
  • Connect the Grey wire (pin 3 on the potentiometer) to the common VCC source (pin 15)

4

  • Connect the Yellow wire (pin 4 on the LCD) to pin 12 on the Arduino
  • Connect the Black wire (pin 5 on the LCD) to the common ground (pin 1 on the potentiometer or pin 16 on the LCD, either works)
  • Connect the Green wire (pin 6 on the LCD) to pin 11 on the Arduino

Ultrasonic Connections

Slide11.jpg
Slide12.jpg
Slide13.jpg
Slide14.jpg
Screen Shot 2017-08-17 at 6.37.24 PM.png
5a
  • Connect the Red wire from the VCC pin of the ultrasonic sensor (pin 1) to any common VCC source (like pin 3 of the potentiometer or pin 2 of the LCD)
  • Connect the Black wire from the GND pin of the ultrasonic sensor (pin 4) to any common ground (like pin 16 of the LCD or pin 1 of the potentiometer)

5b

  • Connect the Blue wire from the trigger pin(send) of the ultrasonic sensor (pin 2) to pin 9 on the Arduino
  • Connect the Brown wire form the echo(receive) pin of the ultrasonic sensor (pin 3) to pin 10 on the Arduino

Code

Screen Shot 2017-08-17 at 12.35.11 PM.png
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;
void setup() {
lcd.begin(16,2); 
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
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.0133/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(100);}

It's Done!

Slide15.jpg
Slide17.jpg