# Ultrasonic Sensor HCSR-04

**URL:** <https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842>\
**Category:** Sensors\
**Created:** [August 24, 2024, 12:45am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842 "2024-08-24T00:45:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 12:45am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/1 "2024-08-24T00:45:14Z")

</div>

Hi everyone,

I wonder if you can help me, I have this code below using ultrasonic sensor HCSR-04. My problem is I am only getting a printout of zero on the serial monitor.```

int trigPin = A0;  
int echoPin= A1;  
int pingTravelTime;  
int dt = 2;  
int dt1 = 10;  
int dt2 = 25;

void setup() {  
//Serial Port begin  
Serial.begin (9600);  
//Define inputs and outputs  
pinMode(trigPin, OUTPUT);  
pinMode(echoPin, INPUT);  
}

void loop() {  
digitalWrite(trigPin, LOW);  
delayMicroseconds (dt1);  
digitalWrite(trigPin, HIGH);  
delayMicroseconds(dt1);  
digitalWrite(trigPin, LOW);  
pingTravelTime = pulseIn(echoPin, HIGH);  
delay(dt2);  
Serial.println(pingTravelTime);

}type or paste code here

```arduino

```

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [August 24, 2024, 1:11am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/2 "2024-08-24T01:11:04Z")

</div>

Welcome! You have an interesting project, but first please read the advice in the topic "How to get the best from this forum". There is a proper way to post code, this allows us to help you. Taking what you posted and putting into a readable form takes a lot of time many do not want to spend. It will only take you another 10 seconds after your figure it out. l have no clue as to what each hardware device is or how it interacts with your code, post an annotated schematic, the language of electronics, showing how you have wired it, not pictures such as frizzy (wiring) drawings they are useless for troubleshooting. Do include links to "Technical information" on the hardware parts, links to sales outlets like azon are next to useless. Doing this will have a big positive effect on your answers and will improve the accuracy of the answer a lot.

Check this out:

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behavior and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frus…

I would sure like to help you but since you did not take the time to read the forum guidelines I will not burn the time trying to figure out the gibberish you posted. All I can say at this point is good luck!

---

<div class="post-metadata">

**Author:** ![Brazilino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/brazilino/32/1198404_2.png) [@Brazilino](https://forum.arduino.cc/u/Brazilino)\
**Post date:** [August 24, 2024, 1:32am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/3 "2024-08-24T01:32:00Z")

</div>

Hi! Welcome to the Forum.

I agree with gilshultz that you should have read "How to get the best out of this Forum", but since I've already spent some time reading your code, here's my 5 cents:

1. you don´t need analog pins to use this sensor and `pulsein()` expects an _int_ as the first parameter. So, you can either still use A0 and A1 physical pins but use their digitalself ID (A0=14 and A1=15) OR choose another pair of digital pins;

2. `pulsein()` returns an unsigned long type. So _pingTravelTime_ should obey this type

Test the code version below:

```arduino
int trigPin = 14;
int echoPin= 15;
unsigned long pingTravelTime;
int dt = 2;
int dt1 = 10;
int dt2 = 25;

void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds (dt1);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(dt1);
  digitalWrite(trigPin, LOW);
  pingTravelTime = pulseIn(echoPin, HIGH);
  delay(dt2);
  Serial.println(pingTravelTime);
}

```

---

<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:** [August 24, 2024, 4:40am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/5 "2024-08-24T04:40:18Z")

</div>

If you want to see a bar graph representation of the numbers, try this:

```arduino
#define trigPin A0
#define echoPin A1

long duration;
int distance;
int wall = 290;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // create trigger pulse
  digitalWrite(trigPin, LOW); // turn off pulses
  delayMicroseconds(2); // pause before transmitting
  digitalWrite(trigPin, HIGH); // transmit pulses...
  delayMicroseconds(10); // ...for 10 microseconds
  digitalWrite(trigPin, LOW); // turn off pulses
  duration = pulseIn(echoPin, HIGH); // time the echo

  for (int i = 0; i < duration / wall; i++) { // create graph of echo
    Serial.print("*");
  }
  for (int i = 0; i < 80 - (duration / wall); i++ ) { // fill the cube to 100
    Serial.print(" ");
  }
  Serial.println("|");
  delay(100);
}

```

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [August 24, 2024, 6:56am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/6 "2024-08-24T06:56:36Z")

</div>

Hi, @chito1623  
Have you looked at the NewPing library, it does it all for you.

> **[NewPing - Arduino Reference](https://www.arduino.cc/reference/en/libraries/newping/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [August 24, 2024, 7:59am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/7 "2024-08-24T07:59:00Z")

</div>

chito1623,

I tried the code that you posted in post #1.  
I can confirm that your code did work, and correctly displayed the 'pingTravelTime' in microseconds.

However, I was able to get it to display zero, by disconnecting the 'trigPin' wire from Arduino A0 pin.

I think you need to check your wiring.

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 10:31am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/8 "2024-08-24T10:31:07Z")

</div>

Thanks, because this is my first time. I will take your advice. My project is to integrate this ultrasonic sensor to read the pingTravelTime so I can use this variable to get a target distance. However sensor is printing 0 on the serial monitor. I've different iterations but I'm stucked with this sensor. Thanks

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 10:49am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/9 "2024-08-24T10:49:22Z")

</div>

thanks for your reply. I wired this sensor as normal wiring diagram thru arduino nano. The code works but its only printing zero to the serial monitor.  
I want to use this variable from the "pingTravelTime" to measure distance. If I'm getting zero variable I can't use this variable to measure distance.  
Below is another Arduino code uploaded to my Arduino Uno R4 and its still printing 0 and 0cm on the serial monitor.  
I wired the vcc pin of the sensor to 5 volts, trigPin to pin 6, echoPin to pin 5 and ground pin to ground of Arduino.  
I hope I done this reply to you ok.  
Thanks

Here is another code I tried and it's printing 0 on the serial monitor.

```arduino
`t#include <HCSR04.h>

int trigPin = 6;
int echoPin = 5;
int pingTravelTime;
int dt = 10;
int dt1 = 100; //for accuracy 100 microseconds delay rate recommended
int dt2 = 200;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);

}

void loop() {
// establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

digitalWrite(trigPin, LOW);
delayMicroseconds(dt);
digitalWrite(trigPin, HIGH);
delayMicroseconds(dt);
digitalWrite(trigPin, LOW);
pingTravelTime = pulseIn(echoPin, HIGH);
delayMicroseconds(dt1);
Serial.println(pingTravelTime);

// The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
    duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(dt2);
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;
}

```

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 10:53am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/10 "2024-08-24T10:53:10Z")

</div>

Hi TomGeorge,

Thanks for your help. I tried what you suggested and its still printing 0 on the serial monitor.  
I want to use this variable "pingTravelTime" to measure distance so I can incorporate this to a robot car.  
Thanks

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [August 24, 2024, 11:02am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/11 "2024-08-24T11:02:49Z")

</div>

Hi, @chito1623

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?  
Hand drawn and photographed is perfectly acceptable.  
Please include ALL hardware, power supplies, component names and pin labels.

Can you post some images of your project?  
So we can see your component layout.

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 11:07am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/12 "2024-08-24T11:07:29Z")

</div>

Hi Brazilino,

Thanks for your reply, I tried your modified code wired the sensor as vcc pin of sensor to 5 volts pin of arduino, trigPin of sensor to A0 of Arduino, echo pin of sensor to A1 of Arduino and ground of sensor to ground of Arduino.  
It's printing 0 on the serial monitor. What numbers should be printing on the monitor?

---

<div class="post-metadata">

**Author:** ![Brazilino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/brazilino/32/1198404_2.png) [@Brazilino](https://forum.arduino.cc/u/Brazilino)\
**Post date:** [August 24, 2024, 11:16am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/13 "2024-08-24T11:16:09Z")

</div>

> [@chito1623](#):
>
> What numbers should be printing on the monitor?

Something between 118 and 23343. You can take a look by yourself at the simulation here:

[https://wokwi.com/projects/407098178668363777](https://wokwi.com/projects/407098178668363777)

click on the ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/4/3/04359fa659ddbb979230bfd49072be3a3f8e2f26.png) button. When the sim starts, click on the Sonar and change the values.

> [@chito1623](#):
>
> I want to use this variable "pingTravelTime" to measure distance

I think for now you should follow @TomGeorge 's advice and use a library to get the distance without worrying with manually triggering the sensor and doing the math.

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [August 24, 2024, 11:27am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/14 "2024-08-24T11:27:54Z")

</div>

Chito1623,  
I've just tried your code from post #9.  
It partially works.

It looks like you have got that got that code by combining two separate codes.

- You trigger the trigPin
- The echoPin generates a pulse
- You measure the pulse width using pulseIn()
- You print the 'pingTravelTime' - correct result
- You then use pulseIn() again - but the pulse has already gone!
- The serial monitor now prints zero for the distance in inches / centimetres

 ![Screenshot 2024-08-24 120532](https://europe1.discourse-cdn.com/arduino/original/4X/9/7/8/978ae8a606713ca19a7354bd6353565c4dd330dc.png)

You can't use pulseIn() twice to measure the same pulse.

If you change that:

```arduino
duration = pulseIn(echoPin, HIGH); `

```

to:

```arduino
duration = pingTravelTime;

```

you can use the initial (correct) results twice.

![Screenshot 2024-08-24 123711](https://europe1.discourse-cdn.com/arduino/original/4X/a/7/2/a72d994497634580ee699ba54af619955a5afaad.png)

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 11:33am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/15 "2024-08-24T11:33:42Z")

</div>

Sorry Tom George, I don't know how to paste a photo to this forum.  
Thanks

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [August 24, 2024, 11:51am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/16 "2024-08-24T11:51:45Z")

</div>

> [@chito1623](#):
>
> I don't know how to paste a photo to this forum.

When you reply, click on the upload button that I have highlighted below.

You can then navigate to whereabouts on your computer the photo is, and then add it.  
 ![Screenshot 2024-08-24 Upload](https://europe1.discourse-cdn.com/arduino/original/4X/5/e/9/5e963d7435e171a4fe8cfa404ce561ef5478ad8c.png)

---

<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:** [August 24, 2024, 11:58am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/17 "2024-08-24T11:58:37Z")

</div>

> [@chito1623](#):
>
> It's printing 0 on the serial monitor.

Do you have something in front of the sensor so it can get an echo?

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 12:25pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/18 "2024-08-24T12:25:40Z")

</div>

> [@chito1623](#):
>
> ```arduino
> #include <HCSR04.h>
> 
> int trigPin = 6;
> int echoPin = 5;
> int pingTravelTime;
> int dt = 10;
> int dt1 = 100; //for accuracy 100 microseconds delay rate recommended
> int dt2 = 200;
> 
> void setup() {
> pinMode(trigPin, OUTPUT);
> pinMode(echoPin, INPUT);
> Serial.begin(9600);
> 
> }
> 
> void loop() {
> // establish variables for duration of the ping, and the distance result
> // in inches and centimeters:
> long duration, inches, cm;
> 
> digitalWrite(trigPin, LOW);
> delayMicroseconds(dt);
> digitalWrite(trigPin, HIGH);
> delayMicroseconds(dt);
> digitalWrite(trigPin, LOW);
> pingTravelTime = pulseIn(echoPin, HIGH);
> delayMicroseconds(dt1);
> Serial.println(pingTravelTime);
> 
> // The same pin is used to read the signal from the PING))): a HIGH pulse
> // whose duration is the time (in microseconds) from the sending of the ping
> // to the reception of its echo off of an object.
> duration = pulseIn(echoPin, HIGH);
> 
> // convert the time into a distance
> inches = microsecondsToInches(duration);
> cm = microsecondsToCentimeters(duration);
> 
> Serial.print(inches);
> Serial.print("in, ");
> Serial.print(cm);
> Serial.print("cm");
> Serial.println();
> 
> delay(dt2);
> }
> 
> long microsecondsToInches(long microseconds) {
> // According to Parallax's datasheet for the PING))), there are 73.746
> // microseconds per inch (i.e. sound travels at 1130 feet per second).
> // This gives the distance travelled by the ping, outbound and return,
> // so we divide by 2 to get the distance of the obstacle.
> // See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
> return microseconds / 74 / 2;
> }
> 
> long microsecondsToCentimeters(long microseconds) {
> // The speed of sound is 340 m/s or 29 microseconds per centimeter.
> // The ping travels out and back, so to find the distance of the object we
> // take half of the distance travelled.
> return microseconds / 29 / 2;
> }
> 
> ```

Hi johnLincoln,

I tried what you suggested I'm still getting zero on the serial monitor.

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 12:26pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/19 "2024-08-24T12:26:17Z")

</div>

Yes

---

<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:** [August 24, 2024, 12:40pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/20 "2024-08-24T12:40:18Z")

</div>

Are you using a solderless breadboard to make connections?

---

<div class="post-metadata">

**Author:** ![chito1623](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chito1623/32/1053475_2.png) [@chito1623](https://forum.arduino.cc/u/chito1623)\
**Post date:** [August 24, 2024, 12:45pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842/21 "2024-08-24T12:45:47Z")

</div>

Hi Brazilino,

I change sensor and the code works fine now. Thanks

[Next page](https://forum.arduino.cc/t/ultrasonic-sensor-hcsr-04/1294842.md?page=2)
