Digital I/O: Reading Inputs and Controlling Outputs
Digital I/O is the foundation of interacting with hardware on an Arduino board. Every pin on the board can be configured as either an input (reading a signal) or an output (sending a signal).
You use pinMode() to configure a pin as either INPUT or OUTPUT, then digitalRead() to read its state and digitalWrite() to write to it.
Controlling an LED
To turn an LED on and off, you need:
- An LED
- A current-limiting resistor (typically 220Ω or 330Ω)
- Jumper wires
Circuit: Connect the resistor from pin 13 to the long leg (anode) of the LED, and the short leg (cathode) to GND.
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // Turn LED on
delay(1000); // Wait 1 second
digitalWrite(13, LOW); // Turn LED off
delay(1000); // Wait 1 second
}Reading a Button
To read a button press, you need:
- A pushbutton
- A 10kΩ pull-down resistor (or use the Arduino's built-in pull-up)
Circuit: Connect one leg of the button to pin 2, the other leg to 5V, and add a 10kΩ resistor from pin 2 to GND (pull-down).
const int buttonPin = 2;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);
Serial.print("Button state: ");
Serial.println(buttonState);
delay(100);
}Switch Debouncing
Buttons are "mechanical switches" that bounce between on and off multiple times when pressed. This can cause unreliable readings. The solution is simple debouncing:
int readButton() {
int reading = digitalRead(buttonPin);
delay(20); // Small delay to let contacts settle
int reading2 = digitalRead(buttonPin);
// If readings are the same, return it
if (reading == reading2) {
return reading;
}
// Otherwise, take a third reading
int reading3 = digitalRead(buttonPin);
return reading3;
}Switch Debouncing
Buttons are "mechanical switches" that bounce between on and off multiple times when pressed. This can cause unreliable readings. The solution is simple debouncing:
int readButton() {
int reading = digitalRead(buttonPin);
delay(20); // Small delay to let contacts settle
int reading2 = digitalRead(buttonPin);
// If readings are the same, return it
if (reading == reading2) {
return reading;
}
// Otherwise, take a third reading
int reading3 = digitalRead(buttonPin);
return reading3;
}A 20ms delay after the first reading is usually sufficient for mechanical debounce. For production code, consider using a library like Bounce2 or implementing timer-based debounce.
Key Takeaways
pinMode(pin, INPUT)vspinMode(pin, OUTPUT)configures the pin modedigitalWrite(pin, HIGH)sends 5V;digitalWrite(pin, LOW)sends 0VdigitalRead(pin)reads the pin state as HIGH or LOW- Use
INPUT_PULLUPfor buttons to avoid needing an external resistor - Mechanical switches need debouncing for reliable readings
Resources
- Arduino.cc - Digital I/O: https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/
- TinkerCAD Circuits - Blink: https://www.tinkercad.com/circuits
- Arduino.cc - pinMode: https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/
- Video: Arduino Button and LED Tutorial - https://www.youtube.com/watch?v=5IJD1tXeo3U