Activity 4.2.2 — Arduino Digital I/O & Basic Projects¶
Learning Objectives¶
By the end of this lesson, students will be able to:
- Set up the Arduino IDE and connect the board
- Write and upload basic Arduino programs
- Control digital outputs (LEDs)
- Read digital inputs (pushbuttons)
- Create simple projects combining inputs and outputs
- Debug basic Arduino programs
Vocabulary¶
Vocabulary (click to expand)
| Term | Definition |
|---|---|
| Sketch | An Arduino program file |
| Upload | Transferring code from computer to Arduino |
| Serial Monitor | A window to send/receive text to Arduino |
| Pin Mode | Configuration for INPUT or OUTPUT |
| Pull-up Resistor | Keeps input HIGH when not connected |
| Debounce | Handling switch bounce with software delay |
Part 1: Setting Up Arduino¶
Step 1: Install Arduino IDE¶
- Download from arduino.cc
- Install the application
- Launch Arduino IDE
Step 2: Connect Your Board¶
- Connect Arduino Uno to computer via USB cable
- Windows: Drivers should install automatically
- Mac: No driver needed
- Linux: Usually works automatically
Step 3: Select Board and Port¶
In Arduino IDE: 1. Tools → Board → Arduino Uno 2. Tools → Port → Select the COM port (should show Arduino)
Key insight: If no port shows, try a different USB cable. Some cables are charge-only!
Part 2: Blink - Your First Program¶
The "Blink" Sketch¶
This is the "Hello World" of Arduino:
// Blink: Turns LED on for 1 second, off for 1 second
// Pin 13 has a built-in LED on most Arduino boards
int ledPin = 13;
// setup() runs once when Arduino powers up or resets
void setup() {
// Configure pin 13 as an OUTPUT
pinMode(ledPin, OUTPUT);
}
// loop() runs continuously forever
void loop() {
digitalWrite(ledPin, HIGH); // Turn LED ON (HIGH = 5V)
delay(1000); // Wait 1000 milliseconds (1 second)
digitalWrite(ledPin, LOW); // Turn LED OFF (LOW = 0V)
delay(1000); // Wait 1 second
}
Upload the Sketch¶
- Type or paste the code into the editor
- Click the Upload button (right arrow →)
- Wait for "Done uploading" message
- Watch the LED on pin 13 blink!
Understanding the Code¶
| Function | What it does |
|---|---|
pinMode(pin, OUTPUT) |
Configures pin to send signals OUT |
digitalWrite(pin, HIGH) |
Sets pin to 5V (ON) |
digitalWrite(pin, LOW) |
Sets pin to 0V (OFF) |
delay(milliseconds) |
Pauses program for specified time |
Part 3: Digital Output - External LED¶
Circuit¶
Connect an external LED to Arduino:
Code to Control External LED¶
int ledPin = 9; // Using PWM-capable pin
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // LED on
delay(500); // Half second
digitalWrite(ledPin, LOW); // LED off
delay(500); // Half second
}
Important Notes¶
- Resistor: Always use a resistor (220Ω-1kΩ) with LEDs to limit current
- LED polarity: Long leg is positive (+), short leg is negative (-)
- PWM pins: Pins marked with ~ can dim LEDs (covered in next lesson)
Part 4: Digital Input - Pushbutton¶
Circuit¶
Connect a pushbutton:
Basic Button Code¶
int buttonPin = 2; // Pushbutton connected to pin 2
int ledPin = 13; // LED on pin 13
int buttonState = 0; // Variable to store button state
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the button state
buttonState = digitalRead(buttonPin);
// If button is pressed (HIGH), turn LED on
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Using Internal Pull-up Resistor¶
Arduino has built-in pull-up resistors:
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up
}
void loop() {
// Button pressed = LOW, released = HIGH (inverted!)
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH); // LED on when pressed
} else {
digitalWrite(ledPin, LOW);
}
}
Part 5: Project 1 - Traffic Light Controller¶
Requirements¶
Create a traffic light that cycles: - Green: 5 seconds - Yellow: 2 seconds - Red: 5 seconds
Repeat indefinitely.
Circuit¶
Code¶
int greenPin = 9;
int yellowPin = 10;
int redPin = 11;
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
}
void loop() {
// Green light
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, LOW);
delay(5000); // 5 seconds
// Yellow light
digitalWrite(greenPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(redPin, LOW);
delay(2000); // 2 seconds
// Red light
digitalWrite(greenPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, HIGH);
delay(5000); // 5 seconds
}
Practice Challenge¶
Modify the code to include a pedestrian crosswalk button!
▶ Show Solution
int greenPin = 9;
int yellowPin = 10;
int redPin = 11;
int buttonPin = 2;
int pedestrianPin = 12;
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(pedestrianPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Normal cycle
if (digitalRead(buttonPin) == LOW) {
// Pedestrian crossing requested!
// Flash pedestrian light
for(int i=0; i<10; i++) {
digitalWrite(pedestrianPin, HIGH);
delay(250);
digitalWrite(pedestrianPin, LOW);
delay(250);
}
}
// [Rest of normal traffic light code...]
}
int greenPin = 9;
int yellowPin = 10;
int redPin = 11;
int buttonPin = 2;
int pedestrianPin = 12;
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(pedestrianPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Normal cycle
if (digitalRead(buttonPin) == LOW) {
// Pedestrian crossing requested!
// Flash pedestrian light
for(int i=0; i<10; i++) {
digitalWrite(pedestrianPin, HIGH);
delay(250);
digitalWrite(pedestrianPin, LOW);
delay(250);
}
}
// [Rest of normal traffic light code...]
}
Part 6: Project 2 - Intruder Alert System¶
Requirements¶
- Detect when someone opens a door (magnetic switch)
- If triggered, turn on buzzer for 3 seconds
- Reset automatically after buzzer stops
Circuit¶
Pin 2 ── Magnetic switch ── GND (with internal pull-up)
Pin 12 ──[Buzzer positive]
GND ──── Buzzer negative
Code¶
int sensorPin = 2;
int buzzerPin = 12;
void setup() {
pinMode(sensorPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW); // Start with buzzer off
}
void loop() {
int sensorState = digitalRead(sensorPin);
// When door opens (sensor goes LOW due to pull-up)
if (sensorState == LOW) {
// Turn on buzzer
digitalWrite(buzzerPin, HIGH);
delay(3000); // 3 seconds
// Turn off buzzer
digitalWrite(buzzerPin, LOW);
}
}
Part 7: Project 3 - Random Fortune Teller¶
Requirements¶
- Generate random numbers (1-6)
- Display number on LEDs (like dice)
- Button triggers new random number
Circuit¶
Code¶
int buttonPin = 2;
int ledPins[] = {3, 4, 5, 6, 7, 8};
int buttonState = 0;
int randomNumber;
void setup() {
for (int i = 0; i < 6; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
// Seed random number generator
randomSeed(analogRead(0));
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
// Generate random number 0-5
randomNumber = random(6);
// Turn on the corresponding LED
for (int i = 0; i < 6; i++) {
if (i == randomNumber) {
digitalWrite(ledPins[i], HIGH);
} else {
digitalWrite(ledPins[i], LOW);
}
}
delay(200); // Debounce delay
}
}
Summary¶
- Arduino IDE is used to write and upload sketches
- setup() runs once at startup, loop() runs continuously
- pinMode() configures pins as INPUT or OUTPUT
- digitalWrite() controls outputs (HIGH/LOW)
- digitalRead() reads input state
- delay() creates time delays in milliseconds
- Use INPUT_PULLUP for buttons to use internal pull-up resistors
Key Reminders¶
- Always use current-limiting resistors with LEDs
- Use the Serial Monitor for debugging (next lesson)
- Break projects into smaller parts to test incrementally
- Read sensor states in the loop() function
- Use delay() for simple debouncing
Custom activity — adapted from PLTW Digital Electronics