I am following the arduino starter kit and confused with using resistors with LED's. In chapter 1 there is a resistor before LED's. There was even reasoning to do it because of ohms law(V=I*R). Basically LED's can only accept 23mA and since input voltage is 5V we need to add a 220 ohms resistor.
However in chapter 2 the 220 ohms resistor is added after the LED(pictures below). The only difference is current is passed through pins 3,4 and 5 programmatically using digitalWrite.
Wondering what's the difference in 2nd setup. Thx!
It makes no difference whether the current limiting resistor is "before" or "after" the LED as long as the current passing through the LED also passes through the resistor
Thanks for the response. The resistor would reduce voltage in the circuit, It makes sense now to me.
One follow up question: Wouldn't there be big surge of current to LED initially after turning on the switch and before hitting the resistor that could damage the LED?
No. Electrical current is not like water flowing into an empty pipe. There will be no delay before the current arrives at the resistor. It's more like water flowing into a pipe already full of water. As soon as the flow begins, the rate of flow is the same along the whole length of the pipe.
Your images show two ways to control an LED, as follows:
+5 --- resistor ---- LED ----- Digital output
and
Digital output ---- LED ---- resistor ---- GND
In the first case, you must output a "LOW" signal to light the LED, in the second case, you must output a "HIGH" to light the LED. Two approaches, functionally the same except that the signal you output is opposite.
Since you asked, there are actually three (3) voltage drops to consider:
V1) the output gate junction within the uC (sink or source)
V2) the LED junction, color based (materials & doping)
V3) the resistor drop based on current.
V1 + V2 + V3 = rail voltage, Positive to Ground (for most cases)
There is absolutely no difference in these two circuits:
The resistor in series with the LED is not for voltage, it is to limit the current through the LED. When conducting, an LED has a resistance of nearly zero. Without a current-limiting resistor you would smoke the LED. The value of the series resistor would be 5V/.023A = 217Ω (Ohm's law). 220Ω is the nearest standard value.
Personally, in my projects I use a series resistor of 2KΩ because when using a 220Ω resistor, the LED is blindingly bright.
The brightness at any given current is a function of the LED's design. Since 1980 I've used LEDs, and over that time the light output at any given current has increased dramatically as technology changed. It's wisest to either use the specification for the LED you purchased, if you have that info, or to actually test with your LED. I have LEDs in my parts drawers that are marginal at best at 20 mA, but there are others that, as you say, are blinding.
I reiterate the last line of Steve Mann. Better to use more than 2kOhm in place of 220 Ohms. That is because an LED is bright enough at 1 or 2mA for what you are doing, and it is gentler to the circuitry within the arduino to aim for that. A sensible way to get an LED to max brightness above 10mA, which can be rather startlingly bright, is to use an npn or an nfet.
No, the voltage and current while often compared to water pressure and flow, they have no inertia. So for all intent and purposes the current happens instantly seeing the resistor and led at the same time.
Be aware, this changes when one adds capacitors to the circuit.