LED not turning completely off

Hello, I am a new arduino user and this is my first post. I am tinkering with a program segment that will turn on a LED through a predetermined value of a LDR and at a predetermined level of brightness, and then will turn off after a 5 seconds.

My problem is that the LED does not entirely turn off - it still has about 0.75v through it??

Code
const int LED = 9; // the pin for the LED
int val = 0; // variable used to store the value coming from the sensor
int brightness = 255; // sets the brightness value the LED
int ledState = LOW; // LED state used to set the LED

void setup() {
pinMode(LED, OUTPUT); // LED is an output
// note: analogue pins are automatically set as inputs
}
void loop() {
val = analogRead(0); // read the value from the sensor
if((val > 500)) {
digitalWrite(LED, brightness); // turns on the LED after reaching a predetermined
// value of the sensor and at an assigned 'brightness' level
}
if (millis() > 5000) { // turns LED off after reaching
// specified milliseconds
digitalWrite(LED, ledState); // turns LED off
}
else
{
delay(10); // stop the program for some time
}
}
/Code

I appreciate any help that can be given.
With thanks

rmbsj160

Why you using digitalWrite?

analogWrite (ledpin, value); (you set it to 255)

As for it not switching off... damaged pin?

If you do mean digitalWrite, use HIGH/LOW not an integer..

Also you using a resistor ?

Please use code tags.

Read this before posting a programming question

How to use this forum

Thank you for your responses.

I have a 10K resister between LDR & Gnd, and 4.7K resister between LED & Gnd. I have also tried the suggestions re digitalWrite. I also tried locating the LED pos on another pin and also using analogWrite instead of digitalWrite - all with the same result.

Unfortunately, the LED is still not fully turning off (despite designating as '0' or 'LOW' within digitalWrite).

I suspect it may have something to do with the LDR, as when it is covered (finger over) the LED light fully extinguishes.

Thank you Nick for the reminder about the text box around the code.

Any other suggestions will be gratefully received.
regards
rmbsj160

rmbsj160:
I suspect it may have something to do with the LDR, as when it is covered (finger over) the LED light fully extinguishes.

Then it means that you have to either scale your resistor connected to the LDR or do it in software.

If the atmega is not working and the transistors are not fully switching off ... (according to your meter) i'm a little concerned.

You could place a diode (to drop the voltage) in front of the LED... (I don't like the idea though)

rmbsj160:
I suspect it may have something to do with the LDR, as when it is covered (finger over) the LED light fully extinguishes.

Then a diode will be fine (to drop the voltage low enough)

Can you post a picture of the circuit (including pins in/out from arduino) the LED is obviously getting it's power from something other than a grounded arduino pin!

cjdelphi:
If the atmega is not working and the transistors are not fully switching off ... (according to your meter) i'm a little concerned.

You could place a diode (to drop the voltage) in front of the LED... (I don't like the idea though)

His scaling is just wrong.

rmbsj160:
I have a 10K resister between LDR & Gnd, and 4.7K resister between LED & Gnd. I have also tried the suggestions re digitalWrite. I also tried locating the LED pos on another pin and also using analogWrite instead of digitalWrite - all with the same result.

Unfortunately, the LED is still not fully turning off (despite designating as '0' or 'LOW' within digitalWrite).

I suspect it may have something to do with the LDR, as when it is covered (finger over) the LED light fully extinguishes.

Could you show us how you connected the LDR and the LED?

You can do it something like this...

GND___10K Resistor___Arduino pin___LDR___5V

GND___LED___4K7 Resistor___Arduino Pin

BTW, 4.7K is way too high for driving a LED from 5V. It should be down around 400-500 ohms at most/

lar3ry:
GND___4K7 Resistor___Arduino Pin___LED___5V

Are you sure about that one?

Publish the actual wiring of your current setup! Include power hookup to LED!
Rip it up and start over!

Use digital pin.
Verify that DIGITAL pin used has INTERNAL limiting resistor - read the spec. ( I don't now)
if not attach SUITABLE resistor to limit the current thru your LED when on – use Ohms law.
You may do this regardless of internal limiting resistor if you are not sure.
Any resistor around 1kOhm should work. Remember the pin limit is 50mA.

Program that digital pin to turn the LED On/ Off. Play around with different way of controlling it – delay, delay loops,software buttons, timer – go wild.

AFTER it works
Use analog pin.
Change the pin to ANALOG OUTPUT.
Attach limiting resistor as specified by LDR.
Program that analog pin to turn the LED On/ Off.
Does it work – fully on / off?
Play with brightness.
Done

UnoDueTre:

lar3ry:
GND___4K7 Resistor___Arduino Pin___LED___5V

Are you sure about that one?

Well it's all wrong, of course, but I was only trying to illustrate a method of showing wiring, and didn't spend much thought on what it was actually doing.

lar3ry:

UnoDueTre:

lar3ry:
GND___4K7 Resistor___Arduino Pin___LED___5V

Are you sure about that one?

Well it's all wrong, of course, but I was only trying to illustrate a method of showing wiring, and didn't spend much thought on what it was actually doing.

Nice, and than you expect the group to spend their time to help you?

Vaclav:
Nice, and than you expect the group to spend their time to help you?

Lar3ry is not the original poster - he is merely suggesting how to represent wiring diagrams textually.

Thank you Nick for the reminder about the text box around the code.

You can edit your existing post to include them.....

Here's two suggestions:

  • Use serial to send info to the serial monitor. For example send the LDR reading so you can see what it is, and also a message to indicate where the code has got to in the ifs, eg "I'm in the val>500 part". If you've never used serial before, have a look here
  • If you don't have a tool for doing schematics, then draw one freehand on paper and take a pic with your phone- that should be clear enough for us to see what your circuit is.

Vaclav:
Nice, and than you expect the group to spend their time to help you?

I just changed it to something realistic. All right with you?

When I asked if the schematic was correct, It was simply to point out that one has to be cautious when posting advice to others which are only starting out and may copy code or circuits verbatim.

In this day and age, there is no good reason not to have a schematic drawing package.
ASCII art often does not convey all of the important points well (like polarities and component orientation) and leaves plenty of opportunity for errors.
There are plenty good freeware schematic packages available, or as a last resort one could always take a picture (or scan) a hand drawn schematic or scribble something on "Paint" or similar (just remember to upload as a jpeg and not a bmp).

Thank you again to those who have made comments and suggestions.

I have included an attached diagram (rough).

I have tried or modified the following:

  1. pos pole of LED on other digital pins (same result)
  2. varied 'analogWrite to digitalWrite and vice versa (same result)
  3. Tried a diode (IN4007) in front of LED as suggested by cjdelphi (no LED result)
  4. adjusted resister on LED to 180 Ohm (using ohms law) - anable current of 0.028mA (suggested bt lar3ry)
  5. tested LDR further on multimeter and changed resister to 1K5 ohm (same result)
  6. Varying 'brightness' is effective via analogWrite only, but remnant LED glow remains as long as 'brightness' is > 175). If 'brightness' <= 175, the LED fully extinguishes. If set to digitalWrite after line 'if (val > 500)' 'brightness' seems ineffective (ie. LED lights fully ie. HIGH) and does not extinguish at end of sketch (LED reads 0.44v)
  7. circuit pulled apart and re-constructed (same result)

At end of sketch, when I measure voltage between pos LED and gnd it is 0.44v ??? If I remove the LDR, the LED extinguishes fully.

My initial post indicated that this was my first post and I am new to Arduino. I am no doubt doing something that is obviously incorrect.

In appreciation of any helpful comments

rmbsj160

I haven't time to have a proper look, but here's a thought: if that's a long, full-sized breadboard, the power rails are often broken in the middle, depending on make.

Can't tell from your sketch exactly where the things are plugged in, but seeing as you use the power rail, could it be that the wire from the Arduino ground, which goes to one side of the resistor, doesn't actually meet the resistor if they're opposite sides of the break?