HC-SR04 Ultrasonic Arduino Guide
HC-SR04 Ultrasonic Arduino Guide
Description
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.
1. The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz).
2. The sound travels through the air. If it finds an object, it bounces back to the module.
3. 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:
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.
Ultrasonic Sensor
Arduino
HC-SR04
VCC 5V
Trig Pin 11
Echo Pin 12
GND GND
Code
Upload the following code to your Arduino IDE.
/*
* created by Rui Santos, https://randomnerdtutorials.com
*
* Complete Guide for Ultrasonic Sensor HC-SR04
*
Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) - Pin11
Echo: Echo (OUTPUT) - Pin 12
GND: GND
*/
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}
View raw code
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
Finally, we print the results in the Serial Monitor:
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
/*
* Posted on https://randomnerdtutorials.com
* created by http://playground.arduino.cc/Code/NewPing
*/
#include <NewPing.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 200
void setup() {
Serial.begin(9600);
}
void loop() {
delay(50);
unsigned int distance = sonar.ping_cm();
Serial.print(distance);
Serial.println("cm");
}
View raw code
#include <NewPing.h>
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.
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 200
Then, you create a NewPing instance called sonar:
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
In the setup(), you initialize the Serial communication at a baud rate of 9600.
Serial.begin(9600);
Finally, in the loop(), you just need to use the ping_cm() method on the sonar object to get the
distance in centimeters.
unsigned int distance = sonar.ping_cm();
If you want to get the distance in inches, you can use sonar.ping_in() instead.
Demonstration
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.