Store
Your lesson ≈ 220 min

Lesson 3 · Weeks 3–4

Inputs, outputs & the PRP activities

What you'll learn

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.

What you're doing today

  • List common items that use each type of sensor and each type of actuator.
  • PRP #02 (Button): IPO chart; load & run the Button sketch; change the button pin to 7; write pseudocode for "LED on when the button is pressed"; "making connections" questions; challenges (reverse the action; LED stays on 3 s after release; turn the button into a toggle switch); evaluation questions.
  • PRP #03 (Analog input): IPO chart; load & run the analog-input sketch; open the Serial Monitor; pseudocode for "blink faster/slower with the pot"; challenges (several LEDs flash; reverse the pot so it speeds up the other way — 1023 − sensorValue; LEDs scroll as you turn the dial; a night light that turns the LED on when the LDR reading is low); evaluation questions.
  • PRP #04 (Buzzer): IPO chart; type up the buzzer sketch; pseudocode to make the buzzer sound; "making connections"; make it sound like an alarm; challenges (two or more tones; buzzer only while the button is pressed; toggle the buzzer; play a familiar tune); evaluation questions.
PRP 2

Digital input — Button

Load _02_Button.ino — this sketch uses the ThinkerShield button (pin 7) to control the D12 LED. Press the button: the LED comes on. Let go: it goes off. The button is a digital input — it's only ever on or off (a binary input).

New ideas in this PRP:

  • if / else statements let the program make a decision based on a condition (here, "is the button pressed?"). Think of a night light: the processor decides to turn the light on if it's dark.
  • Comparison operators: == equal to, != not equal to, < less than, > greater than, <= / >= less-than/greater-than-or-equal. Note: a single = assigns a value; a double == compares two values.
  • The int command (_03_int.ino) lets you give a pin a name — e.g. int LED = 12; — so the code reads clearly and you only have to change a pin in one place. The bit before void setup() is the declarations section. You can't reuse a word Arduino already knows (if it changes colour, pick another).

Then: write pseudocode for "turn the LED on when the button is pressed, off when released"; draw the flowchart; answer the "making connections" questions; and tackle the challenges:

  • reverse the action — the LED is always on unless the button is pressed;
  • make the LED stay on for 3 seconds after the button is released;
  • turn the button into a toggle switch — press once for on, press again for off (tricky — you'll need a variable to remember the LED's state and a short delay() to stop the button "bouncing").

Get a partner or your teacher to tick off each challenge, then complete the evaluation questions.

What you'll learn

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.

What you'll make

  • I can press the button and turn the LED on; release it and turn the LED off.
  • I can explain what each line of _02_Button.ino does.
  • I can flip the action so the LED is on unless the button is pressed.
  • I can use int buttonPin = 7; to give a pin a name.

What you need

Arduino Uno + MAAS ThinkerShield + USB cable, a laptop with the Arduino IDE.

Sample code

_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
}
Quick chips

Steps

  1. Open _02_Button.ino from the Sample code list above and upload it.
  2. Press the button on the ThinkerShield (pin D7). The D12 LED should turn on. Release. LED off.
  3. Complete the IPO chart in your workbook.
  4. Challenges:
    • Reverse the action — change == to make the LED stay on unless the button is pressed.
    • Stay on for 3 seconds — add a delay(3000); after the LED-on line.
    • Toggle switch — make the LED stay on after one press, and turn off on the next press (you'll need an int ledState = 0; variable).
  5. Get it signed off.
  6. Reflect.

IPO chart

Inputs

  • Pushbutton on the ThinkerShield, pin D7 (digital — reads HIGH or LOW)

Processing

  • void setup() sets the LED pin as OUTPUT and the button pin as INPUT
  • void loop() reads the button: buttonState = digitalRead(buttonPin)
  • if buttonState == HIGH → turn the LED on; else → turn the LED off

Outputs

  • LED on the ThinkerShield, pin D12
Stuck? Common things to check Tap to open
  • = 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.

Take it slower

Stick with the basic Button sketch and the Reverse the action challenge. Pair up.

Push further

Use _03_int.ino as a model — give every pin a friendly name in your code.

Done when…

Working button sketch + reversed-action challenge + IPO chart + reflection.

Reflect

What's the difference between a digital input and an analog input?

PRP 3

Analog input — potentiometer & LDR

Load _05_analog_input.ino — this sketch lets a potentiometer (the knob marked POT A5 on the ThinkerShield) or a light-dependent resistor (LDR) control the LED. Unlike a button (only 0 or 5 V), an analog input can be anything in between: analogRead() turns the 0–5 V range into a number from 0 to 1023. Turn the knob and the blink rate changes.

The Serial Monitor. Your code might not look like it's working — the Serial Monitor shows you what's happening "in the background". Add Serial.begin(9600); in setup() (the 9600 is the speed — it must match the dropdown at the bottom of the Serial Monitor window), then Serial.print("text") writes text on the same line and Serial.println(value) writes a value and starts a new line.

Then: write pseudocode to make the LED blink faster/slower as you turn the pot; answer the "making connections" questions; and tackle the challenges:

  • get several LEDs to flash with the pot;
  • reverse the pot so the blink rate speeds up the other way (hint: use a delay of 1023 − sensorValue);
  • use the pot to make different LEDs light up as you turn the dial (e.g. 0–254 → D9, 255–511 → D10, 512–1023 → D11);
  • swap the input to the LDR and make a night light — the LED turns on when the LDR reading drops below a threshold you find by watching the Serial Monitor at different light levels.

Get a partner or your teacher to tick off each challenge, then complete the evaluation questions.

What you'll learn

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.

What you'll make

  • I can turn the potentiometer and watch the blink rate change.
  • I can open the Serial Monitor and read the sensor value.
  • I can write a "night light" using the LDR.
  • I can explain the difference between digitalRead and analogRead.

What you need

Arduino Uno + MAAS ThinkerShield + USB cable, a laptop with the Arduino IDE.

Sample code

_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
}
Quick chips

Steps

  1. Open _05_analog_input.ino and upload it.
  2. Turn the POT A5 knob. The LED's flash rate should change.
  3. Open the Serial Monitor (magnifying glass top-right of the IDE). Make sure the baud rate at the bottom is 9600. Watch the values change as you turn the knob.
  4. Complete the IPO chart.
  5. Challenges:
    • Multiple LEDs flash with the pot (try pins 9, 10, 11, 12).
    • Reverse the pot — make the LED flash faster when you turn the knob down. Hint: use 1023 - sensorValue as the delay.
    • Light meter — use the LDR (pin A4) instead of the pot. Make a night light: the LED should come on when the LDR reading drops below a threshold you find by watching the Serial Monitor with the lights on and off.
  6. Get signed off, reflect.

IPO chart

Inputs

  • Potentiometer on the ThinkerShield, pin A5 (analog — a value from 0 to 1023)
  • Light-dependent resistor (LDR) on pin A4 (analog) — for the night-light challenge

Processing

  • sensorValue = analogRead(sensorPin) — read the knob (or the light level)
  • Use sensorValue as the delay so the blink rate tracks the knob — or compare it to a threshold (e.g. < 300) to switch the LED
  • Serial.print / Serial.println report the value to the Serial Monitor

Outputs

  • LED on the ThinkerShield, pin D12
  • Text on the Serial Monitor
Stuck? Common things to check Tap to open
  • 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).

Take it slower

Stick with the basic sketch + Serial Monitor. Watch a classmate complete the night-light challenge before attempting it.

Push further

Combine the LDR + buzzer: make the buzzer beep when the LDR reading drops below the threshold. This is a working alarm prototype.

Done when…

Working analog input + Serial Monitor reading + at least one challenge complete + IPO chart + reflection.

Reflect

How could you use an analog input in your alarm project?

PRP 4

Digital output — Buzzer

Type up (or load) _07_Buzzer1.ino — it makes the piezo buzzer (pin 3) beep using the tone(pin, frequency, duration) command: tone(buzzerPin, 600, 30); plays a 600 Hz tone for 30 ms, then delay(150); waits 150 ms before the loop repeats. A piezo buzzer isn't a speaker, but it still outputs sound.

Then: write pseudocode to make the buzzer sound; answer the "making connections" questions; make it sound like an alarm by alternating a low and a high tone (see _08_Buzzer2.ino); and tackle the challenges:

  • get the buzzer to play two or more different tones (the Arduino Digital → Tone examples help);
  • make the buzzer only sound while the button is pressed;
  • toggle the buzzer — press the button once to start it, press again to stop (you'll need a variable to remember whether it's playing, and a short delay() to debounce the button);
  • make the buzzer play a familiar tune (e.g. the Imperial March) by listing tone(pin, frequency, duration); delay(duration); lines.

Get a partner or your teacher to tick off each challenge, then complete the evaluation questions. _09_Buzzer3.ino is an extension: it uses a for loop to sweep the tone smoothly up and back down.

What you'll learn

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

What you'll make

  • I can make the buzzer beep with _07_Buzzer1.ino.
  • I can make the buzzer alternate between two tones (_08_Buzzer2.ino).
  • I can wire the buzzer to come on only when the button is pressed.
  • I can read _10_Alarm_Basic.ino and explain how it works.

What you need

Arduino Uno + MAAS ThinkerShield + USB cable, a laptop with the Arduino IDE.

Sample code

_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);
  }
}
Quick chips

Steps

  1. Open _07_Buzzer1.ino and upload it. Listen — the buzzer beeps.
  2. Open _08_Buzzer2.ino and upload it. Listen — it alternates high and low like an alarm.
  3. Complete the IPO chart.
  4. Challenges:
    • Two or more tones — modify Buzzer1 to play three different frequencies in sequence.
    • Button-controlled buzzer — combine PRP 2 (Button) and Buzzer1; make the buzzer sound only when the button is pressed.
    • Toggle buzzer — press the button once to start the buzzer; press again to stop.
    • Familiar tune — play four notes of a song you know. Frequency reference is in the _07_Buzzer1.ino description.
  5. Read _10_Alarm_Basic.ino — the worked model for the design project. Explain to a partner what each of the four Status values does.
  6. Get signed off, reflect.

IPO chart

Inputs

  • None for the basic sketch — it is timer-driven
  • Pushbutton on pin D7 — for the button-controlled and toggled-buzzer challenges

Processing

  • void setup() sets the buzzer pin (3) as an OUTPUT
  • void loop(): tone(buzzerPin, frequency, duration) then delay(...) — repeat
  • Alarm version: alternate a low tone and a high tone with a delay between each

Outputs

  • Piezo buzzer on the ThinkerShield, pin D3
Stuck? Common things to check Tap to open
  • 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.

Take it slower

Stick with Buzzer1 + Buzzer2 + the button-controlled buzzer challenge.

Push further

Use _09_Buzzer3.ino as a model — use a for loop to sweep the tone smoothly.

Done when…

Working buzzer + button-controlled buzzer + IPO chart + reflection + reading _10_Alarm_Basic.ino.

Reflect

Sketch the flow chart for _10_Alarm_Basic.ino in your workbook.

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.