Using two ultrasonic sensors control one LED light

Hi everyone, I am doing a project which uses two ultrasonic sensors to control one LED light. When the two ultrasonic sensors detect objects within 5cm at the same time, it will trigger the light. If just one sensor detects an object, it will not trigger the light.

I have done some research and done some prototypes, but they did not work very well. Hope someone would give me some suggestions if that is possible. Thank you very much.

In order to find the problem, I changed the sensor first, but it still has the same problem. Thus I think my sensor maybe not the problem. I have also changed the two sensor's connection. I did not change the position of the two sensors. I just change their connection. I connect the pin of left sensor (not working before)to the trigger pin 1 and echopin1. (It connected to triggerpin2 and echopin2 before). At this stage, I find that the left sensor works but the right sensor did not work now. I am wondering is it the problem of the wire connection or the code?...I am not quite sure. And the serial monitor shows that the distance 1 always has the same value 0cm. Or sometimes it becomes a very high value over 200cm...

I have attached my code and my wire connection photos underneath. If someone could help me, that will be great! Really appreciate!

#define TRIGGER_PIN1  12
#define ECHO_PIN1     11
#define TRIGGER_PIN2  6
#define ECHO_PIN2     5
#define LIGHT_PIN     3

long duration1, duration2;  
int distance1, distance2;          

void setup() {
  Serial.begin(9600);        
  pinMode(TRIGGER_PIN1, OUTPUT);  
  pinMode(ECHO_PIN1, INPUT);  
  pinMode(LIGHT_PIN, OUTPUT);
  pinMode(TRIGGER_PIN2, OUTPUT);  
  pinMode(ECHO_PIN2, INPUT); 
   
} 
 
void loop() { 
  /*==================================
   *    Ultrasonic Sensor#1
   ==================================*/
  //clear TRIGGER_PIN1
  digitalWrite(TRIGGER_PIN1, LOW);  
  delayMicroseconds(2);          

  //set TRIGGER_PIN1 to High for 10 microseconds
  digitalWrite(TRIGGER_PIN1, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN1, LOW);

  //read the duration of the sound wave with ECHO_PIN1
  duration1 = pulseIn(ECHO_PIN1, HIGH);

  //calculate distance based on duration of ultrasound from triggerPin to echoPin
  distance1 = duration1*0.034/2;
  
  //add thresholds to correct for sensor value errors
  if (distance1 < 0 ) distance1 = 0;
  if (distance1 > 200) distance1 = 200;
  

  /*==================================
   *    Ultrasonic Sensor#2
   ==================================*/
  //clear TRIGGER_PIN2 value, setting it to LOW for 2 microseconds
  digitalWrite(TRIGGER_PIN2, LOW);  
  delayMicroseconds(2);          

  //set TRIGGER_PIN2 to High for 10 microseconds
  digitalWrite(TRIGGER_PIN2, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN2, LOW);

  //read the duration of the sound wave with ECHO_PIN1
  duration2 = pulseIn(ECHO_PIN2, HIGH);

  //calculate distance based on duration of ultrasound from triggerPin to echoPin
  distance2 = duration2*0.034/2;
  
  //add thresholds to correct for sensor value errors
  if (distance2 < 0 ) distance2 = 0;
  if (distance2 > 200) distance2 = 200;

  /*==================================
   *    Print to Serial Monitor
   ==================================*/
  
  Serial.print("distance1: ");
  Serial.print(distance1);
  Serial.print("cm, distance 2");
  Serial.print(distance2);
  Serial.println("cm ");

  /*==================================
   *    Control LED
   ==================================*/  

  if (distance2 <= 30 && distance2 <= 30) {
    digitalWrite(LIGHT_PIN, HIGH);
    Serial.println("Light Triggered *******");
    delay(3000);
  } else {
    digitalWrite(LIGHT_PIN, LOW);
  }
  
}

  if (distance2 <= 30 && distance2 <= 30) {

I should think that if distance 2 is less than 30 then it is also less than 30.

Can one sensor 'hear' the other? If they are both pointed roughly the same direction then sensor 2 may hear the echo from sensor 1 coming off the back wall long after you stopped listening on the sensor 1 pin. Stick a big delay between the two readings. (Like a hundred milliseconds.)

Thanks Morgan!!!That's help!!! :smiley: