I have a requirement to use all digital pins on my arduino mega, mostly for powering simple LEDs.
So is this kind of loading troublesome?
Also in this case what value of resistor do I use for my LED.?
I am connecting one LED per port. using a total of 40 LEDs for 40 ports.
Each individual pin can handle a MAXIMUM of 40mA but prolonged current draw at this limit will maybe damage the pin over time so keep it below 20mA to be safe.
The entire power limits for the Mega chips are 200mA so if you lit all 40 LED's at the same time at 10mA that would be twice the power limit of the chip (40x10=400).
I would suggest you find a better way of connecting the LED's, something like the MAX7219 chip can drive 64 LED's and only use 3 pins on an Arduino to control them. The MAX7219 chip drives the LED's using multiplexing to only a few LED's are on at one time but it turns them on and off so fast they all look to be lit at the same time. You can turn on/off all 64 LED's individually of dim all at the same time in 15 brightness stepps.
Also, it is advised to use 10k ohm resistor when interfacing a switch.
What happens if i use say 2.2k ohm?
same with LED, it says use 220ohm while interfacing LED.
Ohm's law is what you want here.
To answer your example above a 10k resistor on a 5V arduino pin will limit the current to 0.0005 amps (5/10000) so the current will be very small but enough to register on an arduino pin.
If you use a 2.2K then the current will be about 0.0023 amps (5/2200) so this will still be safe.
A milliamp (mA) is 1000th of an amp so the 0.0023 amp is 2.3mA.
If you arrange that half the LED pins are sourcing to ground and half are sinking from 5V, then you
share the LED current load between Vcc and ground pins, allowing 400mA max of LEDs in total.
The chip limits are to each of the GND and Vcc rails.
Of course your code would need to know to drive half the LEDs with reverse logic.
In your OP you confuse PORT and PIN. On ATmega chips there are upto 8 pins per port. See
the datasheet for more information.