Smart systems and connected devices

A smart crossroads with the micro:bit - from the LED screen to two traffic lights that talk to each other

smart systems
So far we have learnt how the micro:bit can show a picture, react to a button, count points, pick a random number and run a simple program.

Now we are going one step further.

We won’t just make a program that runs on one micro:bit any more. We will build smart devices that can control real lights, sound, sensors, motors and other parts of our models.

We will learn how one micro:bit can:

  • switch on a real LED;
  • control a traffic light;
  • notice what is happening around it;
  • send a message to another micro:bit;
  • receive a message and react to it;
  • work together with another device as part of a bigger system.

Imagine a traffic light at a crossroads. It isn’t just a set of lights. It has to know when the other direction is red, when pedestrians may cross and how to send a clear message to the people who are waiting.

That is an example of a smart system.

A smart system has several important parts:

Input ⇒ the program decides ⇒ output ⇒ communication with other devices

For example, at our crossroads today:

  • A button or a radio message
  • The micro:bit program decides what state the crossroads is in
  • The traffic light switches between red, yellow and green
  • The LED screen shows the signal for pedestrians
  • A sound sends an extra message to people with poor eyesight

From today, we will think like a team building a real device:

  • What should our device do?
  • What information does it need?
  • How will it show the result?
  • What happens when it works together with another device?
  • How will we check that it doesn’t make a mistake?

The most important rule stays the same:

First we make one small thing work. Then we test it. Only then do we add the next part.

Today we start with a smart crossroads – our first project in which our micro:bits will light up, communicate and work together.

A smart crossroads with the micro:bit

Today we will build a small smart crossroads.

First we will show a traffic light on the micro:bit’s built-in LED screen. Then we will move the same logic to our 3D-printed traffic light model with real LEDs. After that we will connect two traffic lights by radio, add a pedestrian light on the LED screen and, finally, add sound to help people with poor eyesight.

Our crossroads won’t be a real traffic system. It is an educational model that helps us learn:

  • how a program controls a light;
  • how the micro:bit sends a signal to external LEDs;
  • how two micro:bits exchange messages;
  • how one device can show information in several ways: with light, a picture and sound.

The most important rule of our crossroads: two directions must never have a green light at the same time.

Our mission

By the end of the workshop, each pair will have:

  • two 3D-printed traffic lights;
  • two micro:bits;
  • two directions at the crossroads;
  • external red, yellow and green LEDs on each model;
  • the micro:bit LED screen showing a pedestrian light;
  • radio messages that keep both traffic lights in step;
  • a short sound signal when pedestrians get the signal to cross.

Our system goes through several states

StepStateTraffic light ATraffic light BPedestrians at APedestrians at B
1Safety state before direction Aredredstandstand
2Direction A gets readyred + yellowredstandstand
3Direction A goesgreenredwalkstand
4Direction A gives a warninggreen flashesredstandstand
5Direction A stopsyellowredstandstand
6Safety state before direction Bredredstandstand
7Direction B gets readyredred + yellowstandstand
8Direction B goesredgreenstandwalk
9Direction B gives a warningredgreen flashesstandstand
10Direction B stopsredyellowstandstand
⇒Back to the startredredstandstand

For our educational model we use a simple rule:

  • Vehicles green ⇒ pedestrians in the same direction walk.
  • Vehicles green flashing ⇒ pedestrians in the same direction stand.
  • Vehicles yellow ⇒ pedestrians in the same direction stand.
  • Vehicles red + yellow ⇒ pedestrians in the same direction stand.
  • Vehicles red ⇒ pedestrians in the same direction stand.

In real towns and cities, traffic lights can have more rules, special times for pedestrians, sensors for cars and extra safety phases. Today we are building a simpler model so that we can understand the basic logic.

What we use

Each pair gets:

  • 2 × micro:bit V2 – The programmable “brains” of the traffic lights
  • 2 × 3D-printed traffic light – Models with real LEDs
  • 2 × mini breadboard – A place for the resistors and connections
  • 2 × battery pack – Power for the micro:bit after programming
  • Crocodile clips – Connect the micro:bit, the breadboard and the traffic light model
  • 6 × resistor – Each LED needs its own resistor
  • Wires from the model – Red, yellow, green and a shared black wire

On the micro:bit V2 we will also use the built-in LED screen and the built-in speaker.

Connecting the traffic light safely

Our 3D-printed models have four wires:

  • Red: Connected to the red LED
  • Yellow: Connected to the yellow LED
  • Green: Connected to the green LED
  • Black: The shared GND wire for all the LEDs

Each LED must have its own resistor. The red, yellow and green LEDs don’t share one resistor. The resistors aren’t inside the 3D-printed model. We put them on the mini breadboard.

The layout we have agreed on is:

  • micro:bit P0 ⇒ resistor ⇒ red wire of the traffic light
  • micro:bit P1 ⇒ resistor ⇒ yellow wire of the traffic light
  • micro:bit P2 ⇒ resistor ⇒ green wire of the traffic light
  • micro:bit GND ⇒ shared black wire of the traffic light

A micro:bit digital pin can send the state 1 to switch a signal on or 0 to switch it off. In MakeCode we use this to switch external LEDs on and off.

Digital signals and the micro:bit's pins

The micro:bit doesn't always have to send a complicated message. Sometimes a very simple decision is enough: ON (1) or OFF (0).

We call a message like this a digital signal.

A digital signal has only two states:

  • 1 = On / HIGH; the LED can light up
  • 0 = Off / LOW; the LED is off

On the micro:bit we use pins – the small metal contacts along the bottom edge of the board – to send a digital signal to an external device. The micro:bit has lots of pins, and for our traffic light we use P0, P1 and P2.

The rule before connecting

  1. The battery pack must be switched off.
  2. We connect the wires and the resistors.
  3. We check the connections.
  4. The leader checks the circuit.
  5. Only then do we switch on the power and test.

We never connect an LED straight to a pin without a resistor.

Exercise 1: A traffic light on the LED screen

Before the real model, we will make a traffic light on the micro:bit’s built-in LED screen.

The micro:bit has a small screen of 5 × 5 LEDs. In MakeCode we can use the show leds block and draw our own pictures. The LED dots that are lit make a symbol, a picture or a message.

A traffic light you control by hand

Let’s make three simple pictures:

  • Button A = red: STOP
  • Button B = yellow: GET READY
  • Buttons A+B = green: GO

We can use these symbols:

  • Red: X
  • Yellow: !
  • Green: ↑

Or we can draw our own pictures with the show leds block.

Plan before you program:
WHEN BUTTON A IS PRESSED
show the stop sign
WHEN BUTTON B IS PRESSED
show the get ready sign
WHEN BUTTONS A+B ARE PRESSED
show the go sign

What are we learning here?

  • Button = input
  • LED screen = output

When we press a button, the micro:bit receives information. Then the program decides which picture to show on the LED screen.

exercise 1

Quiz 1: The LED screen and events

1. What is the input in our hand-controlled micro:bit traffic light?
The inputs are buttons A, B and A+B. When we press a button, the micro:bit gets the information that it should run a particular part of the program.
2. What is the output in the first part of the exercise?
The output is the micro:bit’s LED screen. It shows the sign, picture or message that the program has chosen.
3. Why do we make the traffic light on the LED screen first, and not straight away on the real model?
First we want to understand the program’s logic without wires and without mistakes in the connections. Once the traffic light works on the screen, it is easier to move it to the real model.

Exercise 2: The green light flashes

Now we will add an important warning.

Before the green light changes to yellow, the green light will flash for a short time. The flashing tells drivers that the state is about to change.

The flashing plan:

  • Switch green on (1)
  • Wait
  • Switch green off (1)
  • Wait
  • Switch green on (2)
  • Wait
  • Switch green off (2)
  • Show yellow
  • Wait a little longer

To start with, let green flash two or three times.

In MakeCode we will use:

  • pause to wait,
  • repeat to flash a set number of times,
  • forever to keep repeating the whole traffic light cycle.
Plan before you program:
REPEAT ALL THE TIME:
show red
wait
show green
wait
repeat a few times:
show green
wait
switch green off
wait
show yellow
wait
  • pause says how long we wait.
  • repeat says how many times we repeat the same action.
  • forever says that we keep repeating the whole cycle.
exercise 2

Extra challenge

Change the waiting time when the light flashes:

  • 100 ms = a tenth of a second
  • 1000 ms = one second

Which kind of flashing looks better: faster or slower?

Quiz 2: Flashing and time

4. What does it mean when the green light flashes?
It flashes by switching on and off again and again. It isn’t a new traffic light colour; the green LED is just being switched on and off over and over.
5. What is the pause block for?
pause tells the program how long to wait before the next command. A bigger value means a longer wait and slower flashing.
6. How long is 1000 ms?
1000 milliseconds is one second. 500 ms is half a second.
7. What is the difference between repeat and forever?
repeat repeats an action a set number of times, for example three flashes. forever keeps repeating the commands for as long as the micro:bit is running.

Exercise 3: A real 3D-printed traffic light

Now we move the logic from the LED screen to the real model.

The micro:bit won’t just show a symbol on its own screen any more. Now it will use its pins to control real LEDs in the 3D-printed traffic light.

Connecting

First disconnect the micro:bit from the computer and from the batteries (if you are using them).

Then connect:

  • P0 ⇒ resistor ⇒ red wire
  • P1 ⇒ resistor ⇒ yellow wire
  • P2 ⇒ resistor ⇒ green wire
  • GND ⇒ black wire
exercise 3 wiring
Why do we use these particular wire colours?
We use red, yellow and green wires so that it is easier to match each light on the traffic light (the LEDs) with the micro:bit, and black is the usual colour for GND.

After connecting, check:

  • The black wire is on GND.
  • The red LED has its own resistor.
  • The yellow LED has its own resistor.
  • The green LED has its own resistor.
  • The crocodile clips aren’t touching each other.
  • The wires aren’t pulled too tight.
  • The leader has checked the connections.

Colour test

Before the automatic traffic light, we check each colour separately:

  • Button A ⇒ switch on only the red light
  • Button B ⇒ switch on only the yellow light
  • Buttons A+B ⇒ switch on only the green light
exercise 3 test

When we switch one colour on, the other two should be off.

If pressing button A switches on the green LED instead of the red one, we don’t change the whole program straight away. First we check:

  • whether the red wire really is connected to P0;
  • whether the wire has been mixed up with another colour;
  • whether the resistor is in the right place;
  • whether the shared black wire is on GND.

Moving the automatic cycle across

After testing, we use the pins instead of the LED screen:

  • Red:
    • P0 = 1
    • P1 = 0
    • P2 = 0
  • Yellow:
    • P0 = 0
    • P1 = 1
    • P2 = 0
  • Green:
    • P0 = 0
    • P1 = 0
    • P2 = 1

To make the green light flash, we repeat:

  • P2 = 1
  • wait
  • P2 = 0
  • wait

This way the same program idea controls a real light, not just a picture on the screen.

Quiz 3: External LEDs

8. Why does each LED in the traffic light need its own resistor?
The resistor limits the current through the LED and protects it from damage. That is why the red, yellow and green LEDs each have their own resistor on the breadboard.
9. Where do we connect the traffic light's shared black wire?
We connect the shared black wire to the micro:bit’s GND. GND is the shared return path for the current through the LEDs.
10. What do we do first, before we move a wire or a resistor on the breadboard?
We switch off the battery pack or disconnect the power. Only then do we change wires, resistors or crocodile clips.
11. What should we check if button A switches on the green LED instead of the red one?
We check whether the red wire is connected to the right pin, whether the wires have been mixed up, whether the program sends the signal to the right pin and whether the connections on the breadboard are correct.

Exercise 4: Two traffic lights talk by radio

Now each pair has two micro:bits and two traffic lights:

  • Traffic light A controls one direction of the road.
  • Traffic light B controls the other direction.

The traffic lights need to know when the other direction has green.

The micro:bits can send short radio messages without any wire between them.

Radio group

For two micro:bits to talk to each other, they must be in the same radio group.

Plan before you program:
WHEN THE PROGRAM STARTS:
radio set group [RADIO GROUP]
A radio group is like a channel on a walkie-talkie.

Pair 1 ⇒ radio group = 41
Pair 2 ⇒ radio group = 42
Pair 3 ⇒ radio group = 43
Pair 4 ⇒ radio group = 44

Only micro:bits with the same group number should react to each other. A radio group can have a value from 0 to 255.

First we test the radio without the traffic lights

On the first micro:bit:
WHEN BUTTON A IS PRESSED:
radio send number 1
On the second micro:bit:
WHEN THE RADIO RECEIVES A NUMBER:
show the number received
exercise 4A test

If the second micro:bit shows the number 1, the radio link works.

Only then do we connect the radio message to the real traffic lights.

Agreeing on the messages

Both micro:bits need to know what each number means:

  • 0 = Both traffic lights are red
  • 1 = Traffic light A is red and yellow, traffic light B is red
  • 2 = Traffic light A is green, traffic light B is red
  • 3 = Traffic light A flashes green, traffic light B is red
  • 4 = Traffic light A is yellow, traffic light B is red
  • 5 = Traffic light A is red, traffic light B is red and yellow
  • 6 = Traffic light A is red, traffic light B is green
  • 7 = Traffic light A is red, traffic light B flashes green
  • 8 = Traffic light A is red, traffic light B is yellow

The number 2 doesn’t mean green on its own. We agreed that message 2 means this state.

Notice: after message 4 comes the phase in which both traffic lights are red. It doesn’t have its own number because it is the same as the state for message 0, so we use the number 0 again.

The most important test

For each radio message, we check both models:

  • If traffic light A shows green,
    • traffic light B must show red.
  • If traffic light B shows green,
    • traffic light A must show red.

No message may give a green light on both traffic lights at the same time.

Can both traffic lights be red at the same time?
Yes. A red light on both traffic lights makes a safe pause, because no vehicles will drive through the crossroads.

From now on, each micro:bit is programmed separately.

The micro:bits don't have the same job:

One micro:bit is in charge of the traffic lights, and the other listens to the messages it receives and acts on them.

What is a function?

A function is a group of blocks that we give a name. When we need it, we don't put all those blocks together again; we just call the function by its name with the call block.

A function is like a recipe: we write it down once, and then we can cook from it as many times as we like.

Our function is called Svjetlo (Croatian for "light"). When we call it, we give it three numbers, for the red, yellow and green lights, in the same order as the lights on the traffic light, from top to bottom. In the code they are called crveno, zuto and zeleno (red, yellow and green). We call these numbers parameters:

  • 0 = the light is off
  • 1 = the light is on
  • 2 = the green light flashes

For example, call Svjetlo 1 1 0 means: red on, yellow on, green off.

Why do we use a function here?

  • For each state of the traffic light we have to set all three pins: P0, P1 and P2. Without a function, we would have to put the same digital write pin blocks together again for every state.
  • The program is shorter and easier to read. The call call Svjetlo 0 0 1 shows straight away which light is on, just like a row in the state table.
  • If we change something, for example the pin for a colour or how fast the light flashes, we only change it in one place.
  • Both micro:bits use the same function. Traffic light A and traffic light B switch their lights on in the same way; the only difference is when they call it.

The micro:bit in charge (the main one)   Traffic light A:

The micro:bit that listens   Traffic light B:

12. What do two micro:bits need so that they can talk to each other by radio?
They must be set to the same radio group. The radio group works like a shared communication channel.
13. Why does each pair get its own radio group?
Each pair gets a different group so that it doesn’t accidentally control another pair’s traffic lights. micro:bits in different groups should ignore each other’s messages.
14. Why do we test the radio first by showing the received number on the LED screen?
This way we check that the radio link works before we add the more complicated traffic light logic. If the number doesn’t arrive, the problem is in the radio group, the program or the power, not in the traffic light’s LEDs.
15. Can both traffic lights show green at the same time?
No, they can’t. In real traffic that could cause a crash. Our program doesn’t check this with a special block. The safety comes from our plan: the main micro:bit chooses the order of the states itself and sends the agreed number for each state, and no number means green for both directions. Between the two directions there is always a phase in which both traffic lights are red. That is why, when we test, we look at both traffic lights for every message.

Exercise 5: A pedestrian light on the LED screen

The external 3D-printed model shows the lights for vehicles. We use the micro:bit’s built-in LED screen as the light for pedestrians.

We will make two pictures:

  • A person standing ⇒ pedestrians wait
  • A person walking ⇒ pedestrians can cross

Pedestrian standing

You can draw a picture like this in the show leds block:

. . # . .
. # # # .
. . # . .
. # . # .
. # . # .

Pedestrian walking

You can draw a picture like this:

. . # . .
# # # # .
. . # . .
. # . # .
# . . # #

It doesn’t have to look exactly the same. What matters is that you can tell the difference between a person standing and a person walking.

The pedestrian rule

On one micro:bit:

  • If vehicles have green:
    • the pedestrian walks
  • If vehicles have flashing green:
    • the pedestrian stands
  • If vehicles have yellow:
    • the pedestrian stands
  • If vehicles have red:
    • the pedestrian stands

This way one micro:bit controls two different outputs:

  • External LEDs ⇒ the traffic light for vehicles
  • LED screen ⇒ the light for pedestrians

If you look at the rule above, you will see that the pedestrian only walks when the green light is on all the time. That means we can add this behaviour to our earlier code (from exercise 4) as a call to the function pjesak (Croatian for “pedestrian”), and only in the part of the code that deals with the green light (the zeleno parameter):

  • If green is on (value 1):
    • The pedestrian walks
  • Otherwise (value 0 or 2):
    • The pedestrian stands.

The function’s parameter is called hoda (“walks”): true means the pedestrian walks, and false means the pedestrian stands.

exercise 5

Quiz 5: The pedestrian light

16. What does the micro:bit's LED screen show in this exercise?
The LED screen shows the light for pedestrians: a person standing when pedestrians wait, and a person walking when pedestrians can cross.
17. What should the pedestrian light show when vehicles in the same direction have green?
It should show that pedestrians walk. While vehicles in the same direction are going, pedestrians may cross the road.
18. What can the pedestrian light show when vehicles in the same direction have red?
It can show a person standing, because in our model pedestrians have to wait then.

Exercise 6: Sound to help pedestrians

Now we add sound.

A question to think about:

How can a person who can't see the traffic light find out that it is safe to cross the road?
One answer is a sound signal.

When the pedestrian light shows a person walking, the micro:bit can play two short tones:
Show a person walking
Play a short high tone
Wait
Play another short high tone
When the pedestrian light shows a person standing, it can play one short lower tone:
Show a person standing
Play a short lower tone

The MakeCode block play tone can play a tone of a certain pitch and length. We set the tone with a frequency and a time in milliseconds. We will use the in background option, so that playing the sound doesn’t get in the way of the traffic light working normally.

The sound rule in the workshop

  • We test the sound briefly.
  • We don't play a tone non-stop for the whole green state.
  • When several pairs are working, we test the sound when the leader gives the signal.
Sound isn’t just a fun effect: it is another way of passing on important information.

A good device can send a message in several ways: with light, a picture and sound.

Task: Add suitable sounds to the pjesak function.

exercise 6

Final challenge: Show your crossroads

Each pair shows one full traffic cycle.

Before you show it, check:

  • Both traffic lights use the same radio group within the pair.
  • Different pairs use different radio groups.
  • Each LED has its own resistor.
  • No crocodile clips are touching each other.
  • The LED screen can be seen.
  • At no moment is there a green light on both traffic lights.

Check that everything works as shown in the traffic light state table.

What did we learn?

  • The micro:bit can control its built-in LED screen and real external LEDs.
  • The micro:bit’s buttons can be inputs, and the LED screen and external LEDs can be outputs.
  • A green light can flash by switching the LED on and off again and again.
  • pause sets how long the program waits.
  • repeat repeats an action a set number of times, and forever keeps repeating the program.
  • Each external LED needs its own resistor.
  • The shared black wire of our traffic light is connected to GND.
  • Pins P0, P1 and P2 can control the red, yellow and green LEDs.
  • Two micro:bits can exchange radio messages when they use the same radio group.
  • Different pairs use different radio groups so that their traffic lights don’t get in each other’s way.
  • Radio messages must have a meaning that has been agreed in advance.
  • A pedestrian light can be shown on the micro:bit’s LED screen.
  • In our model, pedestrians walk when vehicles in the same direction have a steady green light.
  • Sound can help people with poor eyesight find out that the state has changed.
  • Two directions at a crossroads must never have a green light at the same time.