Introduction: Arduino HC-SR04 (with LCD)

About: Hello, My name is Samuel, I am a student. I love micro controllers like arduino, they are my favorite interest. I also do geocaching, a worldwide treasure hunt game. I hope you enjoy my instructables! Samuel

Hello,

In this tutorial I am going to show you how to 1. Connect the HC-SR04 to an arduino and print the distance to the serial monitor, 2. Connect an LCD and print the results there instead. The sensor works by pinging out an ultrasonic sound we cannot hear. It then starts a timer and measures how long it takes to get that ping back. And that's the HC-Sr04 in a nutshell.

Now I guess we will go right ahead and get started. Remember, first, let's go through the parts we will use.

Step 1: Parts

In this project we will use the following parts:


Arduino Mega (10-15$), This cost can of course be displaced over future projects.

An HC-SR04 (1 $), I bought this for one dollar and there are a lot of ones that cost a lot more. I bought mine from aliexpress, please make a comment if you would like the link to it.

Some jumper cables (I bought mine for 2-3$ (60 pcs) )

And of course, if you are going to utilize an LCD:


A LCD screen (1-4$), depending on whether you get a shield or a standalone one. I use a 16 x 2 alphanumeric one.

Now that you know the parts that we are going to use, let's connect it up.

Step 2: Connecting (Only HC-SR04)

The way I am going to do this is first only wire up the HC-SR04. Then in another step I will add to the project with the LCD and add a little to the code.

Connections are:


Vcc -> Arduino 5V

Echo -> Any digital pin, I used 7.

Trig (Trigger) -> Any digital pin, I used 6.

GNC -> GND

Step 3: Code (without LCD)

I used the NewPing library, which makes your life much easier with this sensor. Download the library files here and see documentation here. This library does not only work with the SR04 but also these ones: SRF05, SRF06, DYP-ME007 & Parallax PING)))™.

The code I used:


#include <NewPing.h>

#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Creating the NewPing Object.

void setup() { Serial.begin(115200); // Begin serial com. at 115200 baud rate. }

void loop() { delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println("cm"); //If you would like ping in inches, remove "US_ROUNDTRIP_CM" and the backslash, don't forget to rename "cm" to "inches" }

Step 4: Connections With LCD

Instead of using a potentiometer to control the contrast, I will be connecting pin 3 of LCD screen to a PWN pin on arduino, in this case pin two of the arduino mega. I am using the arduino mega for all connections.

Connections: (LCD -> ARDUINO)

Pin 1 -> GND

Pin 2 -> VCC

Pin 3 -> Arduino pin 2

Pin 4 -> Arduino pin 33

Pin 5 -> GND

Pin 6 -> Arduino pin 31

Pin 7 - 10 -> NONE

Pin 11 -> Arduino pin 22

Pin 12 -> Arduino pin 24

Pin 13 -> Arduino pin 26

Pin 14 -> Arduino pin 28

Pin 15 -> VCC through 220 OHM resistor.

Pin 16 -> GND

Step 5: LCD Code

This is the code for the LCD to work, the values are displayed in both centimeters and inches. Enjoy!

#include <LiquidCrystal.h> //Importing libraries
#include <NewPing.h>

LiquidCrystal lcd(33,31,22,24,26,28); //Creating object

#define ECHO_PIN 7 //Defining pins #define TRIG_PIN 6 #define MAX_DIST 300

int contrast = 100; //Defining contrast

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DIST); //Defining newping object (The HC-SR04)

void setup() { //Setup lcd.begin(16,2); //Change this to the size of your LCD, my happens to be 16x2 lcd.clear(); //Clear screen pinMode(2, OUTPUT); //Setting contrast pin 2 to an output analogWrite(2, contrast); //Setting contrast }

void loop() { unsigned int uS1 = sonar.ping_in(); //Ping in inches unsigned int uS2 = sonar.ping_cm(); // Ping in cm lcd.clear(); //Clearing screen lcd.setCursor(2,0); //Setting cursor lcd.print("Ping: "); //Printing text lcd.print(uS2); lcd.print(" cm"); lcd.setCursor(2,1); lcd.print("Ping: "); lcd.print(uS1); lcd.print(" in"); lcd.setCursor(1,0); delay(1000); //Change this to the update time you want, I just used 1000 ms }

Step 6: Done, Finished

That's pretty much it. If you liked the tutorial and it helped you, please make it your favorite, it would mean a lot. Thanks for reading and I hope that you found it helping.

Samuel