← Engineering

Arduino

The same circuits on an Arduino Uno, now in C++: pins by number, setup() and loop(), variables and decisions, the serial monitor and PWM. Each sketch runs on a board in the lesson, and each lesson has a step to build it in Tinkercad Circuits. The last unit moves to a real breadboard and a project tested against a brief.

Start1. Same circuit, new board

A new board

Pins by number, and what setup() and loop() do.

1Same circuit, new boardNot started

An Arduino Uno names its pins by number, and the code must use the number of the pin the wire goes to. pinMode sets a pin as an output; digitalWrite switches it HIGH or LOW. The circuit ideas from the micro:bit carry straight over.

  1. Which pin?
  2. Try: run it
  3. Worked example: read the pins › Every pin number
  4. Worked example: read the pins › Find the mistake
  5. Vary: change the pin
  6. Practise: pins › Set up a pin
  7. Practise: pins › A dim glow
  8. Your own sketch
Start lesson
2Reading the C++Not started

Every sketch has two parts: setup() runs once when the board starts, then loop() runs again and again for ever. delay(ms) waits that many milliseconds, so the delays set the timing of a blink.

  1. How many times?
  2. Try: watch the counters
  3. Worked example: time it › Board time
  4. Worked example: time it › Sam's blink
  5. Vary: change the timing › Twice as fast
  6. Vary: change the timing › A flash
  7. Practise: timing › Count the blinks
  8. Practise: timing › Where it goes
  9. A lighthouse
Start lesson

Writing C++

Values and decisions, printing to the computer, and outputs that vary.

3Values and decisionsNot started

A variable is declared with its type, int level = 0;. analogRead(A0) gives 0 to 1023, and if (level > 500) { … } else { … } decides. == compares; a single = sets the variable, even inside an if. Every statement ends with ;, or the sketch won't upload.

  1. Knob at zero
  2. Try: turn the knob › Up
  3. Try: turn the knob › Down
  4. Worked example: two slips › Read it
  5. Worked example: two slips › Find the mistake
  6. Vary: change one thing › Below 300
  7. Vary: change one thing › Won't upload
  8. Vary: change one thing › Always on
  9. Practise: conditions › Which is true?
  10. Practise: conditions › Between
  11. A middle band
Start lesson
4Talking to the computerNot started

Serial.begin(9600) in setup() opens a line to the computer; Serial.println(level) sends a value and starts a new line, and Serial.print stays on the same line. The serial monitor shows what the board sees, which is how you test and debug a sketch. In C++, "words" + a number does not join them.

  1. What appears?
  2. Try: watch the monitor
  3. Worked example: print and println › Build a line
  4. Worked example: print and println › Find the mistake
  5. Vary: change one thing › Add a label
  6. Vary: change one thing › Nothing printed
  7. Practise: printing › Print and println
  8. Practise: printing › Debugging
  9. Print a percentage
Start lesson
5Outputs that varyNot started

analogWrite(9, value) sets a brightness from 0 to 255 on a PWM pin (marked ~ on the Uno: 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 the time it is on. analogRead gives 0 to 1023, so divide by 4 to drive it from a knob.

  1. 128
  2. Try: see the signal
  3. Worked example: knob to brightness › Scale it
  4. Worked example: knob to brightness › On how long?
  5. Worked example: knob to brightness › Find the mistake
  6. Vary: change one thing › Smooth
  7. Vary: change one thing › Not a PWM pin
  8. Practise: PWM › 75%
  9. Practise: PWM › Which pin
  10. Turn it round
Start lesson

Real hardware

A breadboard, a small system tested against its brief, and the same project on a Pico.

6From simulator to breadboardNot started

A breadboard joins the five holes in each numbered column (a–e, and separately f–j) with a metal strip; the rails along the edges join along their length. Each leg of a part must be in a different strip, and the loop goes from strip to strip through the parts.

  1. One column
  2. Try: find the strips › Look at column 8
  3. Try: find the strips › Fix it
  4. Worked example: trace the strips › Strip by strip
  5. Worked example: trace the strips › Find the mistake
  6. Vary: the gap and the rails › Across the gap
  7. Vary: the gap and the rails › On the rails
  8. Practise: which holes? › Which holes?
  9. Practise: which holes? › A short
  10. Build it from scratch
Start lesson
7A small systemNot started

A system reads a sensor, decides, and drives an output, and a brief says what it must do. Test it against a table of cases: each side of every threshold, the threshold itself, and the extremes. Tests that pass far from the threshold can hide a mistake right at it.

  1. Which test finds it?
  2. Try: run the tests
  3. Worked example: choose the tests › A test table
  4. Worked example: choose the tests › Find the mistake
  5. Vary: change the brief › Fix the boundary
  6. Vary: change the brief › Three states
  7. Practise: tests › A fan
  8. Practise: tests › All pass
  9. A fridge door alarm
Start lesson
8The same project on a Pico OptionalNot started

A Raspberry Pi Pico runs MicroPython. The ideas carry straight over (pins as outputs, reading a sensor, deciding, timing), but the words change, and so do two numbers: time.sleep takes seconds, and read_u16 gives 0 to 65535, not 0 to 1023.

  1. The threshold
  2. Worked example: line for line › Match the lines
  3. Worked example: line for line › Two numbers that change
  4. Worked example: line for line › Find the mistake
  5. Vary: put it together
  6. Practise: converting › Timing
  7. Practise: converting › What stayed
  8. Port the fridge alarm
Start lesson

Progress is saved in this browser only. Nothing is sent anywhere.

For teachers: Teacher guide (notes, answers, solutions, slides and worksheets)