ATmega328P/Digital pins/16. digitalWrite and the pin-13 lamp
Digital pins · 16 of 33

digitalWrite and the pin-13 lamp

digitalWrite(13, HIGH) joins pin 13 to the 5 V rail inside the chip, and LOW joins it to ground. A lamp on the board shows the pin's state: on the UNO through a buffer that takes none of the pin's current, on the Nano straight off the pin for about 0.6 mA.

Your board

Two lines make a pin an output

Every digital pin starts as an input when the chip resets. pinMode(13, OUTPUT) turns on the pin's driver, and from then on the pin is always driven: digitalWrite(13, HIGH) connects it to the 5 V rail inside the ATmega328P, and digitalWrite(13, LOW) connects it to ground. Nothing else changes the pin until the sketch writes to it again.

The names HIGH and LOW are the numbers 1 and 0, and digitalWrite treats any other non-zero value as HIGH.

What HIGH is, in volts

With nothing taking current from it, a HIGH pin sits at the supply, near enough: on USB power, close to 5 V. It drops as you load it. The ATmega328P datasheet promises at least 4.1 V while the pin gives 20 mA, and at most 0.8 V on a LOW pin taking 20 mA in. Those are the guarantees the next article leans on.

The lamp on pin 13, and who pays for it

From digitalWrite(13, …) to the L lamp
HIGH
The last digitalWrite on pin 13
D13
about 5 V
Current from D13
almost none
Lamp current
at most 3 mA
HIGH connects the pin to the 5 V rail inside the chip. The LM358 copies that voltage and pays for the lamp out of its own supply, so D13 delivers almost no current and its full 20 mA budget is still yours.

The two boards light their pin-13 lamp in different ways, and the difference matters the day you use D13 for something else.

On the UNO, D13 feeds the input of an LM358 op-amp wired as a voltage follower, an amplifier whose output copies its input. The follower's output, not D13, lights L through R10, a 1 kΩ resistor. D13 gives almost no current to the lamp, so its whole budget is still available, and it still works as an input: the follower's input takes almost nothing, so it does not hold the pin up or down. The follower is an analogue part, not a logic gate, so a floating D13 can make L glow part-way, copying a pin that is neither HIGH nor LOW.

On the Nano there is no buffer. The lamp, marked D13 on the silkscreen, hangs on the pin itself through R4, 5.1 kΩ, to ground. A HIGH pin pays for it: about (5 − 2.0) V / 5.1 kΩ = 0.6 mA, a few percent of its 20 mA, which an output never notices. An input does. The lamp and R4 stay on the pin as a path to ground, so on the Nano D13 is a poor choice for a button or a sensor. Why the internal pull-up cannot hold it HIGH is in INPUT_PULLUP; a pin left floating is the subject of digitalRead and the floating input.

Writing to a pin that is not an output

digitalWrite(pin, HIGH) on a pin left as an input does not drive it. It turns on the pin's internal pull-up, 20 to 50 kΩ to 5 V, which is what INPUT_PULLUP does on purpose. A Blink sketch that forgot pinMode lights the lamp dimly or not at all, and the missing line is the reason.

The code

Blink_And_Say.ino

Sets D13 as an output, then switches it HIGH and LOW once a second and prints each change. Open the Serial Monitor at 9600 baud and the line and the lamp change together.

// Blink_And_Say.ino — lights the pin-13 lamp, then dark, and says so
// 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. The lamp is L on the UNO (behind an LM358
//   buffer) and D13 on the Nano (straight off the pin).

const byte LAMP = 13;   // LED_BUILTIN is 13 on both boards

void setup() {
  pinMode(LAMP, OUTPUT);   // driven from now on, never floating
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LAMP, HIGH);   // D13 joined to the 5 V rail
  Serial.println("D13 HIGH: lamp on");
  delay(1000);

  digitalWrite(LAMP, LOW);    // D13 joined to ground
  Serial.println("D13 LOW: lamp off");
  delay(1000);
}

Nothing is wired. Pin 13 is the lamp's pin, L on the UNO and D13 on the Nano, and the Arduino core names it LED_BUILTIN on both, so any Blink sketch lights the same lamp.

When it does not work

The pin-13 lamp stays dark, or stays lit, after the upload

Check the upload finished with Done uploading, then that the sketch calls pinMode(13, OUTPUT) in setup(). Without it, digitalWrite(13, HIGH) only turns on the pin's weak internal pull-up, and the lamp may light dimly or not at all.

The pin-13 lamp is already blinking before I upload anything

That is the sketch already on the chip, usually a Blink from the factory test. Uploading anything replaces it. The lamp also usually blinks a few times at every reset while the bootloader runs, which is normal.

The Serial Monitor shows nothing, or rubbish

Set the baud rate box at the bottom of the Serial Monitor to 9600, the number in Serial.begin(9600). A mismatched rate prints garbled characters; no port selected prints nothing.

I wrote to a different pin and nothing lit

Only D13 has a lamp. Writing HIGH to any other pin sets it to 5 V correctly, but there is nothing on the board to show it. A multimeter between that pin and GND will read close to 5 V.

Where this goes next

20 mA is the figure to design to, 40 mA is the damage line, and how to size an LED's resistor.

How much a pin can give →

Edit this page — content/books/atmega328p/digital-output.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.

Browse Modules and blocks on the forum →