Interfacing HC-SR04 Ultrasonic sensor to Arduino UNO

Hello all.

I'm trying to interface two HC-SR04 Ultrasonic sensors to Arduino UNO. All the digital pins are occupied and I'm left with the Analog pins A0 - A5. Below I have attached the code which I have used and only one sensor is working for that code. Please help me to find out the problem.

Thanks.

long duration1, cm1, inches1,duration2, cm2, inches2;
void setup() {
  Serial.begin (9600);
  pinMode(A2,OUTPUT);
  pinMode(A3,INPUT);
  pinMode(A0,OUTPUT);
  pinMode(A1,INPUT);

}

void loop() {
 digitalWrite(A2,HIGH);
 delayMicroseconds(10);
 digitalWrite(A2,LOW);
 
 digitalWrite(A0,HIGH);
 delayMicroseconds(10);
 digitalWrite(A0,LOW);
 

 pinMode(A3,INPUT);
 duration1 = pulseIn(A3,HIGH);

 pinMode(A1, INPUT);
  duration2 = pulseIn(A1, HIGH);
 
  // convert the time into a distance
  cm1 = (duration1/2) / 29.1;
  inches1 = (duration1/2) / 74; 

  cm2 = (duration2/2) / 29.1;
  inches2 = (duration2/2) / 74; 
  
  Serial.print(inches1);
  Serial.print("in, ");
  Serial.print(cm1);
  Serial.print("cm");
  Serial.println();
  Serial.print(inches2);
  Serial.print("in, ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
  
  delay(1000);

}

Use each sensor in turn, not both at once. ie. Ping the first sensor and measure how long until the echo arrives using 'pulseIn()', then do the same for the second one.

Can you please explain with a code?

void loop() {
 digitalWrite(A2,HIGH);
 delayMicroseconds(10);
 digitalWrite(A2,LOW);

 pinMode(A3,INPUT);
 duration1 = pulseIn(A3,HIGH);

 
 digitalWrite(A0,HIGH);
 delayMicroseconds(10);
 digitalWrite(A0,LOW);
 

 pinMode(A1, INPUT);
  duration2 = pulseIn(A1, HIGH);

If your second sensor doesn't get reliable resuts it may be picking up some late echos from the first sensor. Try adding a delay(30) between the two.

If loop() is fast you might also need a delay between the second ping and the end of loop().

Thank you all for the help. It is working perfectly.