Ultrasocic sensors HC-SR04

My code which was supposed to measure distance isn’t working because everytime it shows the duration is 0. Could someone please help me with it?

// defines pins numbers
const int trigPin = 5; // middle
const int echoPin = 6; // middle
const int trigPin2 = 2; // left
const int echoPin2 = 4; // left
const int trigPin3 = 7; // right
const int echoPin3 = 8; // right

// defines variables
long duration;
int distance;

long duration2;
int distance2;

long duration3;
int distance3;

void setup() {
  
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin3, INPUT); // Sets the echoPin as an Input

  pinMode(3,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode (10, OUTPUT);
  pinMode (11, OUTPUT);
  digitalWrite(3,HIGH);
  digitalWrite(9,LOW);
  digitalWrite (10, HIGH);
  digitalWrite (11, LOW);

Serial.begin(9600); // Starts the serial communication

}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if (distance <= 5)
{
  digitalWrite(3,LOW);
  digitalWrite(9,LOW);
  digitalWrite (10, LOW);
  digitalWrite (11, LOW);
} else {
digitalWrite(3,HIGH);
  digitalWrite(9,LOW);
  digitalWrite (10, HIGH);
  digitalWrite (11, LOW);
}

// Clears  the trigPin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance2= duration2*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance2: ");
Serial.println(distance2);
if (distance2 <= 5)
{
  digitalWrite(3,LOW);
  digitalWrite(9,LOW);
  digitalWrite (10, LOW);
  digitalWrite (11, LOW);
} else {
digitalWrite(3,HIGH);
  digitalWrite(9,LOW);
  digitalWrite (10, HIGH);
  digitalWrite (11, LOW);
}

// Clears the trigPin
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration3 = pulseIn(echoPin3, HIGH);
// Calculating the distance
distance3= duration3*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance3: ");
Serial.println(distance3);
if (distance <= 5)
{
  digitalWrite(3,LOW);
  digitalWrite(9,LOW);
  digitalWrite (10, LOW);
  digitalWrite (11, LOW);
} else {
digitalWrite(3,HIGH);
  digitalWrite(9,LOW);
  digitalWrite (10, HIGH);
  digitalWrite (11, LOW);
}
}

Hi,
Try the known code here first.

Then work on your own version...

Well, I tried that code but I don't know why it isn't working. It shows some numbers, which are bad.

gabriela0423:
My code which was supposed to measure distance isn't working because everytime it shows the duration is 0. Could someone please help me with it?

No idea why do you define more than one echo and trigger pins, while the unit has one each. Also what is the controller model? (arduino uno, nano, nodemcu, mega.

I have just tried this code and it works fine.

/*
********************************************
14CORE ULTRASONIC DISTANCE SENSOR CODE TEST
********************************************
*/
#define TRIGGER 5
#define ECHO    4

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO

void setup() {
  
  Serial.begin (9600);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
}

void loop() {
  
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;
  
  Serial.print(distance);
  Serial.println("Centimeter:");
  delay(1000);
}

Istvanb:
No idea why do you define more than one echo and trigger pins, while the unit has one each. Also what is the controller model? (arduino uno, nano, nodemcu, mega.

I have just tried this code and it works fine.

/*

14CORE ULTRASONIC DISTANCE SENSOR CODE TEST


*/
#define TRIGGER 5
#define ECHO    4

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO

void setup() {
 
  Serial.begin (9600);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
}

void loop() {
 
  long duration, distance;
  digitalWrite(TRIGGER, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
 
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;
 
  Serial.print(distance);
  Serial.println("Centimeter:");
  delay(1000);
}

I've tried this code but my arduino uno shows distance 0 cm again. I've tried all my sensors but nothing better happened, it's always the same

Do you supply the sensor with 5V? (as the HC-SR04 will not work at 3.3V)

Are you sure your wiring is correct? If your module gives you 0cm (so practically doesn't work), then I suggest to simplify your code to use one unit, solve the problem then extend your code to start using three sensors.

So I would say go with the code I have linked, that works if the sensor and the wiring works.