Sub topic 3
Driving a Pin with `pinMode()` and `digitalWrite()`
Assume you know what a program and a variable are, and that you have an Arduino-compatible board, a USB cable, and the Arduino IDE installed. This chapter focuses on digital output. A digital output pin can produce one of two logic states: HIGH or LOW.
What a Digital Pin Does
A microcontroller pin is an electrical connection controlled by the program. When configured as an output, the pin can drive a connected component:
HIGHsets the pin to approximately the board's operating voltage.LOWsets the pin near 0 volts.- The exact voltage depends on the board. A 5 V Arduino commonly produces about 5 V for
HIGH; a 3.3 V board produces about 3.3 V.
A digital pin is different from an analog output. A digital output does not directly produce every voltage between zero and the supply voltage. It switches between two logical states.
Before using a pin, configure its role with pinMode():
pinMode(pinNumber, mode);For an output, use OUTPUT as the mode. Then use digitalWrite() to select its state:
digitalWrite(pinNumber, state);The state is normally HIGH or LOW.
Configure the Pin in `setup()`
An Arduino sketch has two important functions:
setup()runs once after the board starts or resets.loop()runs repeatedly aftersetup()finishes.
Put pin configuration in setup(). This makes the pin's intended role clear and ensures it is configured before the repeated work begins.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}LED_BUILTIN is a predefined name supplied by the board's Arduino core. It identifies the pin connected to the board's built-in LED. Using this name makes the sketch more portable than assuming a particular pin number.
The first call to digitalWrite() turns the LED on. The program then waits for 1,000 milliseconds, or one second. The second call turns the LED off, and the program waits again. After that, loop() starts over.
The sequence is:
flowchart TD
A[Board starts] --> B[setup runs once]
B --> C[Configure LED pin as OUTPUT]
C --> D[loop begins]
D --> E[Write HIGH]
E --> F[Wait one second]
F --> G[Write LOW]
G --> H[Wait one second]
H --> D`HIGH` and `LOW` Control the Output
digitalWrite() changes the electrical state of the selected output pin. It does not mean “turn on any device automatically.” The connected circuit determines what the state does.
For a typical LED circuit:
| Pin state | Pin voltage | LED result |
|---|---|---|
| `HIGH` | Near the board supply voltage | Usually on |
| `LOW` | Near 0 V | Usually off |
The word “usually” matters. Some components are active-low: they turn on when their control pin is LOW. Also, a board's built-in LED may be wired with inverted behavior on some boards. Follow the board documentation if HIGH appears to turn the LED off.
Use Named Constants for External Pins
When driving an external component, store the pin number in a named constant. A constant is a value that the program does not change while it runs.
const uint8_t LED_PIN = 8;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}This sketch uses digital pin 8. uint8_t is an unsigned integer type that can hold small non-negative values. The declaration gives the pin a meaningful name and puts the hardware choice in one place.
Changing LED_PIN to another suitable digital pin changes which pin the sketch controls. The circuit must be moved to the same pin.
Common Errors
If the output does not behave as expected, check these points:
- The pin number in the program matches the wire's pin.
pinMode()runs beforedigitalWrite().- The mode is
OUTPUT, notINPUT. - The sketch was uploaded to the intended board and port.
- The LED and its resistor are connected correctly.
- The board has power and the USB cable supports data.
- The selected pin is available on your board and is not reserved for another function.
A pin left in its default INPUT mode does not actively drive the circuit. Calling digitalWrite() on an input has a different meaning: it controls an internal pull-up resistor on many Arduino boards. That is not the output behavior used in this chapter.
Exercise
What two lines are required to make digital pin 8 drive an LED, and where should each line go?
Show answer
Put pinMode(8, OUTPUT); in setup() to configure the pin. Put digitalWrite(8, HIGH); or digitalWrite(8, LOW); in loop() or another function to choose its output state.