Explore the power of the HC-SR04 ultrasonic sensor with NewPing library for accurate distance measurements using Arduino Uno.
The ultrasonic sensor (HC-SR04) is the most popular and widely used sensor module for distance measurement and object detection. It is a cost-effective sensor that offers reliable performance and ease of use. Ultrasonic sensors work by emitting high-frequency sound waves and measuring the time it takes for the waves to travel to an object and return as echoes. For further insights on an ultrasonic sensor, check out my previous blog post on Ultrasonic sensor with Arduino uno, where I discussed all fundamental aspects related to ultrasonic sensors.
There are multiple libraries available in the Arduino ecosystem such as NewPing, Grove, UltrasonicRanging, HCSR04, Ultrasonic, etc. Each library may have different features, compatibility with sensor models, and implementation methods. Here I will consider only the NewPing library to code the ultrasonic sensor (HC-SR04) by Arduino Uno.
What will you learn from this article?
Now, In this article, I am going to explain two basic examples that will help you understand how to code an ultrasonic sensor (HC-SR04) using the NewPing library.
In the first example, you will learn how a NewPing library is used to detect the distance from the object, using an ultrasonic sensor and display them on a serial monitor. In the second example, I will explain how a NewPing library is used with an LCD screen to display the calculated distance by ultrasonic sensor.
Example 1: Ultrasonic sensor with NewPIng library basics
This fundamental example will help you to understand the following things:
- How to connect the ultrasonic sensor with Arduino Uno
- How to code an ultrasonic sensor using the NewPIng library
- How to display the distance of the object on a serial monitor in centimeters and inches
Required components
- Arduino uno board: BUY ON AMAZON
- Few jumper wires: BUY ON AMAZON
- Ultrasonic sensor (HC-SR04): BUY ON AMAZON
- 1 x Breadboard: BUY ON AMAZON
Circuit Diagram
According to the connection diagram, the HC-SR04 has four terminals such as VCC, TRIG, ECHO, and GND. The VCC terminal is connected to the 5-volt power supply of the Arduino uno board. The TRIG terminal is connected to the analog pin 3 and the ECHO terminal is connected to the digital pin 8 of the Arduino uno board. The last GND terminal is connected to the respective GND terminal of the Arduino uno board.
Code
There are multiple libraries available in the Arduino ecosystem that can be used to code ultrasonic sensors. The NewPing library is a popular choice for working with ultrasonic sensors, including the HC-SR04.
The NewPing library is designed to provide an improved and efficient solution for working with ultrasonic sensor (HC-SR04). It offers enhanced timing techniques, multiple sensor support, and simplified distance measurement functions.
It provides precise timing for distance measurements, resulting in improved accuracy and stability of the readings.
The NewPing library offers a user-friendly and simplified interface for working with ultrasonic sensors. It provides straightforward functions for distance measurements, allowing developers to easily retrieve accurate distance values without the need for complex calculations or code.
//Ultrasound sensor basics with NewPing version
//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
//For more details please visit:
//For more projects please visit://www.arduinounomagic.com
#define TRIG_PIN 3 //connect trigger pin to pin 3 of Arduino uno
#define ECHO_PIN 8 //connect echo pin to pin 8 of Arduino uno
#define MAX_DISTANCE_CM 300 //maximum distance up to which need to measure in cm
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE_CM);//setup pins for NewPing
void setup()
{
Serial.begin(115200);//assign serial monitor at 115200 baud
}
void loop()
{
delay(50);//wait for 50 milliseconds
Serial.print("Distance in cm:"); //print Distance on serial monitor
Serial.print(sonar.ping_cm());//send a ping, returns the distance in cm or zero if echo ping is not under the set distance range (300)
Serial.println(" cm");
Serial.print("Distance in inch:");//print Distance on serial monitor
Serial.print(sonar.ping_in());//send a ping, returns the distance in inch
Serial.print(" inch");
Serial.println();
}
Output
Check out the video, it will help you to understand the working of the code.
Example 2: Ultrasonic sensor with NewPIng library using LCD screen
This example will help you to understand the following parameters:
- How to use the NewPing library for HC-SR04 ultrasonic sensor
- How to code the NewPIng library with LCD screen
- How to Display output on LCD screen in both centimeters and inches
If you don’t know how to use LCD screen with Arduino Uno and want to learn more about LCD screen, check out my separate article for it (How to use LCD with Arduino Uno). I explained each and every small aspect of the LCD screen in that separate tutorial.
Require components
- Ultrasonic sensor (HC-SR04): BUY ON AMAZON
- 1 x Arduino Uno board: BUY ON AMAZON
- 1 x 10k preset: BUY ON AMAZON
- LCD screen: BUY ON AMAZON
- 1 x breadboard: BUY ON AMAZON
- Few jumper wires: BUY ON AMAZON
- 220-ohm resistor: BUY ON AMAZON
Circuit Diagram
According to the circuit connection diagram:
- The ultrasonic sensor’s VCC pin and the VDD pin of the LCD screen are both connected to the 5-volt power supply of the Arduino uno board.
- The LCD screen’s RW, GND, and LED- pins are connected to the GND pin of the Arduino Uno board respectively.
- The LCD screen’s RS and E pins are connected to pin numbers 8 and 2 of the Arduino Uno board.
- The data pins D4, D5, D6, and D7 of the LCD screen are connected to pin numbers 7, 6, 5, and 4 of the Arduino uno board respectively.
- The 220-ohm resistor’s one terminal is connected to the LED+ pin of the LCD screen and the other terminal is connected to the 5-volt power supply of the Arduino uno board. This 220-ohm resistor works as a current-limiting circuit and helps to control the high current flow through the backlight LED of the LCD screen.
- The preset is a three-terminal device. Its first terminal is connected to the 5volt power supply of the Arduino uno board, the middle terminal is connected to the VEE of the LCD screen, and the third terminal is connected to the GND terminal of the Arduino uno board.
Code
Check out, my previous article regarding the ultrasonic sensor, In which I manually set off the ultrasonic sensor and measured the pulse duration of the received signal. After that according to those results calculated the distance in centimeters and inches. Now here in this article, I am doing all these mathematical calculations with just a single line of code using the NewPing library.
Hence, the NewPing library simplifies the coding process, enhances accuracy, and provides additional features, making it a valuable tool for working with ultrasonic sensors in Arduino projects.
//Ultrasound sensor using LCD with NewPing version
//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
//For more details please visit:
//For more projects please visit://www.arduinounomagic.com
#include<LiquidCrystal.h>
#include<NewPing.h>
LiquidCrystal ARDUINO_LCD(8, 2, 7, 6, 5, 4);// Set RS pin=8, Enable pin=2, D4=7, D5=6, D6=5, d7=4.
#define TRIG_PIN 3 //connect trigger pin to pin 3 of Arduino uno
#define ECHO_PIN 12 //connect echo pin to pin 12 of Arduino uno
#define MAX_DISTANCE_CM 300 //maximum distance up to which need to measure in cm
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE_CM);//setup pins for NewPing
void setup()
{
ARDUINO_LCD.begin(16, 2);//assign serial monitor at 115200 baud
pinMode(3, OUTPUT); //Configure trig_pin 3 as output pin
pinMode(12, INPUT); //Configure echo_pin 12 as input pin
ARDUINO_LCD.clear(); //Clear screen
}
void loop()
{
delay(50);//wait for 50 miliseconds
ARDUINO_LCD.clear(); //Clear screen
ARDUINO_LCD.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
//print distance in cm
ARDUINO_LCD.print("Distance:"); //print Distance on serial monitor
ARDUINO_LCD.print(sonar.ping_cm());//send a ping, returns the distance in cm or zero if echo ping is not under the set distance range (300)
ARDUINO_LCD.println(" cm ");
ARDUINO_LCD.setCursor(0,1);// Sets the location at which subsequent text written to the LCD will be displayed
//print distance in inch
ARDUINO_LCD.print("Distance:");
ARDUINO_LCD.print(sonar.ping_in());//send a ping, returns the distance in inch
ARDUINO_LCD.print(" inch");
delay(1000);
}
Output
Check out the video, it will help you to understand the working of the code.
Hope you like the article.
if you have any queries, please let me know in the comments.