Multiple HC-SR04 with as few pins as possible

sparknburn:
I guess something like this?

I have a couple of diodes and resistors lying around, I might actually try that.

I was thinking about doing it like but, but I was a bit afraid of getting weird results if the echo from sensor A ends up in the receiver for sensor B. Do you notice any issues with crosstalk between the sensors?

So lets look at what could change with the wiring that may resolve you concern
If we were to hook the first sensor trigger pin to the arduino pin 2 as I suggested but hook the second sensor trigger pin to the echo pin of first sensor. continue this for each sensor 3's trigger to 2's echo, 4's trigger to 3's echo.

Datasheet: https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf
The trigger pin only fires after the pulse to it falls the pulse has a minimum "Using IO trigger for at least 10us high level signal" but I didn't see a maximum limit on the datasheet. so the above method would fire each sensor in sequence. and you timing of the rise an fall of the echo pin is what you would use to detect distance. using your simple diode circuit you could achieve multiple sensors with 1 trigger pin and 1 input pin.

.........................

The more I type the more I'm still favoring the all at once method because the actual sensor only senses a 15° window. meaning that any pulse that is able to reflect back would be a indication of an object that is within 15° . Since the start pulse is exactly the same on all devices the result is more accurate even if it is a reflection of a reflection is detected (something is there but its just stealthy) that actually returns a signal. My thought is that this is more like a submarine that sends out 1 pulse and listens in all directions for each resulting echo if 2 sensors pick up the same echo you can triangulate the exact position on a flat plane

Z

Here is some simple code I put together for the above circuit :slight_smile: i created (did not test)

#define EchoInputCount 3
#define TriggerPin  2
// echo pin will be interrupt 1 on pin 3
#define DelayBetweenPings 50 // it works to about 5 milliseconds between pings

volatile  unsigned long PingTime[EchoInputCount];
volatile int Counter = 0;
volatile  unsigned long edgeTime;
volatile  uint8_t PCintLast;
int PinMask = B1000; // pin 3
float Measurements[EchoInputCount];

void PintTimer( )
{
  uint8_t pin;
  static unsigned long cTime;
  cTime = micros();         // micros() return a uint32_t
  pin = PIND >> 3 & 1;      // Quickly get the state of  pin 3
  if (pin)edgeTime = cTime; //Pulse went HIGH store the start time
  else { // Pulse Went low calculate the duratoin
    PingTime[Counter % EchoInputCount] = cTime - edgeTime; // Calculate the change in time  NOTE: the "% EchoInputCount" prevents the count from overflowing the array look up % remainder calculation
    Counter++;
  }
}
void debug() 
{
  char S[20];
  static unsigned long PingTimer;
  if ((unsigned long)(millis() - PingTimer) >= 100) {
    PingTimer = millis();
    for(int c = 0; c < EchoInputCount; c++){
      Serial.print(dtostrf(Measurements[c], 6, 1, S));
    }
    Serial.println();
  }
}

float microsecondsToInches(long microseconds) 
{
  return (float) microseconds / 74 / 2;
}

float microsecondsToCentimeters(long microseconds) 
{
  return (float)microseconds / 29 / 2;
}


void PingTrigger(int Pin)
{
  Counter = 0;
  digitalWrite(Pin, LOW);
  delayMicroseconds(1);
  digitalWrite(Pin, HIGH); // Trigger another pulse
  delayMicroseconds(10);
  digitalWrite(Pin, LOW);
}

void PingIt()
{
  unsigned long PT[EchoInputCount];
  static unsigned long PingTimer;
  if ((unsigned long)(millis() - PingTimer) >= DelayBetweenPings) {
    PingTimer = millis();
    cli ();         // clear interrupts flag
    for(int c = 0; c < EchoInputCount; c++){
       PT[c] = PingTime[c];
    }
    sei ();         // set interrupts flag
    for(int c = 0; c < EchoInputCount; c++){
         Measurements[c] = (float) (microsecondsToCentimeters(PT[c]));
    }
    //      Measurements = (float) (microsecondsToInches(PT));
    PingTrigger(TriggerPin); // Send another ping
  }
}

void setup()
{
  Serial.begin(115200);
  Serial.println("Ping Test");
  pinMode(3, INPUT);
  pinMode(2, OUTPUT);
  attachInterrupt(1, PintTimer, CHANGE );
}

void loop() 
{
  PingIt(); // Manage ping data
  debug(); // display ping data (at a slower rate due to serial limitations)
}