Can code be a substitute for a resistor? (noob alert!)

I'm brand new to arduino, coding, and electronics in general. I'm reading Arduino for Dummies, and trying to understand each step fully before reading the next.

I understand that an LED needs low voltage, and that resistors are used to accomplish this. But if I understand correctly, you can send lower voltage from a pin by lowering the analogWrite value. If a value of 255 sends full power to the LED, wouldn't a value of 100 provide the correct voltage without a resistor?

Sure, I could just use a resistor, but I want to be able to do as much as possible with code. Plus, if code can substitute for a resistor, then I have an unlimited supply of resistors to choose from, and they don't need soldering! :)

If a value of 255 sends full power to the LED, wouldn't a value of 100 provide the correct voltage without a resistor?

No. The purpose of the resistor is to limit the amount of current that the LED tries to draw, not to lower the voltage to the LED.

But if I understand correctly, you can send lower voltage from a pin by lowering the analogWrite value

You didn't understand correctly. Unless you're on a Due with a proper DAC, an analogWrite simply varies the ratio of on time to off time of a full amplitude square wave.

First of all, analogWrite is not writing analog. You are a noob so I don't expect you to know that. AnalogWrite is a misnomer because it should be named pwmWrite since that is what is is writing, a pulse-width-modulated waveform that varies the duty cycle based on the value written. The signal is a 0 to 5V switching signal and the duty cycle is the On time/Off time ratio %. Leds can tolerate higher than normal current for brief periods so yes you can drive an led with a PWM pin IF AND ONLY IF you load the program (download the sketch from your PC to the arduino) BEFORE you connect the led AND if your program sets the PWM value to what you said AT THE BEGINNING OF THE PROGRAM. That being said, what you have neglected to mention or discuss or otherwise acknowledge , is the MOST of the programs that use leds in the arduino world do not use analogWrite code to turn them on and off because they are not applications that require varying intensity. Most of the applications simply require a led as an indicator of the status of some condition. Some people are not interested in all the other arduino applications and ONLY want to drive every kind of led you can imagine in every possible way imaginable. So depending on what you are doing what you said is true but on the other hand if you plug a led into the wrong I/O pin without a resistor and the arduino is not running the PWM code you mentioned, you can fry the led. It's your arduino and your led so it's your call.....

In general, you are correct. But in this case it won't work. Once the LED's voltage gets above its forward voltage its current increases exponentially (translation: very very quickly.) Also the LED responds very quickly. Also the forward voltage changes from LED to LED and over temperature. So even if you could program an exact analog voltage, it would be very difficult to "chase" the forward voltage to keep the current in check.

Wow. Instant replies. Thanks. You guys are great.

Yes, I read the part about the duty cycle, but wan't sure if I understood how to apply it.

Years ago I tried to learn coding, but had no real need to write anything (there were already enough programs out there to do whatever I needed), so I didn't stay interested long enough to learn it. In those days, a hobbyist had few options for applying programming to anything beyond his PC. With Arduino, programming moves out into my offline life, so now I see opportunities to apply it all over the place.

Sure, I could just use a resistor, but I want to be able to do as much as possible with code. Plus, if code can substitute for a resistor, then I have an unlimited supply of resistors to choose from, and they don't need soldering! smiley

The resistor is required to establish a safe maximum led current when the analogWrite() is at 100% (255 counts), then under software you can lower the average led current from the max by lowering the analogWrite count value.

retrolefty: The resistor is required to establish a safe maximum led current when the analogWrite() is at 100% (255 counts), then under software you can lower the average led current from the max by lowering the analogWrite count value.

Sorry, but I don't think this is correct.

The PWM output is a combination of periods with 5v and 0v. When the voltage is high (even with a low duty cycle) the LED will pass as much current as it can get (until it burns out) and will usually draw more than the 40mA max that an Arduino pin is capable of. The resistor is essential to keep the current within safe bounds for the Arduino, under 20mA is best and lower if the LED can't accept that much. The LED will not average the PWM output.

...R

Let me simplify it for you. What you asking about or proposing to do , for whatever reasons you have is BAD PRACTICE. No matter what your reasons, it is a BAD idea to start doing this or get in the habit of doing it. My suggestion to you is forget you ever brought it up and don't ever mention it again.

raschemmel: Let me simplify it for you. What you asking about or proposing to do , for whatever reasons you have is BAD PRACTICE.

I get it. That's why I asked first.

raschemmel: My suggestion to you is forget you ever brought it up and don't ever mention it again.

Brought what up?

Brought what up?

Sure, I could just use a resistor, but I want to be able to do as much as possible with code. Plus, if code can substitute for a resistor, then I have an unlimited supply of resistors to choose from, and they don't need soldering!

This part is an unrealistic overgeneralization....

Plus, if code can substitute for a resistor, then I have an unlimited supply of resistors to choose from, and they don't need soldering!

It's good that you asked.

Robin2:

retrolefty: The resistor is required to establish a safe maximum led current when the analogWrite() is at 100% (255 counts), then under software you can lower the average led current from the max by lowering the analogWrite count value.

Sorry, but I don't think this is correct.

The PWM output is a combination of periods with 5v and 0v. When the voltage is high (even with a low duty cycle) the LED will pass as much current as it can get (until it burns out) and will usually draw more than the 40mA max that an Arduino pin is capable of. The resistor is essential to keep the current within safe bounds for the Arduino, under 20mA is best and lower if the LED can't accept that much. The LED will not average the PWM output.

...R

Yes, it was correct. Note I said "average led current ", not instantaneous current while the PWM is at some variable percentage at a HIGH level. The resistor is calculated to limit the current for the led, typicall 20ma for standard leds, which of course also has to be below the maximum safe current rating of a output pin when the pwm is at 100%. The calculation for the resistor is based only the voltage of the output pin, minus the Vf drop of the led, and the max safe continuous current rating for the led, it does not use 40ma in the calculation.

A newbie asked, so we talk about it.

It is a question of limitations. The instantaneous current must be limited to an amount that won't damage any of the parts.

The absolute maximum current that should be drawn out of an Arduino pin is 40mA. But without current limiting, you can draw quite a bit more than that.

A generic LED, without knowing more about it, may be able to withstand around 100mA instantaneous current. In any case, a lot more than the Arduino limit.

Just a point, the Solder Time 2 watch contains no resistors. And contains thsi comment. Note that it is being driven by a shift register, not the MCU.

"IMPORTANT: While hacking the ST2 it is “safer” to only make changes to the values in the LEDMAT array rather than addressing the matrix directly. The LED matrix is driven directly from the ATmega and the 74HC154 without any current limiting resistors. Each pixel in the matrix is only lit for a short amount of time, so driving a LED with too much current is not a problem. But, if your hacking leads to a program “crash” and a LED gets stuck on, it could burn out"

http://spikenzielabs.com/SpikenzieLabs/SolderTime_2.html

If you are really wanting to avoid using a resistor with a LED (reducing part count, less soldering, etc) you can use a so-called 5V LED. But that is really cheating. 5V LEDs have the resistor already integrated. (Often if you look very carefully inside a 5V LED you should see a little black speck on the anode (+) pin. That is the integrated resistor.)

I have a couple 5V 0.1" spacing LEDs for troubleshooting or very simple test designs. If I have two adjacent Arduino pins I can set one as OUTPUT LOW and control the other. Then I can plug the 5V LED directly into the Arduino pin housings. Especially useful to have one or more indicator LEDs when I have pin13 busy dealing with SPI communications on my UNOs... ;) (I always make sure I can see the little black fleck inside the LED to both verify that I grabbed the right part from my bin and to identify anode from cathode since I've trimmed the pins evenly and the LED body is too small to have a polarity flat/mark on it.)

Sidebar - Anyone else ever notice that if you can see inside a LED, the cathode is invariably the pin that attaches to the cup/reflector that the diode is mounted on, and the anode has the fine wire coming from up the center of the diode?

That wristwatch runs from 3V in an undocumented way.

KeithRB: "... The LED matrix is driven directly from the ATmega and the 74HC154 without any current limiting resistors. ..."

A 74HC chip output pin can source or sync much less than an Arduino pin as its output FETs have higher resistance - these FETs are acting as current limiters for the LED (but taking the 74HC chip outside its absolute maximum ratings alas - bad practice).

can source or sync

"source or sink"

Rhymes with "sync" ?

Rhymes with "sync" ?

Homophone