← Arduino

Arduino: recap

For after the lessons: one card per lesson, to check what you remember. Answer each question in your head or on paper first, then check it. Nothing here is marked.

A new board

1. Same circuit, new board

Try firstAn LED is wired to pin 4. Write the two lines that set it up and switch it on.

Check your answer

pinMode(4, OUTPUT); (in setup) and digitalWrite(4, HIGH); (in loop).

An Arduino names its pins by number. pinMode(12, OUTPUT) sets pin 12 up as an output, and digitalWrite(12, HIGH) switches it to 5 V (LOW is 0 V). The number in every line must be the pin the wire goes to: the code doesn't find the LED for you. A pin written HIGH without pinMode(..., OUTPUT) only glows dimly.

An Uno with an LED and its resistor on pin 13, and the sketch that blinks it.
An Uno with an LED and its resistor on pin 13, and the sketch that blinks it.
The pin in the code must match the wire · Sound on for the voiceover
Open the lesson →

2. Reading the C++

Try firstWhat is the difference between setup() and loop()?

Check your answer

setup() runs once when the board starts; loop() runs again and again after it.

Every sketch has setup(), which runs once when the board starts, and loop(), which runs again and again after it. delay(ms) waits that many milliseconds; writing a pin takes almost no time, so the delays set the timing. A blink of HIGH, delay(500), LOW, delay(500) takes 1000 ms.

setup() sets pin 13 up once; loop() blinks it for ever.
setup() sets pin 13 up once; loop() blinks it for ever.
setup() once, loop() for ever · Sound on for the voiceover
Open the lesson →

Writing C++

3. Values and decisions

Try firstWrite an if that switches pin 13 on when level is 800 or more, and off otherwise.

Check your answer

if (level >= 800) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); }

int level = 0; declares a whole-number variable. level = analogRead(A0); stores the knob's reading (0 to 1023). if (level > 500) { … } else { … } runs the first part when the comparison is true and the second when it isn't. == compares; a single = sets the variable, even inside an if, so the if is then always true. Every statement ends with ;, or the sketch won't upload.

A knob on A0: above 500 the LED on pin 13 comes on.
A knob on A0: above 500 the LED on pin 13 comes on.
Read a value, then decide · Sound on for the voiceover
Open the lesson →

4. Talking to the computer

Try firstWrite the two lines in loop() that print Temp: and then the value of temp on one line.

Check your answer

Serial.print("Temp: "); Serial.println(temp);

Serial.begin(9600); in setup() opens a line to the computer. Serial.println(level); sends the value and starts a new line; Serial.print stays on the same line, so Serial.print("Level: "); Serial.println(level); gives Level: 512. In C++, "Level: " + level does not join them. Printing what the board sees is the first step in debugging a sketch.

The serial monitor shows each reading the board sends.
The serial monitor shows each reading the board sends.
Printing what the board sees · Sound on for the voiceover
Open the lesson →

5. Outputs that vary

Try firstWhy does analogWrite(9, 128) make an LED look half as bright, if the pin is only ever 0 V or 5 V?

Check your answer

The pin is on for half of each cycle and switches too fast to see, so the eye sees the average: half as bright.

analogWrite(9, value) takes 0 (off) to 255 (fully on) on a PWM pin (marked ~: 3, 5, 6, 9, 10, 11). The pin doesn't get a lower voltage: it switches fully on and off very fast, and the value sets how much of each cycle it is on, so 128 looks about half as bright. A knob gives 0 to 1023, so level / 4 scales it to 0 to 255.

analogWrite(9, 128): pin 9 is on for half of each cycle.
analogWrite(9, 128): pin 9 is on for half of each cycle.
Brightness by switching fast · Sound on for the voiceover
Open the lesson →

Real hardware

6. From simulator to breadboard

Try firstAn LED's legs are in b14 and e14. Will it light? Why?

Check your answer

No: both legs are in column 14's strip, so the current goes round it.

A breadboard joins the five holes of each numbered column (a–e, and separately f–j; the gap splits them). The rails along the edges are joined along their length (+ for power, − for GND). Rows are not joined. Each leg of a part must be in a different strip, and the loop runs from strip to strip through the parts: pin → resistor → LED → GND.

Pin 13 → column 3 → resistor → column 7 → LED → column 8 → GND.
Pin 13 → column 3 → resistor → column 7 → LED → column 8 → GND.
Hidden strips · Sound on for the voiceover
Open the lesson →

7. A small system

Try firstA brief says a buzzer sounds when the reading is 500 or more. Write three test readings for the threshold and what each should do.

Check your answer

499 silent, 500 sounds, 501 sounds.

A system reads a sensor, decides, and drives an output; a brief says what it must do. Test it with a table of cases: each side of every threshold, the threshold itself, and the ends of the range. All tests passing only shows the sketch does what the tests check, so a table without the threshold can hide an off-by-one mistake there.

A plant-water alarm: the soil reads low (dry), so the LED blinks.
A plant-water alarm: the soil reads low (dry), so the LED blinks.
Testing against the brief · Sound on for the voiceover
Open the lesson →