# HCSRO4 problems

**URL:** <https://forum.arduino.cc/t/hcsro4-problems/1241881>\
**Category:** Sensors\
**Created:** [March 30, 2024, 4:08pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881 "2024-03-30T16:08:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [March 30, 2024, 4:08pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/1 "2024-03-30T16:08:40Z")

</div>

Hello, I am doing a project that tracks a train and switches the electromagnet on when the train is closed. I am using a code for the HCSR04 but the readings for distance are messed up, sometimes they are correct but sometimes they are wrong, just really inconsistent, I don't think it's the circuit. This is the code I used.

```arduino
#include <NewPing.h>

#define MAX_DISTANCE 200 // Maximum distance in centimeters

// Define ultrasonic sensor pins
const int trigpin_1 = 3;
const int echopin_1 = 2;
const int trigpin_2 = 5;
const int echopin_2 = 4;
const int trigpin_3 = 7;
const int echopin_3 = 6;

// Define MOSFET pins
const int mosfet_1 = 8;
const int mosfet_2 = 9;
const int mosfet_3 = 12;

// Create NewPing objects for each ultrasonic sensor
NewPing sonar_1(trigpin_1, echopin_1, MAX_DISTANCE);
NewPing sonar_2(trigpin_2, echopin_2, MAX_DISTANCE);
NewPing sonar_3(trigpin_3, echopin_3, MAX_DISTANCE);

void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinMode(mosfet_1, OUTPUT);
  pinMode(mosfet_2, OUTPUT);
  pinMode(mosfet_3, OUTPUT);
}

void loop() {
  // Read distances from ultrasonic sensors
  int cm_1 = sonar_1.ping_cm();
  int cm_2 = sonar_2.ping_cm();
  int cm_3 = sonar_3.ping_cm();

  // Print distances to serial monitor
  Serial.print("Distance from Sensor 1: ");
  Serial.print(cm_1);
  Serial.println(" cm");

  Serial.print("Distance from Sensor 2: ");
  Serial.print(cm_2);
  Serial.println(" cm");

  Serial.print("Distance from Sensor 3: ");
  Serial.print(cm_3);
  Serial.println(" cm");

  // Control MOSFETs based on distance readings
  digitalWrite(mosfet_1, cm_1 < 10 ? HIGH : LOW);
  digitalWrite(mosfet_2, cm_2 < 10 ? HIGH : LOW);
  digitalWrite(mosfet_3, cm_3 < 10 ? HIGH : LOW);

  // Add a short delay to avoid excessive readings
  delay(100);
}

```

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [March 30, 2024, 4:25pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/2 "2024-03-30T16:25:38Z")

</div>

You may need to leave some time between pings to let the old echos die out. I usually wait about 35 ms between.

The quality of the return echo depends on the target. Solid surfaces perpendicular to the sensors are best. Soft targets or targets at an angle may not return strong signals.

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [March 30, 2024, 4:27pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/3 "2024-03-30T16:27:43Z")

</div>

Pleasse where shld I put the delay?

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [March 30, 2024, 4:34pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/4 "2024-03-30T16:34:41Z")

</div>

Delay between sending the pings and eliminate the 100ms at the end of the code.

---

<div class="post-metadata">

**Author:** ![noobmastha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noobmastha/32/738653_2.png) [@noobmastha](https://forum.arduino.cc/u/noobmastha)\
**Post date:** [March 30, 2024, 5:48pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/5 "2024-03-30T17:48:55Z")

</div>

What is the position in space of those 3 sensors, maybe you have interference problem?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [March 30, 2024, 7:39pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/6 "2024-03-30T19:39:50Z")

</div>

> [@l4v3r](#):
>
> put the delay?

> [@groundFungus](#):
>
> leave some time between pings to let the old echos die out. I usually wait about 35 ms

> [@l4v3r](#):
>
> ```arduino
> // Read distances from ultrasonic sensors
> int cm_1 = sonar_1.ping_cm();
> delay(35); // you should make a timed interval for this
> int cm_2 = sonar_2.ping_cm();
> delay(35); // you should make a timed interval for this
> int cm_3 = sonar_3.ping_cm();
> 
> ```

For timed intervals in milliseconds, read about "millis()"...  
[https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/)

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 11:33am UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/7 "2024-04-01T11:33:19Z")

</div>

I tried it and it still didn't work, but I don't think the delay is the reason the readings are messing up

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 11:35am UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/8 "2024-04-01T11:35:16Z")

</div>

Sometimes the readings would be right at first, then after like 3 times cm\<10, they start messing up. Like they stay on the last number they were on

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 1, 2024, 12:19pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/9 "2024-04-01T12:19:51Z")

</div>

> [@l4v3r](#):
>
> I tried it and it still didn't work, but I don't think the delay is the reason the readings are messing up

Why don't you think that is the reason?

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 12:33pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/10 "2024-04-01T12:33:35Z")

</div>

I don't know, I just didn't think it would affect the readings

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 1, 2024, 12:45pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/11 "2024-04-01T12:45:42Z")

</div>

You should make the sketch that disproves (fails) "...leave some time between pings..." is a solution.

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 12:11pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/12 "2024-04-01T12:11:33Z")

</div>

I am doing a train project and using hcsro4's to track train positions. I was testing a code, when I use this code the readings are accurate and consistent:

```arduino
const int trigpin_1 = 3;
const int echopin_1 = 2;
const int trigpin_2 = 5;
const int echopin_2 = 4;
const int trigpin_3 = 7;
const int echopin_3 = 6;
int cm_1;
int cm_2;
int cm_3;

long readUltrasonicDistance_1(int trigpin_1, int echopin_1) {
  pinMode(trigpin_1, OUTPUT); // Clear the trigger
  digitalWrite(trigpin_1, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_1, LOW);
  pinMode(echopin_1, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_1, HIGH);
}

long readUltrasonicDistance_2() {
  pinMode(trigpin_2, OUTPUT); // Clear the trigger
  digitalWrite(trigpin_2, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_2, LOW);
  pinMode(echopin_2, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_2, HIGH);
}
long readUltrasonicDistance_3() {
  pinMode(trigpin_3, OUTPUT); // Clear the trigger
  digitalWrite(trigpin_3, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_3, LOW);
  pinMode(echopin_3, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_3, HIGH);
}

void setup() {
  Serial.begin(9600);
  pinMode(trigpin_1, OUTPUT);
  pinMode(echopin_1, INPUT);
  pinMode(trigpin_2, OUTPUT);
  pinMode(echopin_2, INPUT);
  pinMode(trigpin_3, OUTPUT);
  pinMode(echopin_3, INPUT);
}

void loop() {
 cm_1 = 0.01723 * readUltrasonicDistance_1(3, 2);
  Serial.print("Distance from Sensor 1: ");
  Serial.print(cm_1);
  Serial.println(" cm");

 cm_2 = 0.01723 * readUltrasonicDistance_2();
  Serial.print("Distance from Sensor 2: ");
  Serial.print(cm_2);
  Serial.println(" cm");

 cm_3 = 0.01723 * readUltrasonicDistance_3();
  Serial.print("Distance from Sensor 3: ");
  Serial.print(cm_3);
  Serial.println(" cm");

  delay(100); // Delay between measurements
}

```

But when I add the mosfets to turn on, the readings start to mess up, sometimes they r stuck on same reading, or they aren't as accurate:

```arduino
const int trigpin_1 = 3;
const int echopin_1 = 2;
const int trigpin_2 = 5;
const int echopin_2 = 4;
const int trigpin_3 = 7;
const int echopin_3 = 6;
int cm_1;
int cm_2;
int cm_3;
const int mosfet_1 = 8;
const int mosfet_2 = 9;
const int mosfet_3 = 12;

long readUltrasonicDistance_1(int trigpin_1, int echopin_1) {
  pinMode(trigpin_1, OUTPUT); // Clear the trigger
  digitalWrite(trigpin_1, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_1, LOW);
  pinMode(echopin_1, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_1, HIGH);
}

long readUltrasonicDistance_2() {
  pinMode(trigpin_2, OUTPUT); // Clear the trigger
  digitalWrite(trigpin_2, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_2, LOW);
  pinMode(echopin_2, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_2, HIGH);
}
long readUltrasonicDistance_3() {
  pinMode(trigpin_3, OUTPUT); // Clear the trigger
  digitalWrite(trigpin_3, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_3, LOW);
  pinMode(echopin_3, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_3, HIGH);
}

void setup() {
  Serial.begin(9600);
  pinMode(trigpin_1, OUTPUT);
  pinMode(echopin_1, INPUT);
  pinMode(trigpin_2, OUTPUT);
  pinMode(echopin_2, INPUT);
  pinMode(trigpin_3, OUTPUT);
  pinMode(echopin_3, INPUT);
  pinMode(mosfet_1, OUTPUT);
  pinMode(mosfet_2, OUTPUT);
  pinMode(mosfet_3, OUTPUT);
}

void loop() {
 cm_1 = 0.01723 * readUltrasonicDistance_1(3, 2);
  Serial.print("Distance from Sensor 1: ");
  Serial.print(cm_1);
  Serial.println(" cm");
  if (cm_1<20){digitalWrite(mosfet_1, HIGH);}
  else{digitalWrite(mosfet_1, LOW);}

 cm_2 = 0.01723 * readUltrasonicDistance_2();
  Serial.print("Distance from Sensor 2: ");
  Serial.print(cm_2);
  Serial.println(" cm");
  if (cm_2<20){digitalWrite(mosfet_2, HIGH);}
  else{digitalWrite(mosfet_2, LOW);}

 cm_3 = 0.01723 * readUltrasonicDistance_3();
  Serial.print("Distance from Sensor 3: ");
  Serial.print(cm_3);
  Serial.println(" cm");
  if (cm_3<20){digitalWrite(mosfet_3, HIGH);}
  else{digitalWrite(mosfet_3, LOW);}

  delay(100); // Delay between measurements
}

```

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 1, 2024, 12:24pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/13 "2024-04-01T12:24:24Z")

</div>

This seem to be the same as your other post

> [@HCSRO4 problems](https://forum.arduino.cc/t/hcsro4-problems/1241881):
>
> Hello, I am doing a project that tracks a train and switches the electromagnet on when the train is closed. I am using a code for the HCSR04 but the readings for distance are messed up, sometimes they are correct but sometimes they are wrong, just really inconsistent, I don't think it's the circuit. This is the code I used. #include \<NewPing.h\> #define MAX\_DISTANCE 200 // Maximum distance in centimeters // Define ultrasonic sensor pins const int trigpin\_1 = 3; const int echopin\_1 = 2; const i…

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 12:34pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/14 "2024-04-01T12:34:38Z")

</div>

I thought. the code was just messed up, but know it works well without mosfet and doesn't work with mosfet, so I don't know if it's the code or the mosfet, cuz mosfets work well too

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [April 1, 2024, 12:39pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/15 "2024-04-01T12:39:16Z")

</div>

Why did you started a new thread?  
Crossposting is a violation of the forum rules.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 1, 2024, 12:42pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/16 "2024-04-01T12:42:36Z")

</div>

So which post is correct this one or the other one?

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 12:43pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/17 "2024-04-01T12:43:35Z")

</div>

My bad, I thought it would be like a new topic since problem changed

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 12:43pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/18 "2024-04-01T12:43:50Z")

</div>

This one is correct then

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 1, 2024, 1:13pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/19 "2024-04-01T13:13:03Z")

</div>

What is connected to the mosfet\_1,2,3 pins?

---

<div class="post-metadata">

**Author:** ![l4v3r](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/l4v3r/32/1015583_2.png) [@l4v3r](https://forum.arduino.cc/u/l4v3r)\
**Post date:** [April 1, 2024, 1:18pm UTC](https://forum.arduino.cc/t/hcsro4-problems/1241881/20 "2024-04-01T13:18:22Z")

</div>

It's meant to be an electromaget and led, but rn I'm using water pump and leds

[Next page](https://forum.arduino.cc/t/hcsro4-problems/1241881.md?page=2)
