# Problem Ultrasonic sensor

**URL:** <https://forum.arduino.cc/t/problem-ultrasonic-sensor/697006>\
**Category:** Programming\
**Created:** [February 20, 2021, 5:04pm UTC](https://forum.arduino.cc/t/problem-ultrasonic-sensor/697006 "2021-02-20T17:04:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coderturkey](https://avatars.discourse-cdn.com/v4/letter/c/439d5e/32.png) [@coderturkey](https://forum.arduino.cc/u/coderturkey)\
**Post date:** [February 20, 2021, 5:04pm UTC](https://forum.arduino.cc/t/problem-ultrasonic-sensor/697006/1 "2021-02-20T17:04:37Z")

</div>

Hello, I am a beginner to C++ and I have only done a few projects. Currently, I am trying to make a distance measurer with an ultrasonic sensor [(Data shee](https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf)t). What I want it to do is, when there is something close to the sensor the LED will turn on, however, the LED is always on and there is no error message.

here is the code

```cpp
const int trigPin = 13;
const int echoPin = 12;

long duration;
int distance;
int led6 = 6;
int led5 = 5;
int led4 = 4;
int led3 = 3;
int led2 = 2;
int led1 = 1;

void setup() {
  pinMode(led6,OUTPUT);
  pinMode(led5,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led1,OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delay(500);

  digitalWrite(trigPin, HIGH);
  delay(500);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;

  Serial.print("DISTANCE: ");
  Serial.println(distance);

  
  if (distance < 10){
    
    digitalWrite(led6,HIGH);
    digitalWrite(led5,HIGH);
    digitalWrite(led4,HIGH);
    digitalWrite(led3,HIGH);
    digitalWrite(led2,HIGH);
    digitalWrite(led1,HIGH);
  }
    else( digitalWrite(led1, LOW));
    

}

```

If anyone is able to help it would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [February 20, 2021, 5:12pm UTC](https://forum.arduino.cc/t/problem-ultrasonic-sensor/697006/2 "2021-02-20T17:12:02Z")

</div>

What values do your prints show?

Also, why such long delays? Typical trigger pulses are single digit microseconds, though most I've seen will trigger on a falling edge.

---

<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:** [February 21, 2021, 4:59am UTC](https://forum.arduino.cc/t/problem-ultrasonic-sensor/697006/3 "2021-02-21T04:59:43Z")

</div>

Here is a link to the circuit and code to do your project:  
[https://create.arduino.cc/projecthub/Junezriyaz/distance-measurement-using-arduino-ultrasonic-sensor-409169](https://create.arduino.cc/projecthub/Junezriyaz/distance-measurement-using-arduino-ultrasonic-sensor-409169)  
Have a great day:-)

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 21, 2021, 1:32pm UTC](https://forum.arduino.cc/t/problem-ultrasonic-sensor/697006/4 "2021-02-21T13:32:00Z")

</div>

```cpp
  digitalWrite(trigPin, HIGH);
  delay(500);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

```

Waiting half a second (500 milliseconds) after pulsing the Trigger pin HIGH is causing you to miss the echo pulse. The pulseIn() function doesn't see a pulse so it times out (1 second) and returns zero. You see 0 as \<10 (close) rather than "too far to get an echo from" so you light up the light.

Look at examples of the ultrasonic sensor and you will see delays in the order of a few MICROSECONDS. Take that delay(500) out and check for a 0 return (no echo) from pulseIn().

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 21, 2021, 1:32pm UTC](https://forum.arduino.cc/t/problem-ultrasonic-sensor/697006/5 "2021-06-21T13:32:48Z")

</div>

Closed on June 21, 2021, 1:32pm UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
