I was told that even when connecting LED to a digital IO pin with resistor, it's dangerous for Arduino, and that I should supply power from the power rails and use a transistor to toggle current flow to the LED.
This seems counter to what everyone tells you and I couldn't find any other mentions online of this being the truly correct way of going about it.
If we take @LarryD's example and choose a resistor to give a current if 20mA, if the Arduino is, for example, a Due, then the absolute maximum current for most pins is 15mA and could be damaged. For a few of Due's pins, the limit is only 3mA.
Many older, 5V Arduino models based on AVR chips have quite high absolute limits of 40mA per pin. But many more modern Arduino models, which are mostly 3.3V, can have much lower absolute limits.
It's just a simple circuit of Uno, 470ohm resistor, generic red LED, back to ground. The simplest of demo circuits.
Of course, I can understand if the resistor is has too low a value, it's the same issue as using no resistor at all - too much current will be allowed to flow. It just seemed surprising to me that in this case, I would be suggested to use a whole transistor setup? I thought that the risk of burning components was already completely mitigated in this scenario by using the correct resistor value, that has been long decided by the community/tutorials/documentation?
The advice to use a simple resistor to limit the current to an LED vs the use of a transistor to switch an external power supply needs to be seen in context
There is a danger that a user reads that using a resistor is safe then attaches an LED and resistor to multiple pins on an Arduino and in doing so exceeds the capacity of the Arduino to provide enough current to power them all.
So yeah, for tinkering with a single indicator light, it's not necessary. But when using many LEDs, or LEDs that may draw more than 20-30mA, then we should be supplying power from the 5V rail or an external power source instead.
What is the power rail? If you're referring to the power rail on the Uno, it has its limitations, either USB or the voltage regulator when powering it on Vin.
There are three specs for the 328P (not the Uno specific) relating to what you're talking about
Current per pin; absolute maximum 40 mA.
Current per port (a group of pins): should not exceeds 100 mA.
Current through Vcc or GND; should not exceeds 200 mA.
Led voltage drop depends on color: https://www.electricalengineering.xyz/led-voltage-drop-by-color/
Let's say you have a green LED powered by 5 volts and want an 8mA (0.008 Amp) current, a green LED has a voltage drop of about 3.5V, subtract that from supply voltage, 5 - 3.5 = 1.5, 1.5 divided by 0.008 = 187, use the closest standard resistor value, 180Ω.
1.5V / 180Ω = 0.0083 Amps = 8.3mA.
What is the power rail? If you're referring to the power rail on the Uno, it has its limitations, either USB or the voltage regulator when powering it on Vin.
Yeah I know the Arduino 5v pin can't handle that much current either, like 500mA limit, so if we're using enough LED that we need to draw more than that, we should not be powering them through Arduino 5V at all.
Maybe I haven't made myself clear here. I know individual IO pins have a limit, and total draw from all IO pins also has a limit. I'm just saying that I didn't think it was a bad idea to power one or two LEDs directly from an IO pin (one pin per LED obviously), given that I'm using the correct resistor value in the range of 220-470 ohms as is often recommended to do. I was just surprised at being advised otherwise and wanted to see what this forum thought.
I was just showing a circuit I was using for a test (one RGB led and a rotary encoder) to a friend, and he told me that. Looking at the responses here, I think he was speaking more broadly and less about the simple circuit I had made.