Page 1 of 1

ESP32 with 4 HC-SR04 sensors

Posted: Thu May 02, 2019 12:51 pm
by theobouwman
Hello,

I want to use 4 HC-SR04 sensors with the ESP32. Currently I am developing using the Arduino IDE.
Below is the code I am using:

Code: Select all

// Pins
#define SENSORS_NUM 3

#define TRIG_PIN_1 2
#define ECHO_PIN_1 4

#define TRIG_PIN_2 12
#define ECHO_PIN_2 14

#define TRIG_PIN_3 26
#define ECHO_PIN_3 25

// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;

// structs
struct Measurement {
  float cm;
  unsigned long pulse_width;
};

struct Pins {
  unsigned int trigger;
  unsigned int echo;
};

void setup() {

  // The Trigger pin will tell the sensor to range find
  pinMode(TRIG_PIN_1, OUTPUT);
  digitalWrite(TRIG_PIN_1, LOW);
  pinMode(TRIG_PIN_2, OUTPUT);
  digitalWrite(TRIG_PIN_2, LOW);
  pinMode(TRIG_PIN_3, OUTPUT);
  digitalWrite(TRIG_PIN_3, LOW);

  // We'll use the serial monitor to view the sensor output
  Serial.begin(9600);
}

void loop() {
  for (int i = 1; i <= SENSORS_NUM; i++) {
    struct Pins p = get_pins(i);
    struct Measurement m = measure(p);

    // Print out results
    if ( m.pulse_width > MAX_DIST ) {
      Serial.println("Out of range");
    } else {
      Serial.print("pin ");
      Serial.print(i);
      Serial.print(" is ");
      Serial.print(m.cm);
      Serial.println(" cm \t");
    }

    // Wait at least 60ms before next measurement
    delay(60);
  }
}

/*
 * Creates a {Pins} struct for the given index
 * @param index the index of the sensor (1 to 4)
 */
Pins get_pins(int index) {
  struct Pins p;
  
  switch(index) {
    case 1:
      p.trigger = TRIG_PIN_1;
      p.echo = ECHO_PIN_1; 
      break;
    case 2:
      p.trigger = TRIG_PIN_2;
      p.echo = ECHO_PIN_2;
      break;
    case 3:
      p.trigger = TRIG_PIN_3;
      p.echo = ECHO_PIN_3;
    default:
      p.trigger = TRIG_PIN_3;
      p.echo = ECHO_PIN_3;
      break;
  }

  return p;
}

/**
   Perform measurement
   @param trig_pin the trigger pin
   @param echo_pin the echo pin
   @return returns {Measurement} struct
*/
Measurement measure(struct Pins p) {
  unsigned long t1;
  unsigned long t2;
  struct Measurement m;

  // Hold the trigger pin high for at least 10 us
  digitalWrite(p.trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(p.trigger, LOW);

  // Wait for pulse on echo pin
  while (digitalRead(p.echo) == 0);

  // Measure how long the echo pin was held high (pulse width)
  // Note: the micros() counter will overflow after ~70 min
  t1 = micros();
  while (digitalRead(p.echo) == 1);
  t2 = micros();
  m.pulse_width = t2 - t1;

  // Calculate distance in centimeters. The constants
  // are found in the datasheet, and calculated from the assumed speed
  //of sound in air at sea level (~340 m/s).
  m.cm = m.pulse_width / 58.0;
  return m;
}
Everything works until I add the third sensor with pins 26 and 25. I can confirm that all sensors work. And I am using a external 5v power supply because the ESP32 only outputs 3.3v, to my understanding.

If someone could help me that would be great.

Thanks in advance,

Theo