In this tutorial, it is shown how to use the ultrasonic sensor (and actuator) “HC-SR04” with the Arduino Uno. The HC-SR04 has an ultrasonic transmitter and receiver. The transmitter sends out a 40 kHz burst signal (which is not audible by humans). The receiver captures the signal if it is reflected by an obstacle. Therefore, the HC-SR04 can be used for distance measurements and alarm detection systems. The HC-SR04 supports 50 measurements per second and distances between 2cm and about 300cm.
List of materials:
How to connect the HC-SR04 to the Arduino?
The HC-SR04 module has four pins. The module’s Vcc pin has to be connected with the 5V pin of the Arduino. In this tutorial, the module’s “Trig” pin (trigger) is connected to the Arduino digital pin 2 and the “Echo” pin is connected to Arduino’s digital pin 3. Lastly, the module’s “Gnd” pin is connected to one of the Arduino’s GND pins. Here, a mini breadboard is also utilized so that the HC-SR04 can be connected to the Arduino in an upright position.
How to program distance measurements with the HC-SR04?
The ultrasonic module is used to measure the distance between the module and an obstacle. In order to measure the distance, the module must be prepared to send out the 40 kHz burst signal. The burst signal is sent when the signal of the “Trig” pin goes from HIGH to LOW. In addition, the HIGH signal must have been present for at least 10 microseconds. The module sends a pulse on the echo pin with the duration of the round-trip-time of the burst signal. The round-trip-time is the time from the transmitter to an obstacle and from the obstacle to the receiver. Fortunately, the pulseIn-function covers this use-case perfectly. If the second parameter is HIGH, the pulseIn-function waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW.
In order to obtain the distance, the duration has to be divided by two, since only the one-way distance is of interested (from module to obstacle). Next, the acoustic velocity of the air has to be taken into account which is about 343.5 meters per second (only at a temperature of 20°C). Since the duration measured by the pulseIn-function is in microseconds and we are interested in cm, the divided duration has to be multiplied by 0.03435.
// (c) Michael Schoeffler 2016, http://www.mschoeffler.de const int pinTrigger = 2; // pin for sending out the signal const int pinEcho = 3; // pin for receiving the echo of the signal long duration = 0; // the roundtrip duration of the signal in us void setup() { pinMode(pinTrigger, OUTPUT); pinMode(pinEcho, INPUT); Serial.begin(9600); // open serial connection to print out distance values } void loop() { digitalWrite(pinTrigger, LOW); // turn off the trigger delayMicroseconds(3); noInterrupts(); // disable interrupts as they might interfere with the measurement digitalWrite(pinTrigger, HIGH);// prepare to send "trigger" command to module delayMicroseconds(10); // wait for 10us (module sends signal only, if trigger had a HIGH signal for at least 10 us) digitalWrite(pinTrigger, LOW); // module sends signal now duration = pulseIn(pinEcho, HIGH); // waiting for a HIGH signal on the echo pin. interrupts(); // enable interrupts, we are done with the measurement // from roundtrip duration to distance: long durationOneWay = duration / 2; // divided by two, since duration is a roundtrip signal // acoustic velocity of air at a temperature of 20°C => ~343.5 m/s // => 0.03435 cm/us long distance = durationOneWay * 0.03435; // distance in cm Serial.print("Distance[cm]: "); Serial.println(distance); delay(500); // wait for 500ms (added only for debugging reasons) }
If the code is uploaded to an Arduino, the output of the serial monitor should show the distance between the module and an obstacle. Since many factors, such as temperature, the obstacle’s material and surface characteristics, have an influence on the accuracy of the measurements, it is very challenging to achieve highly accurate measurements with such a low-cost ultrasonic sensor. Nevertheless, the HC-SR04 is a cheap and easy-to-use ultrasonic module with a wide range of possible applications.
Video Tutorial
[…] the distance to the right side. I won’t cover the wiring in detail since I already wrote a tutorial that shows how to connect an HC-SR04 to an Arduino. In addition, I wire an SD card read/writer to the Arduino. Again, I won’t go into details […]
Hello and thank you for this beautiful tutorial, I realized your editing with your code and I have a problem of measurement. I often have 0 values. Do you have a solution to solve this problem?
Thanks
Once i activate the sensor by triggering the trigger pin, how can i make it to stop. Like i guess if you send back to level HIGH the trigger pin once you made it low-high-low, you can make the wave no to come out off the module thus no being able of measure distance.. i tryed something like that, but no successfully…