Sub topic 8
Digital pins: reading and driving HIGH and LOW
This chapter assumes you know basic voltage, current, and resistance, and that you can write a sketch with setup() and loop(). It covers how a digital pin on the Uno R3 works as an input and as an output, and which mistakes cause most wiring and code problems.
What a digital pin is
A digital pin connects the ATmega328P microcontroller to the outside world through one of two states. On a 5V Uno:
HIGHmeans the pin sits near 5V (as an output) or is read as a 1.LOWmeans the pin sits near 0V, which is GND (as an output), or is read as a 0.
The Uno has 14 digital pins, numbered 0 to 13. The analog pins A0 to A5 can also act as digital pins. Use them with pinMode(A0, OUTPUT) and digitalRead(A0), or by their numbers 14 to 19.
Every digital pin is either an input or an output at any moment. You choose the direction with pinMode(), and you can change it while the sketch runs.
| Mode | Meaning | Typical use |
|---|---|---|
| `INPUT` | High impedance, the pin only senses voltage | Sensors, external pull-up or pull-down circuits |
| `INPUT_PULLUP` | Input with an internal pull-up resistor enabled | Buttons and switches wired to GND |
| `OUTPUT` | The pin actively drives HIGH or LOW | LEDs, transistor gates, relay driver inputs |
Driving a pin as an output
When you call pinMode(pin, OUTPUT), the microcontroller connects the pin to an output driver. digitalWrite(pin, HIGH) connects the pin to the 5V supply inside the chip. digitalWrite(pin, LOW) connects it to ground.
This sketch blinks the built-in LED on pin 13. It is also a quick check that an upload over USB worked.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}LED_BUILTIN is a constant the board definition sets to 13 on the Uno. Using the constant keeps the sketch portable to boards where the LED is on another pin.
Sourcing and sinking
An output can push current out of the pin or pull current into it. The two wiring styles are:
- Sourcing: the pin is HIGH, current flows out of the pin through the load to GND.
- Sinking: the load connects between 5V and the pin, and the pin is LOW. Current flows from 5V through the load into the pin.
The ATmega328P handles both in the same way. Each pin is rated for 20 mA as a practical limit, with 40 mA as an absolute maximum. The next chapters on current limits cover the full budget. For now, always put a series resistor on an LED. A 220 Ω to 330 Ω resistor from a 5V pin gives roughly 10 to 14 mA with a typical red LED, which is bright and safe.
What `digitalWrite` does to an input pin
A common trap is calling digitalWrite(pin, HIGH) on a pin that is set as INPUT. On AVR boards this does not drive the pin. It enables the internal pull-up resistor. This is the old way to get INPUT_PULLUP. It still works, but it is confusing to read, so use pinMode(pin, INPUT_PULLUP) instead.
Reading a pin as an input
digitalRead(pin) returns HIGH or LOW. The chip does this by comparing the pin voltage with fixed thresholds. For a 5V ATmega328P:
- A voltage above about 3.0V reads as
HIGH. - A voltage below about 1.5V reads as
LOW. - A voltage between them is undefined. The result can be either value and may change from one read to the next.
Your wiring must therefore always pull the pin firmly toward one state. A pin connected to nothing is called a floating input. It picks up noise from nearby wires, your hand, and other pins, and it reads random values. The next chapter treats floating inputs in detail. The short rule is: an input needs a defined path to 5V or to GND at all times.
A button with INPUT_PULLUP
The internal pull-up is a resistor of roughly 20 kΩ to 50 kΩ between the pin and 5V. With it enabled, an unconnected pin reads HIGH. Wire a button between the pin and GND. Pressing the button connects the pin to GND and the pin reads LOW.
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
bool pressed = (digitalRead(buttonPin) == LOW);
digitalWrite(ledPin, pressed ? HIGH : LOW);
}The logic is inverted: pressed means LOW. The variable name pressed hides that inversion, so the rest of your code can think in terms of "pressed" and "released".
Reading the state of an output pin
digitalRead() also works on a pin that is set as OUTPUT. It returns the level the pin was last told to drive. This lets you toggle an LED without a separate variable:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(500);
}If a heavy load pulls the output away from the level you set, for example a short circuit, the read value can still report what the code asked for, so do not use it as a measurement of the pin voltage.
Changing direction at runtime
You can call pinMode() again at any time. Some designs share one pin between two roles, such as a single wire that is an output while sending and an input while listening. Two points matter:
- When a pin switches from
INPUTtoOUTPUT, it starts at whatever level the output register holds. On AVR, that is the last value written withdigitalWrite, or LOW after reset. Write the level you want first, then switch the mode, to avoid a brief glitch on an attached device. - Never connect two outputs together and drive them to opposite levels. One pushes 5V while the other pulls to GND, and a large current flows between them that can damage both pins.
State at power-up and reset
After a reset, every pin is an input with no pull-up. A pin that drives a relay or motor driver is therefore floating until your setup() runs, and the bootloader runs first, which adds a short delay. For circuits where a floating pin could cause harm, add an external pull-down resistor (for example 10 kΩ to GND) so the load stays off until the sketch takes control.
Pins 0 and 1 are also wired to the USB-to-serial bridge. If you use them as ordinary digital pins, a connected circuit can interfere with uploads and with Serial. Treat them as reserved while you use the Serial Monitor.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Button reading flickers randomly | Input has no pull-up or pull-down, so it floats | Use `INPUT_PULLUP` or add an external resistor |
| LED never turns on | Missing `pinMode(pin, OUTPUT)`, or the LED is reversed | Set the mode in `setup()`; the long leg (anode) goes toward the resistor and pin |
| LED is very dim | Pin still set as `INPUT` with `digitalWrite(HIGH)`, which only enables the weak pull-up | Set `OUTPUT` |
| Button logic seems backwards | Using `INPUT_PULLUP` inverts the reading | Compare against `LOW` for pressed |
| Upload fails after wiring pins 0 and 1 | The circuit loads the serial lines | Disconnect it during upload, or use other pins |
| Pin or board becomes hot | Output shorted to GND or 5V, or no LED resistor | Power off, find the short, add a resistor |
Summary of working rules
- Set the direction with
pinMode()insetup()before you read or write the pin. - Outputs drive 5V or 0V. Limit current to about 20 mA per pin, and use a series resistor with LEDs.
- Inputs need a defined level. Use
INPUT_PULLUPand wire switches to GND. - Treat pins 0 and 1 as reserved for Serial while the board is connected to a computer.
- Do not connect two outputs together.
Exercise
A button is wired between pin 4 and GND, and the sketch calls pinMode(4, INPUT_PULLUP). What does digitalRead(4) return while the button is not pressed, and what does it return while the button is held down? Explain why.
Show answer
It returns HIGH when the button is released, because the internal pull-up resistor holds the pin at about 5V and nothing else drives it. It returns LOW while the button is held, because the closed button connects the pin directly to GND, and that low-resistance path overrides the pull-up of roughly 20 kΩ to 50 kΩ.
Exercise
You want an LED on pin 8 to stay off from the moment power is applied until your sketch decides to turn it on. The LED is wired from pin 8 through a 330 Ω resistor to GND. Is it safe to rely on pinMode(8, OUTPUT) in setup() alone? What happens before setup() runs, and does it matter for this LED?
Show answer
Before setup() runs, including during the bootloader delay, pin 8 is an input with no pull-up, so it floats. With an LED and resistor to GND, a floating pin delivers almost no current, so the LED will stay off or at most glow faintly from noise. It is acceptable for this LED. For a load that could cause harm if it switched on briefly, such as a relay or motor driver, add an external pull-down resistor of about 10 kΩ from the pin to GND.