Skip to content

Activity 4.2.2 — Arduino Digital I/O & Basic Projects


Learning Objectives

By the end of this lesson, students will be able to:

  1. Set up the Arduino IDE and connect the board
  2. Write and upload basic Arduino programs
  3. Control digital outputs (LEDs)
  4. Read digital inputs (pushbuttons)
  5. Create simple projects combining inputs and outputs
  6. 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

  1. Download from arduino.cc
  2. Install the application
  3. Launch Arduino IDE

Step 2: Connect Your Board

  1. Connect Arduino Uno to computer via USB cable
  2. Windows: Drivers should install automatically
  3. Mac: No driver needed
  4. 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!


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

  1. Type or paste the code into the editor
  2. Click the Upload button (right arrow →)
  3. Wait for "Done uploading" message
  4. 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:

Arduino Pin 9 ──[220Ω resistor]───[LED long leg]
LED short leg ──────────────────────────── GND

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:

+5V ──[10kΩ]───[Pin 2]───[Pushbutton]─── GND
                 Pin 2 (input with pull-down)

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

Pin 9 ──[220Ω]─── Green LED
Pin 10 ──[220Ω]─── Yellow LED
Pin 11 ──[220Ω]─── Red LED

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...]
}

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

Pin 2 ── Pushbutton ── GND (with pull-up)
Pins 3-8 ── 220Ω resistors ── LEDs

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