Detecting moving objects with HC-SR04 ultrasonic sensor

Using HC-SR04 sensor to make a movement detector
September 1, 2022 by
Detecting moving objects with HC-SR04 ultrasonic sensor
Boot & Work Corp. S.L., Bernat Brunet Pedra

Introduction

Ultrasonic sensors are used to measure distances from objects by emitting waves and knowing how many time has passed between transmitting and receiving the wave. In this blog you will learn how to make a movement detector using this sensor.

Requirements

Building the detector

The first thing you need to do is to connect the sensor to the PLC, just like seen in the table below. As there are 4 pins in the HC-SR04, 4 wires will be required. 

HC-SR04M-Duino PLC
Vcc5V
GNDGND
TrigPin 3
EchoPin 2

Make sure the switches behind the connections allow access to Pin 2 and Pin 3.

After connecting the sensor, you are now ready to load the program into the PLC. The code to upload is the following:

#define threshold 200
bool cooldown = false;
const int E = 2;
const int T = 3;

void setup() {
    Serial.begin(9600);
    pinMode(T, OUTPUT);
    pinMode(E, INPUT);
    digitalWrite(2, LOW);
}

void loop() {
    long t;
    long d;
    digitalWrite(T, HIGH);
    delayMicroseconds(10);
    digitalWrite(T, LOW);
    t = pulseIn(E, HIGH);
    d = t / 59;

    if (d > threshold ) {
        cooldown = false;
        digitalWrite(Q0_0, LOW);
    }

    else if (d <= threshold and !cooldown) {
        Serial.print("Someone passed at "); Serial.print(d); Serial.println(" cm");
        digitalWrite(Q0_0, HIGH);
        cooldown = true;
    }
    delay(60);
}

The first lines are to define where the Trigger and Echo pins are connected (Pins 2 and 3). The threshold value is the limit distance which the sensor will detect movement. If anything moves in a distance below the value, the detector will notice the movement. Afterwards, we initialize the serial port and the pin modes for the pins of the sensor.

In the loop function, a 10 microseconds pulse is sent from the Trigger pin and with the pulseIn() function, the Echo pin knows exactly how much time has passed since the pulse was emitted. With the time value, time can then be converted to distance using the v= d / t formula, using v as 340 m / s (velocity of sound in the air). By converting it to cm / us, we get that the distance from the target object is equal to the time calculated with the pulseIn() function divided by 29,5. As the time received by the Echo pin is the time it takes the wave to go and return, it is needed to divide the result by 2. The final formula is the following:

d (cm) = t (us) / (2*29,5) = t (us) / 59

After knowing the distance from the object, an if statement decides whether the distance is lower than the threshold value. If so, a message will show and a LED will light. To prevent multiple messages in case the object stays in front of the sensor, a cool down variable has been introduced in the code, and will only allow new messages after the object that triggered the sensor has passed by.

​Search in our Blog

Detecting moving objects with HC-SR04 ultrasonic sensor
Boot & Work Corp. S.L., Bernat Brunet Pedra September 1, 2022
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>