How does current work with leds?

I tried experimenting to understand 5v, 3.3v and gpio pin circuits with an led.

I connected an led anode to 5v and cathode to gpio 7 (non-pwm) and found that gpio 7 can have a highest value of 127 out of 255 for the led to still light up, which translates to 2.49 volts.

I expected that if I changed the 5v to 3.3v the led wouldn't light up (because of the lower voltage difference), but the led lit up as well.

Also, when I use gpio 11 (pwm pin), I can go as high as 254 and still have some light output. How does that work?

Thank you for helping me understand this!

First, do you have a resistor in series with the LED?

I connected an led anode to 5v and cathode to gpio 7

When the Arduino output pin goes low, current flows from positive to negative through the LED and it lights up. With an LED wired that way, PWM is reversed with 255 being off and zero being full-brightness.

(non-pwm) and found that gpio 7 can have a highest value of 127 out of 255

That sounds like PWM. What Arduino are you using?

If you write anything other than a zero to a digital (non PWM) pin you'll get a HIGH (about 5V). I don't know what happens with analogWrite() to a non-PWM pin.

which translates to 2.49 volts.

The Arduino doesn't have true-analog outputs. The output pins are either zero or +5V. [u]PWM[/u] switches quickly between zero and 5V. That can make an LED appear dim or it can control the speed of a motor. A multi-meter might read something close to the average but you can't count on that.

Or, that could be the voltage across the LED with another 2.5V across the resistor.

Or, if you don't have a resistor you may be drawing excess current and "pulling down" (or "pulling up") the Arduino's output voltage. I that case it could be putting 2.49V and that would be a bad thing!

I expected that if I changed the 5v to 3.3v the led wouldn't light up (because of the lower voltage difference), but the led lit up as well.

Most LEDs have a forward operating voltage of about 2V (depending on color, etc.). But, you need a little extra to "drop" across the resistor so you can control the current.

I can go as high as 254 and still have some light output. How does that work?

8-bit PWM goes from zero to 255. With the LED wired "backwards", PWM works "backwards" and 254 is equivalent to "1" and the LED may glow dimly. (Actually it's "flashing" very briefly at full brightness but since it's mostly off, our eyes see it as dim.)

Thank you for your explanation! Yeah, I got a resistor in series with the LED.

DVDdoug:
If you write anything other than a zero to a digital (non PWM) pin you'll get a HIGH (about 5V). I don't know what happens with analogWrite() to a non-PWM pin.
The Arduino doesn't have true-analog outputs. The output pins are either zero or +5V.

I didn't know about this. I always thought analogwrite meant the pin could have any voltage value between 0v and 5v. I'm using an arduino nano, and I'm pretty sure that gpio7 isn't pwm. But from what you said, I think what I was experiencing was analogWrite() rounding up or down (0 or 255) for non-pwm pins. 127 round down to 0 volts, while 128 rounds up to 5 volts. Also, thank you for helping me understand how PWM works.

This code from the analogWrite() source is fairly self explanatory

		switch(digitalPinToTimer(pin))
		{
                [...]
		case NOT_ON_TIMER:
			default:
				if (val < 128) {
					digitalWrite(pin, LOW);
				} else {
					digitalWrite(pin, HIGH);
				}

… if not, pins without PWM capability just get set to a logic level with the threshold analog value of 128.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

Do you have a DMM?

Tom.... :slight_smile:

LEDs output light (energy) when current is flowing through them (there is a maximum current allowed). In order to get current to flow you need to push it with voltage (Ohm's Law). The LED is a bit tricky and has a voltage threshold (color dependent) Vf (Voltage Forward) that has to be exceed before current will flow. This voltage is different on each one (a few micro volts) not much but enough to keep them from being driven properly when connected in parallel. Therefore the best way is to connect them in series. Kirchhoffs Current Law or KCL, states that the “total current or charge entering a junction or node is exactly equal to the charge leaving the node as it has no other place to go except to leave, as no charge is lost within the node“. In other words the algebraic sum of ALL the currents entering and leaving a node must be equal to zero, I(exiting) + I(entering) = 0. This idea by Kirchhoff is commonly known as the Conservation of Charge. To simplify this every item in the series circuit must pass the same current. In your case you need to go to a lower value series resistor and start with RED it has the lowest Vf. Why not start by defining the problem with specifications including a flow chart, schematic, power requirements, and EMI requirements if any. Define what the expected outcome needs to be. Purchase the Arduino cookbook and read it, this will give you some basics. also use the online tutorials and videos available, there are many good ones on this web site. At this point you will be able to define the problem and may have already solved it.