LED reversed - weird problem

Hi Everyone,

I am a newbie with arduino and have been playing around with it for a while.

Things have been working well but today I am having a weird problem with the leds.

The leds turn off with HIGH and turn on with LOW. not sure what I am doing wrong.

here is how I have connected them.

/ ----> to pin 7
grd --> 1k resistor -->
----> -ve or short end of led --> +ve or long end of led ---> 5 v

Hope that makes sense.

I am using an arduino mega 2560 board.

Thanks

So to understand your connections.

you have the positive anode (long leg) connected to 5V

the short negative cathode (short leg) to the current limit resistor (1K).

But it looks like the other side of the resistor is going to ground, so what is going to pin 7.

one side of the resistor is going to the ground while the other is going to pin 7.

OK, are you trying to do a LED project like the simple blink http://arduino.cc/en/Tutorial/Blink.
If so you would go from pin 7 to LED and resistor then to ground. The 5V pin is not used as the 5V is supplied to LED on pin 7 HIGH.
So you would not tie the LED to 5V pin of the Arduino.

If I understand your setup, the LED will light by pin 7 low as the 5V is powering the LED to pin 7.
This puts 5v using what ever resistance pin 7 has as the current limiting, this could cause over current on pin 7 and the LED.

When with your wiring pin 7 is high it has almost 5V on it and so you have 5V on both anode and cathode so no LED light.

Maybe this will help make it clear if this is how it is hooked up. If it is I would suggest you change it.

The way the LED works high for off and low for on is correct for how you have it wired up. This is called current sinking and is the normal professional way of lighting LEDs. Beginners do not like this and so use current sourcing where a high turns the LED on.

However that resistor is doing nothing, it is not in the circuit, remove it and it works just the same. You are letting too much current flow through the Arduino pin and damaging it. Please put the resistor in line with the LED, that is between the LED and the output pin. Connect nothing to ground.

The way your LED is wired up is such that if you disconnect pin 7 or the Arduino from the LED it will light all the time. When you drive pin 7 high, that puts the cathode voltage at 5V which is the same as the anode voltage so it has no voltage across it and will not light. When you drive pin 7 low, the LED then has 5V across it and will light (and as noted above, will damage both the LED and the Arduino).

If you want to wire up the arduino to an LED so that it lights on a high, then go from pin 7 to the anode of the LED and then have a 220 or 330 Ohm resistor from the cathode to ground.

If, later on you decide that you would like an inverting light (which is what you have now) connect the resistor to +5V, and the other end to the LED anode, and connect the LED cathode to pin 7. That way you current limit the LED and it will only light when pin 7 is low.

Hi, the diagram below will explain.

Tom... :slight_smile:

LEDs.jpg

Thanks guys, my current set up is like in the circuit diagram posted by spicetraders. weird that it wouldn't work because all my push buttons are on a similar circuit as well.

I will try the TomGeorge way now.

so to sum it up, ground --> resistors --> -ve or short end of the led --> +ve or long end of the led --> pin 7

would that make it work better.

the way i have it set up right now seemed more intuitive for some reason. going to try it now.

Thanks

btw, this doesnt apply to push buttons does it?

btw that worked for the leds.

Thank you so much guys.

now i am just wondering if my set up for push buttons is wrong and should be changed as well.

what do you think?

Hi, for switches/ pushbuttons.

Tom... :slight_smile:

I am using diagram one for the switches so I think i am good.

this might need a separate thread but I am having a problem with the same project.

all the pins are working fine except for some reason pins 53 and 51 randomly turn to the high state. the only way to fix this problem is to get rid of the loop for push buttons on pins 53 and 51.

The code is exactly the same as for other pins so not sure whats going on.

thoughts?

Note that pins 53 and 51 are used for SPI on the Mega ... try using other available pins.
If your using pulldown resistors, make sure the pinmode is INPUT, not INPUT_PULLUP.

that would explain that. thanks.

I thought they were just normal digital communication pins.

rm65453:
I am using diagram one for the switches so I think i am good.

No, it is not really good.

Diagram three is the preferred version because it is in general not a good idea to have the 5V line itself going to any component that it does not need to and especially where that involves going to external wiring - as may often be the case with switches or sensors - due to the consequences of an accidental short to ground. If a switch or sensor line is shorted to ground where it switches to ground anyway, the worst that happens is a spurious input.

This point of efficient design is precisely why input pull-ups are provided in the MCU and not pull-downs.

rm65453:
I thought they were just normal digital communication pins.

They are. But if you use the SPI at all, those are the pins you use, paralleled to the ICSP/ SPI header.

Ok I am gonna give diagram 3 a shot, would definitely make my circuit less complicated so I hope it works out for me.

Do I have to make any changes to the code? I mean where I define the buttons. would pinMode(pin, INPUT); still work?

No, set pinMode(pin,INPUT_PULLUP) and then you are looking for a low value not a high one.

Just to clarify so when the button is not pressed, the output would be HIGH and when the button is pressed then the output would be LOW.

Is that correct?

rm65453:
Just to clarify so when the button is not pressed, the output would be HIGH and when the button is pressed then the output would be LOW.

Is that correct?

Yes. But not output, it is the input that would be high / low.

sorry that's what I meant.

Thanks :slight_smile:

you guys have been amazing.