HC-SR04 Ultrasonic Sensor with Arduino

Avoiding obstacles or knowing the distance to an object is an essential function for many Arduino based or other microprocessor projects. It gets especially important in robotic applications, as well as in detecting objects, being it humans or stationary objects, that may be in front of a device, or towards which the device is moving. An Ultrasonic sensor, such as the HC-SR04, is a cheap and reliable way to add distance measuring and obstacle detection to your project. In this tutorial, I will show you how to connect and use such an ultrasonic sensor, as well as a bonus feature which will build on my previous post, by adding an LCD to the project to display the distance to the detected object.

For today's tutorial, I will be using the Maker Nano, an Arduino Nano compatible board made by Cytron Technologies. This little board is pin-compatible with the original Arduino Nano, but it has a few extra features to make it easier for makers to create, and students to learn... It has a builtin programmable button on d2, a mutable buzzer, as well as led indicators on pins d2 to d12, as well as the usual led on d13. These make debugging a project easier as you have a visual indication of what a specific pin is doing.

It is perfectly okay if you want to use Arduino Uno for this. The code and connections are compatible. Let us get started by looking at the HC-SR04 Ultrasonic sensor...

The sensor has clearly labelled interface-pins on the front and the back, therefore it will be super-easy to connect it to our project. The pin descriptions is a follows:

Vcc -> Connect to +5v
Trig -> Trigger Pin, used to initiate a reading, pull this pin LOW, then High, and keep LOW
Echo -> Echo Pin, This pin will go HIGH when we receive an echo back from an object

Gnd -> Ground, connect this pin to 0v, or Ground

Start by placing the Maker Nano, or Arduino Nano onto a bread-board, or if you are using Arduino Uno, connect the 5v and ground lines to the breadboard as above.

The red wire is 5v and the blue wire is Gnd. The white wire at D12 goes to the "Echo" pin on the HC-SR04 and the grey wire on D11 goes to the "Trig" pin on the HC-SR04

Remember to add +5v wire (red) between the Vcc Pin on the HC-SR04 and the +5v bus, and a Gnd(blue) wire between the Gnd Pin on the HC-SR04 and the Gnd bus on the breadboard.

We will leave out the connection of the LCD for the moment. Let's focus on getting this setup working first.

Tip: Always divide your project into stages. Then focus on getting the current stage to work exactly as you want it to, before adding another stage. If you were to do everything at the same time, it is easy to get overwhelmed and confused, and in the end, spending a lot of time debugging your project to get it working ;)

Now we can start with the coding. Open your Arduino IDE. Click on Tools -> Board, and select the board that you are using. In my case, it will be Arduino Nano.

Type the following code into the Arduino IDE, and open the Serial Monitor, from the tools menu. Make sure that the baud rate is set to 115200, the same as in the code below.

/*

* Ultrasonic Sensor (HC-SR04) use with Arduino

* Pin Trig to pin 11

* Pin Echo to Pin 12

*

* I2C LCD connected with SCL on A5 and SDA on A4

*

* Maker and IOT Ideas

* https://www.patreon.com/makeriot2020

*

*

*/

long pulse_duration;

int distance;

int trigger_pin = 11;

int echo_pin = 12;

void setup() {

// put your setup code here, to run once:

pinMode(trigger_pin,OUTPUT);

pinMode(echo_pin,INPUT);

Serial.begin(115200);

}

void loop() {

get_distance();

Serial.print("Distance = : ");

Serial.print(distance);

Serial.println(" cm.");

delay(5000);

}

void get_distance() {

digitalWrite(trigger_pin,LOW);

delayMicroseconds(2);

digitalWrite(trigger_pin,HIGH);

delayMicroseconds(10);

digitalWrite(trigger_pin,LOW);

pulse_duration = pulseIn(echo_pin,HIGH);

distance = pulse_duration*0.034/2;

delay(50);

}

While leaving the serial monitor running, go back to the Arduino IDE, and click on the Upload icon. When the code has uploaded completely, click on the serial monitor, hold your hand or a book or other object in front of the ultrasonic sensor. The distance should change as you move the object closer or further away from the sensor.

This unit can sense obstacles up to about 6m away, but I have not seen it being very accurate beyond 3 or 4 meters.

Now we have completed part one of this tutorial. We can detect an object in front of the serial monitor, and print the distance on the serial monitor. Our project is however far from complete. We still have to implement the code for the LCD.

Connect the LCD to the Arduino, SDA to A4, and SCL to A5. Remember to connect Vcc to the +5v and Gnd to the Gnd bus on the Breadboard.

Change your code to the following:

/*

* Ultrasonic Sensor (HC-SR04) use with Arduino

* Pin Trig to pin 11

* Pin Echo to Pin 12

*

* I2C LCD connected with SCL on A5 and SDA on A4

*

* Maker and IOT Ideas

* https://www.patreon.com/makeriot2020

*

*

*/


#include <Wire.h>

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);


long pulse_duration;

int distance;

int trigger_pin = 11;

int echo_pin = 12;



void setup() {

// put your setup code here, to run once:

pinMode(trigger_pin,OUTPUT);

pinMode(echo_pin,INPUT);

lcd.begin(16,2);

lcd.backlight();

lcd.clear();

}


void loop() {

get_distance();

delay(500);

}


void get_distance() {

digitalWrite(trigger_pin,LOW);

delayMicroseconds(2);

digitalWrite(trigger_pin,HIGH);

delayMicroseconds(10);

digitalWrite(trigger_pin,LOW);

pulse_duration = pulseIn(echo_pin,HIGH);

distance = (pulse_duration*0.034/2);

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Object Detected: ");

lcd.setCursor(0,1);

lcd.print(distance);

lcd.setCursor(5,1);

lcd.print("cm");

pulse_duration = 0;

delay(50);

}

Upload it to the Arduino. You should see a similar result as in the picture below.


This concludes the tutorial on connecting an ultrasonic sensor to Arduino and displaying the results on an LCD.

Thank you