Introduction

The HC-SR04 Ping Distance Sensor

by Giffy Keeler (Kiettisack Internantional School. Vientiane, Laos)

This is an electronic sensor commonly used with Arduino microcontrollers as a way to measure distance using sound waves. You will see it used in low cost robots for obstacle detection and avoidance.


How does this sensor work?

  1. It uses sonar to determine the distance of an object. It does this by:
  2. Sending (transmitting) a signal. a high-frequency sound, through the "trig pin". You can't hear it because it is too high for your ears.
  3. When the signal (sound) hits an object, it bounces back (reflects) to the HC-SR04, and
  4. The transmitter "echo-pin" receives it.
If we measure the time it takes for the signal to return, we can determine the distance. Why? Because we know the speed of sound. The speed of sound in dry air at 20 °C = 343 meters/second. 
So we can use a simple equation to get the distance: distance = (time in seconds/2) * 343 meters
Why divide the seconds by 2? The sound must go and also return so it is traveling twice the actual distance.


As a school science project I have searched the internet and tried many builds and the code and most of the time it has resulted in failure. I will share with you one of the simplest uses of this sensor, including some easy code. Hopefully, this will mean success for you and happiness not frustration!

The Project

The challenge is to connect this sensor to an Arduino (I used the UNO). And have it measure the distance to a solid object. The distance can be seen in the Arduino IDE serial monitor and also in the serial plotter.

Here are the materials used:


  1. Breadboard(s)
  2. Wires
  3. Arduino Uno & cable
  4. HC-SR04
Here is the finished wiring:


Connections are:

From the HC_SR04

  1. Vcc - to 5v on UNO
  2. Trig - to digital pin 11 on UNO
  3. Echo - to digital pin 12 on UNO
  4. Gnd - to gnd on UNO

That's it!!!!

Make sure you load up the sketch to you Arduino. Here is the code:

/*
 * 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
 */

int trigPin = 11;    // Trigger
int echoPin = 12;    // Echo
long duration, cm, inches;

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);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // Convert the time into a distance
  cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(250);
}


After you upload the programming, you can check if it is working by starting the  serial monitor under the Tools menu in the Arduino IDE. Make sure the baud is set at 9600 because that is what is set in the above code. You will see it scrolling information with the distance the sensor is reading.



How accurate is it?
I will check with a ruler.

Reading Displayed = 32cm       Actual Distance = 32cm
Reading Displayed = 20cm       Actual Distance = 20cm
Reading Displayed = 10cm       Actual Distance = 10cm

Conclusion - Yes. it is accurate for measuring in cm.

All for now...bye!!!!!




Comments