Clear all

Odd UltraSonic Sensor behavior (HC-SR04)  


Prominent Member
Joined: 1 year ago
Posts: 828
2020-08-26 12:18 pm  

I've got 3 Ultrasonic sensors connected to an Arduino Mega

If I only use 2 of them, (and it doesn't matter which 2) things seem fine, but when I connect the third one, I get this wierd bouncing behavior in the output... from ALL OF THEM... at the SAME TIME

They DO seem to function sort of correctly, meaning that if I put my hand in front of one of them, it gives a proper readout, and that test works for all of them, but, sometimes, if I just sit there and watch the readout, it, (to use a technical term), goes all kerfluey

Has anybody seen this before ?


Is this possibly a low power issue ?

None of the sketches I've seen suggest any extra power needs to be injected

New Member
Joined: 2 months ago
Posts: 4
2020-08-26 1:34 pm  

Are you polling the sensors in turn one after another with a suitable delay between the data read/write operations, or attempting to read the output all at once from all three?

This post was modified 2 months ago by mariog

Prominent Member
Joined: 1 year ago
Posts: 828
2020-08-27 9:50 pm  


I obviously didn't write it, I found it and modified it. It looks like the answer is no delay

#define trigPin1 9
#define echoPin1 10
#define trigPin2 4
#define echoPin2 5
#define trigPin3 8
#define echoPin3 6

long duration, distance, FaceSensor,ChestSensor,BottomSensor;

void setup()
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);

void loop() {
SonarSensor(trigPin1, echoPin1);
FaceSensor = distance;
SonarSensor(trigPin2, echoPin2);
ChestSensor = distance;
SonarSensor(trigPin3, echoPin3);
BottomSensor = distance;

Serial.print(" - ");
Serial.print(" - ");

void SonarSensor(int trigPin,int echoPin)
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;