Arduino HC-SR04 (with LCD)




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


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.


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 =; // 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.




  • Tiny Home Contest

    Tiny Home Contest
  • Water Contest

    Water Contest
  • Fix It! Contest

    Fix It! Contest

17 Discussions

Thanks for the post. In order for this code to work do I need to use a Mega or will it work with an Uno? Thanks in advance!


1 year ago

i'm planning to use a 128x64 lcd instead of 16x2. how am i going to code it? please help. thank you.

That was a great tutorial. Have tried other build tutorials for this sensor, but yours was the best of all. You made this all understandable and the hookup was finished in minutes. It was success at the first try. Thank you very, very much for an outstanding tutorial.

it is done there is a error calledC:\Users\sindr\AppData\Local\Temp\arduino_80ea863bec0f98cd4a4b1afe0793010e\sketch_jan12a.ino:3:21: fatal error: NewPing.h: No such file or directory

#include <NewPing.h>


compilation terminated.

exit status 1

Error compiling.

1 reply

Download the new ping library from and oen arduino app and click sketch and select include library and click include from .zip and select the downloaded file

Where can I find this code which is meant for Arduino Uno R3...?

i wanna attach a buzzer to it can i??

I have a doubt, what if I have an array of obstacles in front of me at different positions at different distances. whose distance will it show?

3 replies

Hello ChinuKB,

These distance sensors pick up items and reflect the sund forward plus minus 25 degrees. So basically that means if you put an item 10 cm in front of it 25 degrees left, it would reflect that sound and it will not reflect the sound of an item the stands 100 cm in fronton the item, as that takes a longer time for the sound to get back. Hope this makes sense.


I tried to do the same thing with 2 sensors. The second sensor is showing erratic readings. Please help. Thanks!

If you were to add another sensor, you would have to choose one new pin for the trig pin on the sensor and one for the echo one. You would have to declare another object in the code and get the value from that new object you created :) Have a great day!

Sam! This is fantastic!! I'm going to use this at my shop on my CNC's!! Thank you!

1 reply

Hello Gavinmarkoqitz!

Thanks for your great feedback, it means alot. Cool idea to use it in your shop! Hope it works great!


Great instructable! Where'd you get the ultrasonic sensor for $1? That's a great deal!

1 reply


Thanks a lot for your feedback! It means a lot and keep me going with creating these :) I got my sensor from a site named They have super cheap prices on many arduino sensors and I found that all work great. Click below for a redirect to the sensor page. It was actually 0,94$ free shipping.

Ultrasonic sensor


I'm curious. What exactly did you make and what does it do? None of that made any sense to me.

1 reply


I am sorry that you did not exactly understand. Basically, I measure distance using an ultrasonic sensor. It pings out a sound and measures the time until it gets that sound back. Humans cannot hear the sound that the sensor is pinging out, therefore it is great for measuring distances. Hope this helps,