How to Read a Switch Input with digitalRead() on Arduino
How to Read a Switch Input with digitalRead() on Arduino
This guide explains how to connect a switch to Arduino and write code to detect whether the switch is pressed or not. You’ll practice using digitalRead(), a fundamental command for almost every Arduino project that accepts user input.
How digitalRead() Works
Arduino digital pins can be configured as inputs or outputs. When set as input, use digitalRead(pin) to check if that pin has voltage present.
The command returns one of two values:
- HIGH → Voltage present (around 5V)
- LOW → No voltage (0V)
Basic Pull-down Circuit
A normally-open (NO) pushbutton doesn’t hold a stable state when not pressed. This leaves the input pin floating with no defined voltage, leading to erratic readings. The solution is to add a pull-down resistor as shown below:
Wiring steps:
- Connect one leg of the switch to 5V
- Connect the other leg to an Arduino digital pin (e.g., D2)
- Connect a 10kΩ resistor between that Arduino pin and GND (this is the pull-down)
Operation:
- Switch open → current flows through resistor to GND → reading is LOW
- Switch pressed → 5V flows into Arduino pin → reading is HIGH
Code: Read Switch Input to Control LED
// Pin definitions
const int buttonPin = 2; // Switch connected to pin 2
const int ledPin = 9; // LED connected to pin 9
// Variable to store switch state
int buttonState = 0;
void setup() {
// Start Serial Monitor for debugging
Serial.begin(9600);
// Set LED pin as output
pinMode(ledPin, OUTPUT);
// Set switch pin as input
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the state of the switch pin
buttonState = digitalRead(buttonPin);
// Check if switch is pressed (HIGH)
if (buttonState == HIGH) {
// Switch pressed → LED turns on
Serial.println("LED ON");
digitalWrite(ledPin, HIGH);
} else {
// Switch released → LED turns off
Serial.println("LED OFF");
digitalWrite(ledPin, LOW);
}
}
What this code does:
- Continuously reads pin D2 using
digitalRead() - If it reads HIGH (switch pressed) → turns the LED on
- If it reads LOW (switch released) → turns the LED off
- Prints the state to Serial Monitor for verification
Customization Points
| Adjustment | Default | How to change |
|---|---|---|
| Switch pin | 2 | Use any available digital pin |
| LED pin | 9 | Change to another pin or swap LED for Relay, Buzzer, etc. |
| Resistor config | Pull-down | Switch to Pull-up to invert logic |
Test Using Serial Monitor
After uploading the code, open Serial Monitor (baud rate 115200). You should see the text change every time you press or release the switch. This confirms Arduino is reading the input correctly.
Key Takeaways
digitalRead() is a core command you need to master before building Arduino projects that accept input from switches, sensors, or other devices. Understanding pull-down and pull-up resistor logic lets you connect a wide variety of input devices reliably.
Reference Video
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย