How much a pin can give
Design every pin to 20 mA, the current the datasheet tests at. 40 mA a pin and 200 mA for the whole chip are absolute maximums, where damage can start, and the pins share smaller totals in groups. Here is the arithmetic for an LED's resistor, before you add one.
Two numbers, and they are not the same kind
The ATmega328P datasheet gives two figures for a pin, and they answer different questions.
20 mA is the current the datasheet tests at. At 20 mA it promises a HIGH pin stays above 4.1 V and a LOW pin below 0.8 V. That is the number to design to.
40 mA a pin and 200 mA through the chip's power and ground pins are absolute maximums. The datasheet says going past them may cause permanent damage, and that sitting at them for long may affect reliability. They are the line not to reach, not a rating to run at.
The groups share a smaller total
The pins are wired inside the chip in groups, and each group has a ceiling of its own (the datasheet's DC characteristics, notes 3 and 4):
| Pins | Sinking (pin LOW) | Sourcing (pin HIGH) |
|---|---|---|
| A0 to A5 | 100 mA together | 150 mA, shared with D0 to D4 |
| D0 to D4 | 100 mA together | 150 mA, shared with A0 to A5 |
| D5 to D13 | 100 mA together | 150 mA together |
So eight LEDs at 20 mA each on D5 to D13 is 160 mA, past the 150 mA that group may source, even though every pin is within its own 20 mA. The 200 mA for the whole chip covers everything at once, the chip's own running current included. The Nano's A6 and A7 are analogue inputs with no output driver, so they give nothing and belong to no group.
Sizing an LED's resistor
An LED does not limit its own current. It holds a forward voltage, about 2.0 V for red and 3.1 V for blue or white, and takes whatever current the rest of the circuit allows. A resistor in series sets that current:
R = (5 V - forward voltage) / current
= (5 - 2.0) / 0.010 = 300 ohm for a red LED at 10 mARound up to the next value you have, never down: a bigger resistor only makes the LED dimmer. Most indicator LEDs are plainly visible at 5 to 10 mA, which leaves room in the groups for everything else. The table in the sketch below does the same sum for eight common values.
What the board already spends
On the UNO, L costs D13 nothing: the LM358 follower powers it, as digitalWrite and the pin-13 lamp shows. The TX and RX lamps hang from the 5 V rail through 1 kΩ onto D1 and D0, so each lets about 3 mA, (5 - 2.0) V / 1 kΩ, into its line while that line is LOW: the ATmega328P sinks the TX lamp's, the CH340C the RX lamp's.
On the Nano, the D13 lamp is part of D13's budget. It hangs on the pin through 5.1 kΩ, so a HIGH D13 gives it about 0.6 mA, (5 - 2.0) V / 5.1 kΩ, and that counts toward the D5 to D13 group as well. The TX and RX lamps sit on the CH340C's side of the 1 kΩ resistors in D0 and D1, from 5 V through 5.1 kΩ. While D1 is LOW the TX lamp's current runs through both resistors into it, about half a milliamp; the RX lamp's goes into the CH340C.
On both boards D0 and D1 belong to the serial line anyway. On every other pin the 20 mA is yours, less 0.6 mA on D13 of a Nano.
The code
Prints the current a red LED would draw from a HIGH pin through each common resistor value, and flags any over 20 mA. Change VF to 3.1 for a blue or white LED and upload again.
// Led_Resistor_Table.ino — prints LED current for common resistors
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Wiring: none. This sketch only does the arithmetic.
//
// R = (supply - Vf) / I, so I = (supply - Vf) / R.
// Design to 20 mA a pin. 40 mA is the absolute maximum, not a rating.
const float SUPPLY = 5.0; // a pin set HIGH, near enough
const float VF = 2.0; // a red LED; about 3.1 for blue or white
const int R[] = {100, 150, 220, 330, 470, 680, 1000, 2200};
void setup() {
Serial.begin(9600);
Serial.print("LED forward voltage: ");
Serial.print(VF);
Serial.println(" V");
for (byte i = 0; i < sizeof(R) / sizeof(R[0]); i++) {
float mA = (SUPPLY - VF) / R[i] * 1000.0;
Serial.print(R[i]);
Serial.print(" ohm -> ");
Serial.print(mA, 1);
Serial.print(" mA");
if (mA > 20.0) Serial.print(" too much: pick a bigger one");
Serial.println();
}
}
void loop() {
}The sketch uses 5 V for the pin. A real pin sags a little under load, so the true current is slightly lower than the table says, which is the safe direction to be wrong in.
When it does not work
With no resistor, the LED takes as much current as the pin can push, far past 20 mA. The LED is usually the part that dies, and the pin may be damaged too. Test the pin with Blink_And_Say pointed at it and a meter before trusting it again.
Add up the currents. Pins share 100 mA sinking and 150 mA sourcing per group, and the whole chip shares 200 mA at most through its power pins. Bigger resistors, or a transistor per load, bring the total down.
That is the pin's own resistance showing. The datasheet allows a HIGH pin to fall to 4.1 V while it gives 20 mA. It is not a fault, and the current is a little lower than the plain arithmetic predicts.
Those need far more than 20 mA, and a motor or relay coil also kicks back a voltage spike when it switches off. Drive them through a transistor or a driver board, with the pin only switching it.
A pin nothing is holding reads noise, and a fingertip is enough to show it.
digitalRead and the floating input →Edit this page — content/books/atmega328p/what-a-pin-can-give.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.