This is a Beginner's Guide to Arduino Ultrasonic Sensor usage - Measuring Distances with HC-SR04
Introduction
Arduino Ultrasonic sensors: The world of Arduino offers endless
possibilities for creative projects, and one of the most fascinating
tools at your disposal is the ultrasonic sensor. This amazing little
device allows you to measure distances accurately and reliably, opening
doors to a wide range of applications.
Whether you're building a robot that avoids obstacles, a parking
assistant for your car, or a water level monitor, the ultrasonic sensor
is your key to success.
Ultrasonic sensors are useful for proximity detection and measuring
distance. In this tutorial, you will look at using an ultrasonic sensor
with an Arduino to measure distance. You will connect an HC-SR04
ultrasonic sensor to an Arduino Uno and write code to trigger ultrasound
pulses and measure the echo return time to calculate distance.
The HC-SR04 ultrasonic sensor uses sonar to determine distance to the
nearest object by emitting high frequency sound pulses and listening for
the echo. By measuring the time between the pulse emission and echo
reception, you can calculate the distance using the speed of sound.
Ultrasonic sensors are commonly used in robotics, presence detection,
and measurement applications.
What is an HC-SR04 Ultrasonic Sensor?
The HC-SR04 is an ultrasonic range finder that uses sound waves to
measure distances. It emits a short burst of ultrasonic sound and then
listens for the echo to return. By measuring the time it takes for the
echo to return, the sensor can calculate the distance to the object it's
facing.
The HC-SR04 is a popular choice for Arduino projects due to its low
cost, ease of use, and high accuracy. It has a measuring range of 2cm to
400cm with a resolution of 0.3cm. It also operates at a frequency of
40kHz, making it safe for use around humans and pets.
Required Components
Arduino Uno
HC-SR04: Arduino Ultrasonic Sensor
Breadboard
Jumper wires
Arduino Ultrasonic Sensor: Circuit Diagram
The circuit diagram shows how to connect the HC-SR04 sensor to the Arduino Uno.
Connect the VCC pin of the HC-SR04 to the 5V pin of the Arduino.
Connect the GND pin of the HC-SR04 to the GND pin of the Arduino.
Connect the Trig pin of the HC-SR04 to digital pin 13 of the Arduino.
Connect the Echo pin of the HC-SR04 to digital pin 12 of the Arduino.
Diagram using Fritzing
Arduino Ultrasonic Sensor: Schematic
Diagram using Fritzing
Libraries needed
The library you need to install is "NewPing".
Installing an Arduino Library with IDE
Install the library as follows:
1st method:
Click the menu bar and follow the menu items:
Sketch-->Include Library-->Manage
Libraries...
2nd method:
Click the ICON on the left that looks like a set of books.
The library manager appears on the left of the screen.
In the search box type NewPing. Now click the install button.
Arduino Ultrasonic Sensor: Example Sketch
Here's an example sketch to get you started with the HC-SR04 sensor:
You can copy and paste the code below into the Arduino IDE (in a new
sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).
#include<NewPing.h>
#defineTRIG_PIN13
#defineECHO_PIN12
#defineMAX_DISTANCE200
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
voidsetup(){
Serial.begin(9600);
}
voidloop(){
delay(50);
int distance = sonar.ping_cm();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
}
Short Code Explanation
The code includes the NewPing library for working with the HC-SR04 sensor.
The TRIG_PIN and ECHO_PIN constants define the digital pins connected to the sensor.
The MAX_DISTANCE constant sets the maximum distance the sensor can measure.
The sonar object is created with the NewPing library.
In the loop function, the ping_cm() function is called to measure the distance.
The measured distance is printed to the serial monitor.
Detailed Code Explanation
The #include line includes the NewPing library, which provides functions for working with the HC-SR04 sensor.
The #define TRIG_PIN 9 and #define ECHO_PIN 10 lines define the digital pins connected to the sensor's Trig and Echo pins, respectively.
The #define MAX_DISTANCE 200 line sets the maximum distance the sensor can measure to 200cm.
The NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); line creates a NewPing object named sonar with the specified trigger pin, echo pin, and maximum distance.
The void setup() function initializes the serial communication with the computer.
The void loop() function is the main loop of the program. It runs continuously.
The delay(50); line delays the program for 50 milliseconds to prevent the sensor from being overwhelmed with requests.
The int distance = sonar.ping_cm(); line calls the ping_cm() function of the sonar object to measure the distance in centimeters.
The Serial.print("Distance: "); line prints the text "Distance: " to the serial monitor.
The Serial.print(distance); line prints the measured distance to the serial monitor.
The Serial.println("cm"); line prints the text "cm" to the serial monitor, indicating the unit of measurement.
Arduino Ultrasonic Sensor: Uploading the Code
Connect the Arduino Uno to the PC with a USB cable.
Select the Arduino Uno hardware from the Tools > Board menu.
Open a new sketch.
Paste the code above into the new page (overwrite everything).
Press the upload button (right arrow at top).
You can find a more detailed tutorial on the Arduino IDE page.
Testing the Circuit
Once you've uploaded the code, open the serial monitor (Tools >
Serial Monitor) and you should see the distance readings being printed
every 50 milliseconds. You can test the sensor by placing your hand or
an object in front of it and observing the changes in the distance
readings.
Conclusions
Congratulations! You've now successfully built and tested an
ultrasonic sensor circuit with Arduino. This is just the beginning of
your journey into the world of distance measurement. With the HC-SR04
sensor, you can create countless projects that will amaze and inspire
you.
If you want to go a bit more in-depth on ultrasonic sensors you can find, on this page,
a project that uses transistor amplifiers to get the result from "raw"
sensors. This uses a smoothing circuit and comparator to get the final
echo return result. It shows essentially what the HC SR04 board
has to do i.e. its circuit operation.
Written by John Main who has a degree in Electronic Engineering.
Note: Parts of this page were written using claude-instant
as a research assistant.
How to get accurate DHT22 digital humidity sensor readings with an Arduino. Did you know it also measures temperature as Well? Find out why, in this page...
A PIR sensor lets your Arduino sense movement without contact. This tutorial covers PIR sensor basics, connecting one to an Arduino board and coding a motion detector.
Arduino Hall Effect Sensor: Add magnetic sensing superpowers to your Arduino projects with an easy-to-use hall effect sensor. With full code and layout...
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.