Problem reading 2 HC-SR04 sensors

Hi, I was trying to interface multiple HC-SR04 ultrasonic sensors to my Arduino Uno. Since the pulsIn function is blocking the code, I tried to write the code using interrupts.
Everything went well, but when testing, the first sensor gives a good distance to a object (at a fixed distance), but the second not. The distance read with the second is much lower than the actual distance.
The distance to the first object was 10cm (measured with a ruler), what it also says. But the distance to the second object was about 20cm (not exactly measured), but the sensor says less than 10 cm. And when I moved the second object closer to the sensor (to about 10 cm), the values of the first sensor halves, but the object 1 has not moved.

This is my code.

#define trig 2


volatile unsigned long current_time, time_dist1, time_dist2;
volatile unsigned long timer1, timer2;
volatile byte last_ultra1, last_ultra2;
unsigned long timer = 0;


void setup() {
  Serial.begin(9600);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
  pinMode(trig, OUTPUT);
  cli();
  PCICR |= B00000001;
  PCMSK0 |= B00000011; // enable interupts when pin D8 or pin D9 changes.
  sei();
}


void loop() {
  // trigger the ultrasoon modules
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);


  // pause until the loop time is 50 milliseconds
  while (timer + 50 > millis());
  timer = millis();


  // calculate and print the distance in cm
  Serial.print("distance 1: ");
  Serial.print((time_dist1 / 2.0) * 0.03435);
  Serial.print("\tdistance 2: ");
  Serial.println((time_dist2 / 2.0) * 0.03435);


}


ISR(PCINT0_vect){   // ISR for port B, PCINT0 - PCINT7, digital pin 8-13
  current_time = micros();
  if (PINB & B00000001) { // pin 8 high?
    timer1 = current_time;
    last_ultra1 = 1;
  }
  else if (last_ultra1 == 1){ // pin 8 again low?
    last_ultra1 = 0;
    time_dist1 = current_time - timer1;
  }
  if (PINB & B00000010) {
    timer2 = current_time;
    last_ultra2 = 1;
  }
  else if (last_ultra2 == 1){
    last_ultra2 = 0;
    time_dist2 = current_time - timer2;
  }
}

Thanks in advance.

Two sensors, but only one trigger?
You're triggering two sensors at the same time, and expecting to differentiate their echoes?
Are they operating in different rooms?

Sorry for late reply. Didn't get a notification mail from a reply and I don't know where to find this when logged in. quite new at the forum.

Two sensors, but only one trigger?
You're triggering two sensors at the same time, and expecting to differentiate their echoes?
Are they operating in different rooms?

Yes. Only 1 trigger. The test was in the same room (both sensors where on the same breadboard). Do you think that is the issue?
I want to implement this on my drone to create an obstacle avoidance system. If I don't use only 1 trigger, I run out of pins. The way to differentiate the echoes is to use different echo pins.
So if I test this outside, they would return the right values?

Simple ultrasonic rangers can't distinguish the source of a ping.
If you trigger two at once, both will hear a return, possibly via different path lengths.

But that interference is only if they are in the same room? Not if it is outside?

The SR04 is a terrible module for any sort of precision use, the dispersal cone of sound is 15 degrees, and is seriously prone to noise.

Consider using a LIDAR.

The SR04 receiver is tuned to 38Khz. The transmitted frequency is 40Khz. There is a mod that can be done to the SR04 to fix this issue. The parts cost more then a TFMini.

I have used a TFMini and now use a TFMiniP on my hexapod to create an image of the things in front of the hexapod. The image comes with ranges and is used to by the hexapod to avoid things.

Yeah, but the lidar modules are much more expensive

cdtje:
Yeah, but the lidar modules are much more expensive

Good luck with using a SR04 to manage your drone's positioning.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.