Categories
TUTORIALS

Detecting movement direction with two ultrasonic distance sensors

This tutorial shows how to detect movement direction in two dimensions using an Arduino Uno and two HC-SR04 ultrasonic distance sensors. I wrote my sketch to detect movement from left to right or from right to left, but it should be possible to adapt for other use cases, like detecting movement into or out of a room.

The setup relies on a very simple premise: By placing a pair of sensors in close proximity, we can infer the direction of movement from the order in which the two sensors detect it.

How it works

To understand what it means for a sensor to detect movement in this context, it might help to first understand how an ultrasonic distance sensor works. Very simply, a sensor like the HC-SR04 works similarly to a bat’s sonar: by sending out an ultrasonic pulse that bounces when it encounters a barrier, like a wall, a person, or an object. After it bounces, the pulse returns to the sensor, which computes the distance to the barrier based on the time it took for the pulse to return.

The HC-SR04 uses two specialized pins to accomplish this, in addition to its power and ground pins: a “trigger pin,” which sends out the signal, and an “echo pin,” which receives the returning signal after it bounces.

Two HC-SR04 ultrasonic distance sensors
Two HC-SR04 ultrasonic distance sensors

This distance detection happens continuously, whether anyone moves in front of the sensor or not, which means we need some additional logic to detect whether someone is passing by. In my code, I define the maximum distance that I care about detecting movement at; if a sensor’s distance measurement drops below that, I consider the sensor “triggered.” The right distance limit will depend on your application, but might correspond to the width of a walking path, the width of a doorway, or the size of a room.

When a sensor is triggered, the program records a timestamp in milliseconds since the code started running. Once both sensors have triggered, the program compares the timestamps for the two sensors to determine the order they triggered and output the direction of movement.

What you need

The list of components for a basic direction detector is fairly short:

  • Arduino Uno microcontroller, or equivalent (I used an ELEGOO board)
  • Two HC-SR04 ultrasonic distance sensors
  • Eight female-to-male Dupont wires
  • Two male-to-male jumper wires
  • Breadboard
  • Power connection (I used a USB cable connected to my laptop)

For a more permanent installation you’d probably want something more robust, but this worked well as a proof of concept.

A single HC-SR04 sensor can be connected directly to an Arduino Uno board with four female-to-male Dupont wires, with no other components required. With two sensors, I used a breadboard to allow both sensors to share 5V power and ground connections, as you’ll see in the schematic below.

Wiring schematic

The schematic shows how I wired everything up. Here, it’s important to make sure each sensor’s trigger pin and echo pin connects to the Arduino pins designated as trigger and echo in the code: the two are not interchangeable, and if you get the wires switched around, the sensors will not work!

You’ll also want to make sure you have an ironclad sense of which of the two sensors is 1/left and which is 2/right, and wire them accordingly. I used different wire colors to keep track, but you could also label them.

Dual ultrasonic sensor wiring schematic
Dual ultrasonic sensor wiring schematic, created with Fritzing

Setup

The picture below shows the real-life version. When testing, I tried to lay the sensors as flat as possible and in as close to a straight line as possible. To detect walking movement past the sensors rather than the movement of my hand above them, I’d want to position them vertically instead of horizontally.

Dual ultrasonic sensor breadboard setup
Dual ultrasonic sensor breadboard setup

Code

Finally, here’s the code to make it all work. I wrote at least three different versions of this program: one meant to use an array to store the two sensors that triggered last, one meant to be able to detect more than one movement in a specified period of time, and finally this one, which was the only one I was able to get to work reliably in the time available.

Because this is the simple version, there are edge cases it doesn’t account for, like if two people were to pass the sensors going opposite directions at the exact same time. I’ll want to keep experimenting with the code to try to account for more complex scenarios, but for a basic 2D movement detector, this worked pretty well!

I could almost certainly take out the last “else” statement in the direction block, because the only way it could trigger is if both sensors recorded identical timestamps down to the millisecond, which I don’t think should happen given the way the rest of the code is set up. It got left in as a vestige of some earlier debugging, but I may go back and update this post once I’ve had a chance to confirm that nothing unexpected happens if I take it out.

Demonstration

I wish I could’ve increased the font size on the serial monitor to show the result more clearly, but the output does correctly print “Left to right” and “Right to left” as I move my hand back and forth over the sensors.

Success! The sensors are correctly detecting the direction of movement

And there you have it! A minimalist movement direction detector using an Arduino and two ultrasonic distance sensors.

I’ll be using it as a building block for a motion-activated LED strip that displays different light patterns depending on the speed and direction of movement, but you might also use it for a light that turns on or off depending on whether you you enter or exit a room, and probably all sorts of other fun interactive projects I haven’t thought of.

Leave a comment if you found this tutorial useful, if you built anything based on it, or if you have ideas for how to improve it!

3 replies on “Detecting movement direction with two ultrasonic distance sensors”

Hi I have installed all the available library function in IDE but still there is no library function detected by my IDE

Hello,

I tested the code and this works fine.
Is there a way this also works with a TOF sensor like (VL53L1X)?
I am not so good in programming.

Geert

Leave a Reply

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