Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino
- Where to buy?
- Arduino with HC-SR04 Sensor
- Arduino with HC-SR04 Sensor Wiring
- Source code with NewPing Library
- Wrapping Up
This article is a guide about the Ultrasonic Sensor HC-SR04. We’ll explain how it works, show you some of its features and share an Arduino project example you can follow to integrate into your projects. We provide a schematic diagram on how to wire the ultrasonic sensor and an example sketch with the Arduino.
The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object. This sensor reads from 2cm to 400cm (0.8inch to 157inch) with an accuracy of 0.3cm (0.1inches), which is good for most hobbyist projects. In addition, this particular module comes with ultrasonic transmitter and receiver modules.
The following picture shows the HC-SR04 ultrasonic sensor.
The next picture shows the other side of the sensor.
Here’s a list of some of the HC-SR04 ultrasonic sensor features and specs—for more information, you should consult the sensor’s datasheet:
- Power Supply :+5V DC
- Quiescent Current : 2mA
- Working Current: 15mA
- Effectual Angle: 15°
- Ranging Distance : 2cm – 400 cm/1″ – 13ft
- Resolution : 0.3 cm
- Measuring Angle: 30 degree
- Trigger Input Pulse width: 10uS TTL pulse
- Echo Output Signal: TTL pulse proportional to the distance range
- Dimension: 45mm x 20mm x 15mm
How Does it Work?
The ultrasonic sensor uses sonar to determine the distance to an object. Here’s what happens:
- The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz).
- The sound travels through the air. If it finds an object, it bounces back to the module.
- The ultrasound receiver (echo pin) receives the reflected sound (echo).
The time between the transmission and reception of the signal allows us to calculate the distance to an object. This is possible because we know the sound’s velocity in the air. Here’s the formula:
- speed of sound in the air at 20ºC (68ºF) = 343m/s
HC-SR04 Ultrasonic Sensor Pinout
Here’s the pinout of the HC-SR04 Ultrasonic Sensor.
|VCC||Powers the sensor (5V)|
|Trig||Trigger Input Pin|
|Echo||Echo Output Pin|
Where to buy?
You can check the Ultrasonic Sensor HC-SR04 sensor on Maker Advisor to find the best price:
Arduino with HC-SR04 Sensor
This sensor is very popular among Arduino tinkerers. So, here we provide an example of how to use the HC-SR04 ultrasonic sensor with the Arduino. In this project, the ultrasonic sensor reads and writes the distance to an object in the serial monitor.
The goal of this project is to help you understand how this sensor works. Then, you should be able to use this example in your own projects.
Here’s a list of the parts required to follow the next tutorial:
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Arduino with HC-SR04 Sensor Wiring
Follow the next schematic diagram to wire the HC-SR04 ultrasonic sensor to the Arduino.
The following table shows the connections you need to make:
|Ultrasonic Sensor HC-SR04||Arduino|
Upload the following code to your Arduino IDE.
How the Code Works
First, you create variables for the trigger and echo pin called trigPin and echoPin , respectively. The trigger pin is connected to digital Pin 11 , and the echo pin is connected to Pin 12 :
You also create three variables of type long: duration and inches . The duration variable saves the time between the emission and reception of the signal. The cm variable will save the distance in centimeters, and the inches variable will save the distance in inches.
In the setup() , initialize the serial port at a baud rate of 9600, and set the trigger pin as an OUTPUT and the echo pin as an INPUT .
In the loop() , trigger the sensor by sending a HIGH pulse of 10 microseconds. But, before that, give a short LOW pulse to ensure you’ll get a clean HIGH pulse:
We use the pulseIn() function to get the sound wave travel time:
The pulseIn() function reads a HIGH or a LOW pulse on a pin. It accepts as arguments the pin and the state of the pulse (either HIGH or LOW). It returns the length of the pulse in microseconds. The pulse length corresponds to the time it took to travel to the object plus the time traveled on the way back.
Then, we calculate the distance to an object, taking into account the sound speed.
We need to divide the travel time by 2 because we have to consider that the wave was sent, hit the object, and then returned to the sensor.
Finally, we print the results in the Serial Monitor:
Source code with NewPing Library
You can also use the NewPing library. Download the library here.
After installing the NewPing library, you can upload the code provided below.
How the Code Works
Getting the distance to an object using the NewPing library is much simpler.
You start by including the NewPing library:
Then, define the trigger and echo pin. The trigger pin is connected to the Arduino digital Pin 11 and the echo to Pin 12 . You also need to define the MAX_DISTANCE variable to be able to use the library.
Then, you create a NewPing instance called sonar :
In the setup() , you initialize the Serial communication at a baud rate of 9600.
Finally, in the loop() , you just need to use the ping_cm() method on the sonar object to get the distance in centimeters.
If you want to get the distance in inches, you can use sonar.ping_in() instead.
Upload the code to your Arduino board. Then, open the Serial Monitor at a baud rate of 115200.
The distance to the nearest object is printed in the Serial Monitor window.
In this post, we’ve shown you how the HC-SR04 ultrasonic sensor works and how you can use it with the Arduino board. For a project example, you can build a Parking Sensor with LEDs and a buzzer.
If you are a beginner to the Arduino, we recommend following our Arduino Mini-Course that will help you get started quickly with this amazing board.