L LED in low state still emitting light

im a biggener doing my first project. Im making distance sensor project.
im using grove ultrasonic sensor.
i want the L led to turn off under certain distance and turn on above that distance.
i manged to programmer it but L led doesn't turn off completely it just emitting
very low light compare to the second state.

onst int pingPin = 13;
unsigned int duration, inches;
void setup() {
void loop() {
pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
digitalWrite(pingPin, LOW); // Ensure pin is low
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(pingPin, LOW); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
inches = duration / 74 / 2; // Convert to inches
Serial.println(inches); // Display result
delay(1000); // Short delay

if (inches < 10)
digitalWrite(pingPin, LOW);
digitalWrite(pingPin, HIGH);

We can't see your code.
We can't see your schematic.

added to the original massage
thank you

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Can you confirm you're controlling the LED and the ranger . . .using the same pin?

the code above turns the LED on/off each iteration of loop().

seems like you only want to do this once, in setup()

1 Like

How to properly post code see below:

pinMode(pingPin, OUTPUT); // Set pin to OUTPUT << belongs in setup

What LED is doing the bad thing?

Make it simple on yourself use the NewPing library.

You appear to be using pin 13 for two conflicting purposes:

  1. As a led indicator.
  2. As an input pin for your sensor.

The glowing led is probably being fed through the pin pull up resistor.
Use separate pins for these two purposes.


Can you please post a circuit diagram of your project?
A hand drawn schematic would be fine, just make sure you label your components and pin names.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

1 Like

yes ,i have 3 pins
and one goes to 13 with the L led

you can in the picture that there are 4 wires going out from the sensor but only 3 going to the arduino
the white is cut
so i plugged pin 13, gnd and 3.3 v

thanks alot

Why not use a pin that isn't being used by the on-board LED?

because i want to turn the L LED on on a certain distance
can i light the L led but using other pin ?

But your code tells the LED to turn on or off, not the ultrasonic ranger.


The ultrasonic sensor ONLY provides the signals to detect an object, YOUR code does the calculation.
You might be better off using the "NewPing" libaray to do your range sensing .
And look that some of the examples.

Tom.. :smiley: :+1: :coffee: :australia:

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