← Arduino

Same circuit, new board

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.

Most electronics kits, sensors and project guides assume an Arduino. The circuits are the ones you already know. What changes is the board, and then the language.

This is an Arduino Uno. Like the micro:bit it has pins, but they are named by number (0 to 13, plus A0 to A5), and it is programmed in C++. The board on this page runs the sketches for you; in Tinkercad Circuits you can build the same thing with blocks and see the C++ beside them.

void setup() {
  pinMode(13, OUTPUT);     // pin 13 is an output
}

void loop() {
  digitalWrite(13, HIGH);  // pin 13 on (5 V)
  delay(500);              // wait half a second
  digitalWrite(13, LOW);   // pin 13 off (0 V)
  delay(500);
}
WidthMore contexts, same idea.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

Which pin?

The LED (with its resistor) is wired to pin 12. The sketch above uses pin 13. When it runs, the LED…

TryTry it on the board and compare the result with your prediction.

Try: run it

Try it yourselfYour edits are saved in this browser.

Upload and run the sketch. What does the LED on pin 12 do?

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: read the pins

Worked exampleBefore each step, say what you think comes next.
  1. pinMode(13, OUTPUT)\text{pinMode(13, OUTPUT)}Sets pin 13 up as an output. Pin 13, not the LED's pin 12.

Now find every line to change.

Which lines must change so the sketch blinks the LED on pin 12?

Sam's LED is on pin 13. It doesn't blink: it just glows very dimly now and then. Click the line where Sam's sketch goes wrong.

ModifyChange the problem on purpose and see what happens.

Vary: change the pin

Try it yourselfYour edits are saved in this browser.
Need a hint?

Three lines name a pin.

Another hint

pinMode(12, OUTPUT); digitalWrite(12, HIGH); digitalWrite(12, LOW);

Change the sketch so the LED on pin 12 blinks. Upload and run it.

Use the board. This ticks when you get there.

Modify TypicalChange the problem on purpose and see what happens.

Build it in Tinkercad

PractiseA few on your own, to check you have it.

Pins

A buzzer is wired to pin 8. Which line sets it up?

An LED glows very dimly instead of lighting, and the pin numbers all match. What is the most likely cause?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Your own sketch

Try it yourselfYour edits are saved in this browser.
Need a hint?

setup() runs first: set the pin up there.

Another hint

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

Another hint

Every line ends with ;

The LED is wired to pin 7. Write the sketch to switch it on (and keep it on).

Use the board. This ticks when you get there.

For teachers

Students run a blink sketch whose pin number doesn't match the wiring, fix every pin number, see the dim pull-up glow of a missing pinMode, build the circuit on an Uno in Tinkercad blocks and read the C++ beside them, then write a short sketch for a new pin. About 40 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

The board in the lesson runs a subset of Arduino C++ against a model of an Uno (5 V pins, an LED and resistor per pin, simulated time). It behaves like the real board for the things these lessons teach: a missing ; stops the upload, a pin written HIGH without pinMode glows dimly (the internal pull-up), = in an if assigns. It is not a full compiler; real hardware comes in the next unit.

Tinkercad: Arduino Uno R3 is in the components list; Code › Blocks + Text shows the generated C++ beside the blocks (it can't be edited until you switch to Text). Same class code as the micro:bit unit.

Running the lesson

  1. Predict: ask students to point at the pin number in each line of the sketch.
  2. Find the mistake: the dim glow is a real effect students will see on hardware; name it the pull-up.
  3. Tinkercad: ask which block made which line of C++.

The hard part (on purpose)

C++ looks dense after blocks. Read it aloud line by line and point at the pin number in each.

When students get stuck

Stretch and support

Stretch: Wire a second LED on pin 9 and blink them in turn.

Support: A printed sketch with the pin numbers highlighted.

Where it leads

Next: Reading the C++ (what setup() and loop() do, and timing with delay).

Circuits with the micro:bit: the same loop and resistor ideas.

Sources

What each step is for

Now you can
Next lesson2. Reading the C++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.