Notifications
Clear all

Odd UltraSonic Sensor behavior (HC-SR04)

3 Posts
2 Users
0 Likes
813 Views
Spyder
(@spyder)
Member
Joined: 5 years ago
Posts: 846
Topic starter  

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 ?

hcsr04bounce

Is this possibly a low power issue ?

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


   
Quote
(@mariog)
Member
Joined: 4 years ago
Posts: 7
 

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?


   
ReplyQuote
Spyder
(@spyder)
Member
Joined: 5 years ago
Posts: 846
Topic starter  

@mariog

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(ChestSensor);
Serial.print(" - ");
Serial.print(BottomSensor);
Serial.print(" - ");
Serial.println(FaceSensor);
}

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

}


   
ReplyQuote