Learning Goals 5 min
Today we drill the programming-heavy domains: 5 (syntax), 6 (I/O + PWM), 7 (logic / algorithms), 8 (boards). These are 55% of the exam — the biggest single block. By the end you will:
- Trace small sketches and predict outputs.
- Recognise common bugs and their fixes.
- Know which board / function / library to pick for a scenario.
Warm-Up 5 min
Same drill: pen + paper, < 2 min per question.
Sample Questions · Domains 5-8 60 min
Domain 5 · Programming Syntax
int a = 5;
int b = 3;
int c = a / b;
int d = a % b;
Serial.print(c); Serial.print(" "); Serial.println(d);Output?
Answer
Integer division: 5/3 = 1. Modulo: 5%3 = 2. Output: 1 2.
What's wrong with this declaration?
unsigned int count = -1;Answer
Assigning a negative literal to unsigned int wraps to the max value (e.g. 65535 on UNO). Sometimes intentional; usually a bug.
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
Serial.print(i);
}Output?
Answer
continue skips the rest of this iteration. Output: 0124.
Which declares a constant that lives in flash (not RAM) on AVR?
const int x = 5;const char* msg = "hello";const char msg[] PROGMEM = "hello";static int x = 5;
Answer
3. PROGMEM places the constant in program memory (flash). Without it, even const arrays get copied to RAM at startup.
Domain 6 · Inputs / Outputs / PWM
What does analogWrite(9, 64) do on a UNO?
Answer
Generates a PWM signal on D9 with duty cycle = 64/255 ≈ 25%. Frequency ≈ 490 Hz on D9. Pin must be PWM-capable.
The UNO's ADC returns values in the range:
Answer
0 to 1023 (10-bit). 0 = 0 V, 1023 = VREF (default 5 V).
Why is delay() inside an interrupt handler bad?
Answer
delay() depends on Timer 0 and interrupts being enabled. Inside an ISR, interrupts are disabled by default → delay() hangs. Also: ISRs should be as short as possible.
Which pin combo is correct for a Servo on a UNO?
- Signal → D9, Power → +5V, GND → GND
- Signal → A0, Power → +5V, GND → GND
- Signal → D9, Power → +3.3V, GND → GND
Answer
1. Servos need 5 V and a digital PWM-capable pin. Most servos won't move from 3.3 V.
Domain 7 · Programming Logic & Algorithms
Why use millis() instead of delay() in a robot main loop?
Answer
delay() blocks the CPU; nothing else can run. millis() lets you check the time without blocking, so other tasks (sensor reads, button checks) keep running.
What does this function compute?
int f(int n) {
int s = 0;
for (int i = 1; i <= n; i++) s += i;
return s;
}Answer
Sum of integers from 1 to n. (Triangle number; closed form n(n+1)/2.)
You debounce a button by:
Answer
Sampling the pin, waiting a short fixed time (e.g. 50 ms), sampling again, and accepting the press only if both samples agree. Or in non-blocking style with millis().
What's a state machine?
Answer
A program organised around a small fixed set of named states + rules for transitions between them. Implemented with an enum + switch in loop(). Used in traffic-light controllers, doorbells, robots.
Domain 8 · Frameworks & Boards
Which Arduino board has the most GPIO pins?
- UNO R3
- Mega 2560
- Nano
- Nano 33 BLE
Answer
2. Mega: 54 digital + 16 analog = 70 pins.
The default I²C pins on Arduino UNO are:
Answer
SDA = A4, SCL = A5. (Also exposed on the SDA/SCL header pins on R3.)
Which library is for I²C communication?
- SPI
- Servo
- Wire
- Stepper
Answer
3. Wire is the I²C library; SPI is for SPI.
Which board can run TensorFlow Lite Micro for gesture recognition?
- UNO R3
- Nano 33 BLE Sense
- Nano (V3)
- Mega 2560
Answer
2. Nano 33 BLE Sense has the Cortex-M4 + 256 KB RAM + 1 MB flash + on-board IMU. UNO/Nano/Mega lack the RAM and have no IMU on board.
Basic 15 min
Bench sprint · Domains 5, 6 and 8
Interrupts, PWM pins and variable types all appear in today's questions. Here they are on the bench. The button is wired from D3 to GND. The LED is on D8 with a 220 Ω resistor. Each press should make the LED one step brighter. Ten presses reach full brightness, and it stays there. The count should print once per press.
const int BUTTON_PIN = 3;
const int LED_PIN = 8;
int presses = 0;
void onPress() {
presses++;
Serial.println(presses);
delay(200);
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), onPress, FALLING);
}
void loop() {
int brightness = presses * 255 / 10;
analogWrite(LED_PIN, brightness);
}There are four mistakes. Find them, fix them, and build the circuit to test your fix.
It works if each press brightens the LED a step, it stays full after ten, and prints one number.
Challenge 3 · Fix the traffic-light state machine 10 min
This covers Domain 7. The lights should show red for 5 s, green for 4 s, amber for 1 s, and repeat. Instead, amber never seems to light, and after about half a minute the lights race. There are three mistakes.
enum Light { RED, GREEN, AMBER };
const int RED_PIN = 10;
const int AMBER_PIN = 11;
const int GREEN_PIN = 12;
const unsigned long RED_MS = 5000;
const unsigned long GREEN_MS = 4000;
const unsigned long AMBER_MS = 1000;
Light state = RED;
int lastChange = 0;
void showLight(int pin) {
digitalWrite(RED_PIN, LOW);
digitalWrite(AMBER_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(pin, HIGH);
}
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(AMBER_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
showLight(RED_PIN);
}
void loop() {
unsigned long now = millis();
switch (state) {
case RED:
if (now - lastChange >= RED_MS) {
state = GREEN;
showLight(GREEN_PIN);
lastChange = now;
}
break;
case GREEN:
if (now - lastChange >= GREEN_MS) {
state = AMBER;
showLight(AMBER_PIN);
}
case AMBER:
if (now - lastChange >= AMBER_MS) {
state = RED;
showLight(RED_PIN);
lastChange = now;
}
break;
}
}Trace one full cycle on paper to find the mistakes. Then build three LEDs (with 220 Ω resistors) on D10–D12 and test your fix.
It works if the lights run red 5 s, green 4 s, amber 1 s, correctly, for at least two minutes.
Recap 5 min
Domains 5-8 are the biggest block. Trace sketches mentally. Recall pin maps. Know which library is which. Don't get stuck — flag + move on. Tomorrow: a full mock exam.
- Integer division
- For ints,
/rounds towards zero. Use float division for fractional results. - Modulo (%)
- Remainder of integer division. Common for "every Nth iteration" tests.
- continue / break
- continue = skip rest of this iteration. break = exit the loop entirely.
- Interrupt service routine (ISR)
- Function called by hardware on an event. Must be brief; can't use delay() or Serial.print() reliably.
- millis()
- Milliseconds since the program started. Wraps after ~49 days. Non-blocking timing.
- State machine
- enum + switch pattern. The most common way to structure non-trivial Arduino sketches.
- Wire library
- I²C; SPI for SPI; Servo for servos; Stepper for steppers. Don't confuse names under exam pressure.
- PROGMEM
- AVR keyword for storing data in flash instead of RAM. Use with
pgm_read_*to read.
Extra Mission 5 min
Part 1 — Design a reaction-time trainer
Domains 5 to 8 meet in one small game. On paper, design a reaction-time trainer. An LED lights after a random wait. The player presses a button as fast as they can.
Your design must include:
- A state diagram: every state, and what moves the game from one to the next.
- How you time the reaction, and which variable types you use.
- What happens if the player presses too early.
- Which pins you use, and why each one suits its job.
Part 2 — Make it
Build it with an enum + switch state machine and millis(), with no delay(). Read the button on an interrupt pin. Print each reaction time in milliseconds, and flag early presses.
Bring back next class: your uploaded sketch, your state diagram and a Serial Monitor screenshot of five rounds, including one early press.