Learn to code

ARDUINO & CODES

Previous Post
Detecting motion with PIR sensor
Next Post
Detecting light with an LDR Sensor

Detecting distance with HC-SR04 sensor

in Arduino Sensors

This tutorial will explain how you can measure distance with the HC-SR04 Sensor. In the upcoming text, we will explain how the sensor works and give example sketches for your Arduino Board. The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object. It will measure the distance between 2cm and 400cm. In contrast to an IR sensor, the HC-SR04 sensor is not affected by light. The sensor will use sonar to measure the time it takes for the sound to bounce off an object and return to the sensor. Before we go into the coding part let us explain a bit more about the usage of this sensor.

Introduction of the HC-SR04 sensor

The HC-SR04 Ultrasonic Sensor is an ultrasonic sensor that comes with a 4 pins names as VCC, Trigger, Echo and Ground. The sensor measures the distance of an object with sonar. 

Here’s what happens:

  1. The transmitter (trig pin) sends a high-frequency sound signal.
  2. When the signal reaches an object, it is reflected.
  3. The receiver part of the transmitter will receive the reflected signal (echo pin).
  4. We can calculate the distance of the object.

The time between the transmission and reception of the signal allows us to measure the distance between the object and the sensor. This is possible because we know the sound’s velocity in the air.

We can use the following formula to determine the distance of the object:

T = S / V

we can rewrite this formula for the distance to calculate.

S = T x V

Where S is the required distance, V is the speed of sound and T is the time the sound wave takes to come back after hitting the object. We need to divide the value by two since the wave will be double since it travels to an object and then bounces back. Dividing it by two gives the actual distance to an object.

S = ( T / 2 ) x 0.034 or S = T x 0.034 / 2

The speed of sound is 343 meters per second. We need to convert that to microseconds since that’s the value that we get from our sensor. 343 meters per second is the same as 1/29.1 cm per microsecond (0.034).

Let apply this to a sketch.

Hardware Required

  • Arduino Uno
  • Breadboard
  • Ultrasonic Sensor HC-SR04
  • hook-up wires

Circuit Diagram

HC SR04

Code Example HC-SR04

int triggerPin = 11;    // Trigger
int echoPin = 12;    // Echo
long duration;
int distance;

void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(triggerPin, 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(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // Convert the time into a distance
  distance = duration * 0.0343 / 2;

  Serial.print(distance);
  Serial.print("cm");
  Serial.println();

  delay(250);
}

Code explanation

Firstly, you create variables for the trigger and echo pin called triggerPin and echoPin. The triggerpin is connected to Digital Pin 11 and the echo pin is connected to digital pin 12.

int triggerPin = 11;    
int echoPin = 12;   

We will also create two more variables. One that holds the time between sending and receiving the signal and one to save the distance in cm.

long duration;
int distance;

In the setup(), we will 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.

//Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);

In the loop() part of our sketch we will trigger the sensor by sending a HIGH signal for 10 microseconds. However, to give a clean result we give a shirt LOW signal.

digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

After sending both HIGH and LOW signals we are ready to read the signal from the sensor. That is the duration in microseconds from sending the signal to the reception of its echo to an object.

pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

Finally, you will have to convert the duration to a distance. We can calculate the distance by using the formula described above.

distance = time x speed of sound / 2

// Convert the time into a distance
  distance = duration * 0.0343 / 2;

Remember, we have to divide the travel time by 2 because the signal was sent and returned back to the sensor. 

At last, we print our results to the Serial Monitor.

Serial.print(distance);
  Serial.print("cm");
  Serial.println();

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu