Connecting LED

Hello i have arduino mega 2560. if i will connect led to the digital pin( the pwm pin) and to the 5v pin, it wil be okay? no risk for the arduino?

Thank you

. if i will connect led to the digital pin( the pwm pin) and to the 5v pin, it wil be okay?

No.

You need a resistor in line with the LED as well.

it wil be okay?

No. The PWM pin supplies power. The other leg of the LED goes to ground, not power.

And if i will connect the 5v pin to resistor and the resistor to the led and the led to the digital pin (not pwm), it will be okay (no risk to the arduino)?
if yes, i can connect the pwm signal ( instead the digital signal) ? i mean 5v in line to resistor than led than pwm signal.

Thank you,

PaulS: No. The PWM pin supplies power. The other leg of the LED goes to ground, not power.

You can do it either way Paul. You can put the led between the pin and +5V if you want, with an appropriate current limiting resistor of course. In that case you use digitalWrite LOW to turn the LED on and HIGH will turn it off. analogWrite values close to 0 make it mostly on and values close to 255 make it mostly off. The led doesn't care if you are switching the 5V side or the ground side of it. When you are multiplexing or charlieplexing leds this is what allows you to put two leds between the same two pins.

You can even have your led between two digital pins and write one HIGH and one LOW to turn it on.

Just like HIGH doesn't always have to mean a closed switch, it doesn't always have to mean a lit led either.

maor444: And if i will connect the 5v pin to resistor and the resistor to the led and the led to the digital pin (not pwm), it will be okay (no risk to the arduino)? if yes, i can connect the pwm signal ( instead the digital signal) ? i mean 5v in line to resistor than led than pwm signal.

Thank you,

Yes, as long as you have a current limiting resistor there is no risk.

Having the resistor / LED between the 5V and the pin ( any output pin ) is called sinking the current. Connecting between a pin and ground is called sourcing the current. Either are acceptable. In fact sinking is the normal way of doing things because with most processors you can sink more current that you can source.

The Arduino's 8 bit processors have equal source and sink capability, where as the 32 bit processors used in the Due and Zero you are back to the asymmetrical source and sink capability and at much lower currents.

The Arduino's 8 bit processors have equal source and sink capability,

Not exactly. Datasheet ATmega 48/88/168/328p Edition : Rev. 8271H – 08/2013

Page : 301

  1. Although each I/O port can source more than the test conditions (20mA at VCC = 5V, 10mA at VCC = 3V) under steady state conditions (non-transient), the following must be observed: ATmega48A/PA/88A/PA/168A/PA/328/P: 1] The sum of all IOH , for ports C0 - C5, D0- D4, ADC7, RESET should not exceed 150mA. 2] The sum of all IOH , for ports B0 - B5, D5 - D7, ADC6, XTAL1, XTAL2 should not exceed 150mA. If IIOH exceeds the test condition, VOH may exceed the related specification. Pins are not guaranteed to source current greater than the listed test condition.

  2. 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: ATmega48A/PA/88A/PA/168A/PA/328/P: 1] The sum of all IOL, for ports C0 - C5, ADC7, ADC6 should not exceed 100mA. 2] The sum of all IOL, for ports B0 - B5, D5 - D7, XTAL1, XTAL2 should not exceed 100mA. 3] The sum of all IOL, for ports D0 - D4, RESET should not exceed 100mA. If IOL exceeds the test condition, VOL may exceed the related specification. Pins are not guaranteed to sink current greater than the listed test condition.

As you can see for AtMega, per PORT, max source current is 150 mA and max sink current is only 100 mA .

But there is another very important maximum current limit : Absolute Maximum Rating :

DC Current VCC and GND Pins. . . . . . . . . . . 200.0mA

Important notice from datasheet page 300 :

Stresses beyond those listed under “Absolute Maximum Ratings” may cause permanent damage to the device. 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.

As you can see for AtMega, per PORT, max source current is 150 mA and max sink current is only 100 mA .

Yes but per pin it is symmetrical.

@68tjs You are over simplifying things and missing some vital points.

But there is another very important maximum current limit : Absolute Maximum Rating : Quote

DC Current VCC and GND Pins. . . . . . . . . . . 200.0mA

There are two Vcc and two GND pins on an Atmega 328, the analogue Vcc and ground and the Digital Vcc and ground. This makes that current rating 400mA.

Note also that a mixture of sourcing and sinking will allow you to get more current out of any one port.