Store
Teacher guide Researching & planning ≈ 220 min

Lesson 3 · Weeks 3–4

Inputs, outputs & the PRP activities

Learning intention

Use digital and analog inputs to control outputs. Read a button with `digitalRead`, a potentiometer/LDR with `analogRead`, and drive an LED and a piezo buzzer. Use the Serial Monitor to inspect what your program is doing.

Success criteria

  • I can wire and run PRP 2 (Button), PRP 3 (Analog input) and PRP 4 (Buzzer).
  • I can read a sensor value from the Serial Monitor and use it to decide what an output does (if/else).
  • I can use the int command to give a pin a friendly name in code.
  • I can describe how the alarm project will use one of these inputs + one of these outputs.

Before class

  • Verify the Serial Monitor opens cleanly on every machine (baud rate 9600).
  • Pre-load the four sample sketches in the IDE: _02_Button, _03_int, _05_analog_input, _07_Buzzer1.
  • Print or share the PRP 2/3/4 student pages; these are the activity guides students follow.
  • Have a working potentiometer-controlled night light to show before students start PRP 3.

Materials

Standard PRP kit (Arduino + ThinkerShield + USB) plus pin-map cards and a class set of Serial Monitor cheat-sheets (downloadable from /resources).

Demo & teacher script

  1. Recap Blink in 60 seconds. Today: make the LED listen to something.
  2. Demo PRP 2 — press the ThinkerShield button; the LED turns on.
  3. Walk through the if/else block out loud. "If the button is HIGH, turn the LED on; else, turn it off."
  4. Set them off on PRP 2. Circulate.
  5. After ~30 min, gather for PRP 3 demo: turn the pot, watch the flash rate change, open Serial Monitor.
  6. PRP 4 demo: change the buzzer tone with two tone() calls. Tease the alarm sound.

Activities in this lesson

PRP 2

Digital input — Button

Students will: Use a digital input — the ThinkerShield button — to control the LED with an `if`/`else` statement. Learn to give pins friendly names with the `int` command.

_02_Button.ino · Button — PRP #02 (Crack the Code) ✦ English View Raw
int buttonPin = 7;        // the pushbutton is on pin D7 of the ThinkerShield
int ledPin = 12;          // the LED is on pin D12
int buttonState = 0;      // variable for reading the pushbutton's state

void setup() {
  pinMode(ledPin, OUTPUT);     // the LED pin is an output
  pinMode(buttonPin, INPUT);   // the button pin is an input
}

void loop() {
  buttonState = digitalRead(buttonPin);   // read the button
  if (buttonState == HIGH) {              // is it pressed?
    digitalWrite(ledPin, HIGH);           // yes -> LED on
  } else {
    digitalWrite(ledPin, LOW);            // no  -> LED off
  }
}
_03_int.ino · The int command — PRP #02 (Crack the Code) ✦ English View Raw
int LED = 12;             // call pin 12 "LED" for the rest of this sketch

void setup() {
  pinMode(LED, OUTPUT);   // make the LED pin an output
}

void loop() {             // runs over and over again, forever
  digitalWrite(LED, HIGH);  // turn the LED on
  delay(1000);              // wait for one second
  digitalWrite(LED, LOW);   // turn the LED off
  delay(1000);              // wait for one second
}
Common errors & fixes
  • = vs == — single = assigns; double == compares. The IDE won't always warn you.
  • Pin 2 vs pin 7 — some printed copies of the workbook use pin 2; on the ThinkerShield the button is pin 7. Use 7.
PRP 3

Analog input — potentiometer & LDR

Students will: Read an analog input (the potentiometer or LDR), use its value to control output behaviour, and use the Serial Monitor to see what your program is doing.

_05_analog_input.ino · Analog input — PRP #03 (Crack the Code) ✦ English View Raw
int sensorPin = A5;       // the potentiometer (POT A5) on the ThinkerShield
int ledPin = 12;          // the LED on pin D12
int sensorValue = 0;      // variable to store the value from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);  // the LED pin is an output
  Serial.begin(9600);       // start serial so we can print the sensor value
}

void loop() {
  sensorValue = analogRead(sensorPin);  // read the knob (0 to 1023)
  digitalWrite(ledPin, HIGH);           // LED on
  delay(sensorValue);                   // wait — bigger value, longer wait
  digitalWrite(ledPin, LOW);            // LED off
  delay(sensorValue);                   // wait again
  Serial.print("The sensor value is: ");
  Serial.println(sensorValue);          // print the value to the Serial Monitor
}
Common errors & fixes
  • Serial Monitor garbled — baud rate mismatch. Set it to 9600.
  • Reading goes to 0 or 1023 instantly — the pot is probably wired wrong (move the connector); the LDR is probably saturated (cover it with your hand).
PRP 4

Digital output — Buzzer

Students will: Use a digital output — the piezo buzzer — to make sound. Use the `tone(pin, frequency, duration)` command and build a two-tone alarm.

_07_Buzzer1.ino · Buzzer — PRP #04 (Crack the Code) ✦ English View Raw
int buzzerPin = 3;        // the piezo buzzer is on pin D3 of the ThinkerShield

void setup() {
  pinMode(buzzerPin, OUTPUT);   // the buzzer pin is an output
}

void loop() {
  tone(buzzerPin, 600, 30);     // play a 600 Hz tone for 30 ms...
  delay(150);                   // ...then wait 150 ms before repeating
}
_08_Buzzer2.ino · Two-tone alarm — PRP #04 (Crack the Code) ✦ English View Raw
int lowTone = 400;
int highTone = 600;
const int ALARMSOUNDER = 3;     // the buzzer is on pin D3

void setup() {
  pinMode(ALARMSOUNDER, OUTPUT);
}

void loop() {
  tone(ALARMSOUNDER, lowTone, 500);   // low tone for half a second
  delay(500);
  tone(ALARMSOUNDER, highTone, 500);  // high tone for half a second
  delay(500);
}
_09_Buzzer3.ino · Tone sweep with a for loop — PRP #04 extension (Crack the Code) ✦ English View Raw
int i = 200;
const int ALARMSOUNDER = 3;     // the buzzer is on pin D3

void setup() {
  pinMode(ALARMSOUNDER, OUTPUT);
}

void loop() {
  for (i = 200; i < 700; i = i + 2) {   // sweep from a low tone up to a high tone
    tone(ALARMSOUNDER, i, 10);          // play the tone, changing every 10 ms
    delay(10);
  }
  for (i = 701; i > 200; i = i - 2) {   // sweep back down
    tone(ALARMSOUNDER, i, 10);
    delay(10);
  }
}
_10_Alarm_Basic.ino · Basic box alarm — design project model (Crack the Code) ✦ English View Raw
int sensorPin = 7;    // the sensor (a switch on the box) connects to pin 7
int flashPin = 13;    // an LED that lights up when the alarm sounds
int buzzerPin = 3;    // the buzzer connects to pin 3
int Status = 0;       // 0 = exit delay, 1 = armed, 2 = triggered
int sensor = 0;       // the current sensor reading

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(flashPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  if (Status == 0) {            // first time through the loop — arm the alarm
    delay(3000);                // gives you 3 seconds to close the box
    tone(buzzerPin, 725, 40);   // a short beep...
    delay(200);
    tone(buzzerPin, 725, 40);   // ...and another, so it double-beeps when armed
    Status = 1;                 // armed — don't run this block again
  }
  if (Status == 1) {
    sensor = digitalRead(sensorPin);   // read the switch
  }
  if (sensor == 0) {            // the box has been opened
    Status = 2;
  }
  if (Status == 2) {            // triggered — flash the LED and sound the alarm
    digitalWrite(flashPin, HIGH);
    tone(buzzerPin, 725, 1000);
    delay(1000);
    tone(buzzerPin, 330, 1000);
    delay(1000);
  }
}
Common errors & fixes
  • Buzzer pin — D3, not D7. Some printed copies of the workbook have D7. The sample sketches here are correct.
  • tone() arguments — pin, frequency in Hz, duration in ms. Follow with a delay() at least as long as the duration or notes run together.

Common misconceptions & fixes

  • = vs == — single = assigns; double == compares. The compiler won't always catch this.
  • Wrong pin on ThinkerShield — button is D7, buzzer is D3, pot is A5, LDR is A4. Some printed copies of the workbook have the wrong pin numbers; the sample sketches here are correct.
  • Serial Monitor blank or garbled — baud rate mismatch (must be 9600).
  • analogRead returns 0–1023, not 0–255. The delay(sensorValue) trick visualises this.

Evidence to collect

  • Three completed IPO charts (PRP 2, PRP 3, PRP 4).
  • Screenshot or video of each student's working button, working analog input + Serial Monitor, working buzzer.
  • Annotated code for at least one PRP showing modifications.
  • Challenge checkboxes signed off for each PRP.

Support path

  • For each PRP, supply a "spot the bug" version of the sketch — students fix three deliberate errors before they get to modify it.
  • Provide a flowchart template for the toggle-switch challenge.

Extension path

  • Combine PRP 3 + PRP 4 — use the LDR threshold to trigger the buzzer (an early alarm prototype).
  • Use tone() with three different frequencies to play a recognisable two-second tune.
  • Use a for loop (extension code in _09_Buzzer3.ino) to sweep the tone.

Exit reflection

Which of the three PRPs felt closest to your alarm idea? What input + output will your alarm use?

Syllabus outcomes hit by this lesson: TE4-4DP TE4-7DI TE4-DES-01 TE4-DIG-02
Maddy, co-founder of Little Bird

Need help? We're here for you!

Hi, I'm Maddy. My team and I are ready to help with your order or any questions.