Two HC-SR04, two leds

Hi guys, I need to set up this project, a very simple thing I guess, but I'm kinda lost...
It'll have two leds and two SR04.
One of the SR04 activates one led, and the other SR04 activates the other led. It mustn't have any lag or inertia, it must be instantaneous on and off, but in a retriggered behavior of sort, so the led has to be on as soon as something is within the SR04 set range (let's say 2 mts).
They won't relate each other at all, will be completely independent from each other.
The SR04 won't "see" each other either, I mean they won't be physically within their mutual reach, so no interference will occur.
I have Arduino UNO for this.
Would you please help me with this set up and code?
I'd deeply appreciate your help.
Many thanks in advance!!

How will they be connected? There is a limit to how long you can make the wires between the Arduino and the SR04.

Oh, I guess I should ask, have you tried one LED and one SR04, so far?

Look up the NewPing library, it has some good examples that may help you.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:


Hello @carol_84 !

What have you done so far? Did you already started your code or the sketch of your circuit?

If the answer of these questions is "no", then I suggest you as a first step trying to learn some more about HC-SR04 and write a program to read correctly one of them. After that we go for the LED and then the twin circuit.

Hi guys, Thanks for your answers!
Well the SR04 will be connected with quite short cables to the UNO board, but they will be separate each other by a little wall of absorbing material, and will be 180 degrees from each other, that's why they won't "see" each other.

You're welcome. You still have a few to give us.

...but they may hear late returns

I'm absolutely newbie with this Arduino world, so I'm sorry for this.
I've been wroking in automation for so long, but with direct sensors under purely analog solutions.
Moreover, i could do this with the SR04 directly without Arduino, but I wanted to get into this world, just to see how it works.
I'm so astonished that something that simple as this, might become so complicated under this platform.
I imagined it would be a four lines code and just plugging the sensors and leds to the board.
But taking a look at some similar projects, I can see there is a remarkable complexity for so simple projects.
Anyways, Thanks again guys, I'll keep looking forward, and find the simplest way to solve this.

In fact, that sensor is so simple that a lot of sketches handle it with a few lines of inline code. The core functions like digitalWrite make it possible to handle an LED in one line of code. So I'm not sure why you would say this.

Hi, @carol_84
Have you done any Arduino coding at all.
Do you have the hardware.
Using NewPing library this is how easy it is to get a distance reading;

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Thank you so much!! It'll help a lot.

I guess it depends on your definition of instantaneous is. Its been awhile but I thought the "no reflection" time out is 300 ms.

Typical HC-SR04 code examples scattered all over the web use pulseIn without a timeout, so the default "no reflection time out" is one second.

The NewPing example uses a default "timeout" of 200 cm, or about 12 ms, if my math is correct (2 meters x 2 / 0.343 m/ms = 12 ms).

Oh! When I said no lag or inertia I was talking about an added time such as the PIR sensor has of about 3 seconds (set by a potentiometer and a couple of capacitors). I guess the SR04 doesn't have any hardware-added inertia, and I wanted to avoid any programmed inertial behavior as well.
That's the main reason I don't use two PIR's instead in this project, due to those 3 added seconds of HIGH status. I should replace two capacitors to reduce that inertia, and is too risky as they're surface mounted...too tiny.

The NewPing library has an example where it pings three sensors. You could easily modify it to ping your two and turn on your LEDs.!ping-3-sensors-sketch

Thank you so much! Very useful!

That assumes a target will always be within 2 m. The OP indicated a trigger at around 2 m, so a return signal might be expected beyond that. A typical SR-04 has a maximum range under best conditions (target flat, large, and normal to the propagation axis) of around 4 m). So cycle time is closer to 30ms to allow for overhead and return signal.

I was replying to @JohnRob in #12 regarding the "default" time-out. The OP needs to make their own judgment about appropriate time-out. If their target will be around 2 m, then yes, certainly, the time-out should be more than 12 ms.

It seems I was off almost an order of magnitude with the max timeout.

A short ultrasonic pulse is transmitted at the time 0, reflected by an object. The senor receives
this signal and converts it to an electric signal. The next pulse can be transmitted when the echo
is faded away. This time period is called cycle period. The recommend cycle period should be no
less than 50ms. If a 10μs width trigger pulse is sent to the signal pin, the Ultrasonic module will
output eight 40kHz ultrasonic signal and detect the echo back. The measured distance is
proportional to the echo pulse width and can be calculated by the formula above. If no obstacle
is detected, the output pin will give a 38ms high level signal.

So the "lag" here is likely not perceivable.