Sub topic 2
Setting a Pin as Input and Calling `digitalRead`
This chapter assumes you know the basic Arduino sketch structure, voltage and resistance basics, breadboard wiring, and how digitalWrite() drives an LED. You will use those ideas to read a push button.
Digital input levels
A digital input represents a signal using two logic levels:
HIGHmeans the voltage is in the board's valid high-voltage range.LOWmeans the voltage is in the board's valid low-voltage range.
On a typical 5 V Arduino, a pin near 5 V usually reads HIGH, and a pin near 0 V reads LOW. The exact voltage thresholds depend on the board. HIGH does not always mean exactly 5 V.
A digital input does not measure an exact voltage like analogRead(). It compares the input voltage with its digital thresholds and returns one of two constants: HIGH or LOW.
Configure the pin before reading it
Use pinMode() to define how a pin behaves. For a signal from a button or another circuit, configure the pin as INPUT:
pinMode(buttonPin, INPUT);Then read the current logic level with digitalRead():
int level = digitalRead(buttonPin);The result is an integer containing either HIGH or LOW. You can test it in an if statement.
if (digitalRead(buttonPin) == HIGH) {
// The input is currently high.
}Call pinMode() in setup(), before the first call to digitalRead(). Place the reading code in loop() so the Arduino checks the button repeatedly.
A button circuit with a defined signal
A simple external pull-down circuit connects the button between 5 V and the input pin. A resistor connects the same input pin to GND.
When the button is released, the resistor pulls the pin to GND, so the input reads LOW. When the button is pressed, 5 V reaches the pin, so it reads HIGH.
| Button state | Pin voltage | `digitalRead(buttonPin)` |
|---|---|---|
| Released | Near 0 V | `LOW` |
| Pressed | Near 5 V | `HIGH` |
The resistor prevents a direct short between 5 V and GND when the button is pressed. A value around 10 kΩ is common.
Complete example
This sketch turns on the built-in LED while the button is held down. It uses an external pull-down resistor as shown in the diagram.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonLevel = digitalRead(buttonPin);
if (buttonLevel == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}The program reads a level, not an event. If you hold the button down, every pass through loop() reads HIGH and keeps the LED on. Later, you can detect a change from released to pressed when you need one action per press.
Common mistakes
- Reading a pin number different from the one used in the wiring.
- Forgetting
pinMode(buttonPin, INPUT). - Connecting the button to GND while expecting a pull-down circuit that needs 5 V.
- Using
INPUTwithout any resistor, causing a floating input. - Assuming every board uses the same voltage thresholds.
Exercise
In the example circuit, what should the program read when the button is released, and why?
Show answer
It should read LOW because the pull-down resistor connects the input pin to GND while the button is open.