3 Ultrasonic Sensors Jumping Back and Forth

I've exhausted reading posts and sites trouble shooting this so now I'm seeking the experienced to offer advice.

I have 3 HC-SR04 ultrasonic sensors in a row like so

A -->

B -->

C -->

They are mounted to the wall and meant to read distance of the closest object at the moment.

Running this sketch.

const int atpin = 2;
const int aepin = 3;
const int btpin = 4;
const int bepin = 5;
const int ctpin = 6;
const int cepin = 7;

void setup() {
  Serial.begin(9600);

  pinMode(atpin, OUTPUT);
  pinMode(aepin, INPUT);
  pinMode(btpin, OUTPUT);
  pinMode(bepin, INPUT);
  pinMode(ctpin, OUTPUT);
  pinMode(cepin, INPUT);
}

void loop()
{
  long adur, acm;
  digitalWrite(atpin, LOW);
  delayMicroseconds(2);
  digitalWrite(atpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(atpin, LOW);
  adur = pulseIn(aepin, HIGH);

  long bdur, bcm;
  digitalWrite(btpin, LOW);
  delayMicroseconds(2);
  digitalWrite(btpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(btpin, LOW);
  bdur = pulseIn(bepin, HIGH);
  
  long cdur, ccm;
  digitalWrite(ctpin, LOW);
  delayMicroseconds(2);
  digitalWrite(ctpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ctpin, LOW);
  cdur = pulseIn(cepin, HIGH);

  Serial.print(adur);
  Serial.print(" ");
  Serial.print(bdur);
  Serial.print(" ");
  Serial.println(cdur);

  delay(1000);
}

I know I can void function() another to make the repeated pings easier in code but that's not the problem. In both codes I get this jumping back and forth. Left is A, Center is B etc
When blocking A physically and leaving B and C open, they read a signal then don't then do then don't etc.

190481 194420 8634 <-- Blocking Left
3875 4027 8656
190799 194971 8553
3792 3979 8776
189674 193434 8576
3819 4082 8632
190276 192711 8677
3739 4055 8608
189802 194260 6276
3721 3930 8656
188833 194558 7253
3753 3613 5279
190889 3214 3000 <-- Blocking Center
189799 3304 3000
189367 193782 6457
189930 3538 8663

From any one's experience, WTH?!

Thanks!

Hi,
Between each ping,read sequence for each sensor, delay until you ping the next, you probably have reflections from all over the place from the previous sensor ping.

Have you tried your code with just each sensor on its own?

Just to check that each is performing in its own right.

Tom..... :slight_smile:

This is not good.

long adur, acm;
  digitalWrite(atpin, LOW);
  delayMicroseconds(2);
  digitalWrite(atpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(atpin, LOW);
  adur = pulseIn(aepin, HIGH);

Write the trigger pins to initial LOW in setup, not in the loop.

long adur, acm;
//  digitalWrite(atpin, LOW);
//  delayMicroseconds(2);
  digitalWrite(atpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(atpin, LOW);
  adur = pulseIn(aepin, HIGH);

TomGeorge:
Hi,
Between each ping,read sequence for each sensor, delay until you ping the next, you probably have reflections from all over the place from the previous sensor ping.

Have you tried your code with just each sensor on its own?

Just to check that each is performing in its own right.

Tom..... :slight_smile:

I have tried each individually before and they still do jump. I had considered and looked up methods to stop beams being picked up by other sensors but after seeing each jump on their own, it was what inspired me to finally post here. :confused: I was wondering if there needs to be some resistor or cap or diode across some where to regulate the signal or ?

aisc:
This is not good.

long adur, acm;

digitalWrite(atpin, LOW);
  delayMicroseconds(2);
  digitalWrite(atpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(atpin, LOW);
  adur = pulseIn(aepin, HIGH);




Write the trigger pins to initial LOW in setup, not in the loop.



long adur, acm;
//  digitalWrite(atpin, LOW);
//  delayMicroseconds(2);
  digitalWrite(atpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(atpin, LOW);
  adur = pulseIn(aepin, HIGH);

Makes sense.
Did not solve the jumping issue.

TVMiller:
I have tried each individually before and they still do jump. I had considered and looked up methods to stop beams being picked up by other sensors but after seeing each jump on their own, it was what inspired me to finally post here. :confused: I was wondering if there needs to be some resistor or cap or diode across some where to regulate the signal or ?

The HC-SR04 needs a rock solid and stable power supply.

Between sender and receiver casings of the HC-SR04 there is no acoustic bridge allowed, they must be in mid-air.

With a weak power supply that creates a voltage drop during the ping triggering or an acoustic bridge between sender and receiver you might read "jumping" values as well as if the triggering rate is too high (repeated triggering while reflected pings are still on their way through the air).

jurs:
The HC-SR04 needs a rock solid and stable power supply.

Between sender and receiver casings of the HC-SR04 there is no acoustic bridge allowed, they must be in mid-air.

With a weak power supply that creates a voltage drop during the ping triggering or an acoustic bridge between sender and receiver you might read "jumping" values as well as if the triggering rate is too high (repeated triggering while reflected pings are still on their way through the air).

I don't have a variable bench power supply but I do have a lot of power supplies in general so I will read up on optimum amperage and see if I can accomplish a more reliable current. Thanks for the tip.

TVMiller:
I don't have a variable bench power supply but I do have a lot of power supplies in general so I will read up on optimum amperage and see if I can accomplish a more reliable current. Thanks for the tip.

Typically you can use without any problem:

  • industrial made Arduino board (UNO, LEONARDO, MEGA or other 5V boards)
  • USB power supply from a desktop PC
  • HC-SR04

What do you have that might cause problems?
Home-made circuit board?
Weak power supply from a tablet or notebook computer USB?
A non-5V controller board?

jurs:
Typically you can use without any problem:

  • industrial made Arduino board (UNO, LEONARDO, MEGA or other 5V boards)
  • USB power supply from a desktop PC
  • HC-SR04

What do you have that might cause problems?
Home-made circuit board?
Weak power supply from a tablet or notebook computer USB?
A non-5V controller board?

Wired directly in to an Arduino Uno that is either powered by the PC USB or a 5V supply in to the Uno.
I ended up adding to the coding a smoothing, using FOR for 10 samples and dividing it so I get an average that should lessen the effects of the jumping. I honestly think these HC-SR04 are terrible products because I have used on other projects HC-06 (3 pins) and they didn't give nearly the head ache and unreliable results.

One other issue I started knocking off was interference from other sensors. I built fake walls to break up the noise from each sensor and still the jumping of readings occurred.

I appreciate all the help but I'm going with this substitute smoothing and moving forward rather than get bogged here. Thanks!

Try this single sensor code - it is tried and tested.
Adapt to your needs.

/* Sensor : Arduino + HC-SR04 with LED/Pin (relay) + Serial Output */
/* Operating Range : min + max limit settings */

#define trigPin 2
#define echoPin 3
#define datPin 13                        // alternatively use as ledPin

float minrange;                          // minimum trip range
float maxrange;                          // maximum trip range
long duration;
float distance;
void setup()
{
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(datPin, OUTPUT);
  digitalWrite(trigPin, LOW); 	         // ensure low state at start
  delay(3000);
}

void loop()
{
   minrange = 7;                         // 5 + 2 for calibration adjustment
   maxrange = 37;                        // 35 + 2 for calibration adjustment
   digitalWrite(trigPin, HIGH);          // initiate pulse
   delayMicroseconds(10);                // maintain pulse for 10�s
   digitalWrite(trigPin, LOW);           // terminate pulse

   duration = pulseIn(echoPin, HIGH);    // measures delay till echo heard
   distance = duration / 29.1 / 2;       // calculates distance in cm

   if (distance > minrange && distance < maxrange) {
     digitalWrite(datPin, HIGH);         // close relay circuit
     Serial.print("Activity Detected at ");
     Serial.print(distance);
     Serial.println(" cm");
     delay(1000);                        // maintain relay closed state
     digitalWrite(datPin, LOW);          // open relay circuit
     delay(2000);}                       // delay before restarting cycle after detection
   else{
     Serial.print("No Activity : ");
     Serial.print(distance);
     Serial.println(" cm");}
   delay(1000);                          // delay before restarting cycle after no detection
}