Yesterday I bought the Dutch translation of the Arduino Startersguide from Massimo Banzi (one of the founders of Arduino).
The first example is an example of a LED. I quote him (my own translation from Dutch) "If you want to turn on the led for a longer time, use a resistor"
Well, I have to admit, my last electronics course was in high school. But I know that a resistor brings down the current. Isn't there a way to program my Arduino so it has less current on a digital PIN, so that I don't need a resistor?
Yes, there is a way but you have to be careful. You can configure a pin as an input with internal pullup resistor enabled. The resistor will limit the current to the LED but it is a very small current so you need a very efficient (bright) LED. Subsequently, you can write to the pin using the same digitalWrite() function that you would use if it were configured as an output.
If you ever get careless and configure the pin as an output and set it to a HIGH state, you could blow the LED or damage the microprocessor.
luisilva:
No. The only thing you can program the Arduino to do is to have less VOLTAGE and is in the analog pins not in the digital pins.
??
AFAIK, Arduino has no analog output.
The only way to vary the brightness of a LED without changing the resistor value is to use "AnalogWrite" on PWM pins (which are not analog outputs) , and you don't change the voltage, but the duty cycle of the square wave the pin generates.
luisilva:
No. The only thing you can program the Arduino to do is to have less VOLTAGE and is in the analog pins not in the digital pins.
You still need a resistor in line with the LED to limit the peak current. Never connect an LED direct to a digital output PWM or not, no matter who says so you can't change the laws of physics.
Do I bother you asking why? Is there a technical reason this is not possible?
LEDs (like all diodes) are non-linear.
Below the diode's breakdown voltage, the resistance is high and very little current flows. At the breakdown voltage ("rated voltage for LEDs) the resistance of the diode/LED suddenly drops to nearly zero... If the resistance is too low, too much current flows and you fry the LED and/or your Arduino.
So... We try to control the current through an LED, instead of controlling the voltage... We know that in in a series circuit the current through the resistor is the same current through the LED, and we know the Arduino puts out 5V.
If the LED is rated at 2V we know there is 3V dropped across the series resistor. We use Ohm's Law to calculate the required resistance at the LED's rated current.
(Ohm's Law is valid for the LED, but that doesn't help us because the LED's resistance changes with voltage.)
Carharttguy:
Do I bother you asking why? Is there a technical reason this is not possible? It would make working with an arduino easier, no?
Why what?
DVDdoug has told you about LEDs. If you are asking why you can not output a varying voltage on an arduino then in order to do that you would have to put a digital to analogue converter on the end of every output pin. There are no chips that do that.
The closest you can come is to use a Due, that has two true analogue outputs. However that is no good for driving an LED as the maximum current you should draw from one of those pins is 3mA. Many people have blown the output of their Due by drawing more current.
Actually, I think the output ratings for the AVR are among the most "sly" that I have ever seen. Because although the absolute maximum continuous output appears straightforward at first:
DC Current per I/O Pin ................................................ 40.0mA
It is then mitigated:
This is a stress rating only and
functional operation of the device at these or
other conditions beyond those indicated in the
operational sections of this specification is not
implied. Exposure to absolute maximum rating
conditions for extended periods may affect
device reliability.
hmmm. So "at" 40.0 is not recommended (but I would like to know what a safe limit is).
But the question is not really clarified by the comments on the test conditions (20mA at 5V):
Although each I/O port can sink more than the test conditions (20mA at VCC = 5V, 10mA at VCC = 3V) under steady state
conditions (non-transient), the following must be observed:
ATmega1281/2561:
1.)The sum of all IOL, for ports A0-A7, G2, C4-C7 should not exceed 100mA.
Hmmm.... So a safe limit is somewhere above 20, but not as much as 40, but we won't tell you how much above.
C'mon guys...