Why Traffic Lights are good for Computing

I’m well known for promoting the simulation of Traffic Lights using Scratch so I thought it was about time I said why and explained how good they are for the new computing curric.

In the old days,  Data Harvest with with Flow-Go box and GO software were the No 1 method of engaging pupils in the practical computing.

The Flo-Go box has yet to be bettered (or equalled) in terms of its design/sturdiness and ease of use.

The Go software had many activities but no 1 in my mind was the concept of simulating Traffic Lights.  They even provided a 1 m high cardboard set of lights with Red/Amber/Green – a wait light and a switch for whole class room

The hardware was superb but the programming software was based on flowcharting concepts.

Which was fine at the time but then Scratch came along :)

So, although the hardware stuff was brilliant, Scratch was such a transformation in bringing programming to young people that I switched to doing all my teaching using it and abandoned  Flow-Go :(

I still loved using Traffic Light simulations to teach control concepts but missed having real lights to control.

2 Years Ago
The Raspberry Pi was launched with Scratch built into it and the capability of easily controlling its GPIO pins.  The PiFace team very generously shared their code with me and I’ve built up ScratchGPIO so that Scratch on Pi can be used to control lots of hardware plugged into the Pi and we can now do “real” Traffic Lights again :)

Using Traffic Lights as Teaching Tool.

You can approach this in various ways – I like to use it as a bit of Road Safety exercise and a test of observation skills to see who knows what the main TF sequence is.

It turns out not many young people know the full sequence and you can have a lot of discussion on why they go Green/Amber/Red – Red & Amber/Green

(TopTip: Always start off from Green as the sequence down is much simpler than the sequence up)

So then you can get your pupils to program up the Green/Amber/Red sequence.

The move onto the Red & Amber/Green up sequence.

Then we can discuss and get into the pedestrian crossing version with its flashing Amber.

Then add in Red/Green Man – and the Wait light – and the button

So with all this we have used sequencing, repetiton and hardware input/output .

But we can also use costume changes on a TF sprite to do the same (or as well as) as using LEDs

And have a baby doing cartwheels across a crossing when the lights go to Red :)

I love it :)



Arduino class-set in Year 6

Today I tried out a class-set of 15 Arduinos with LEDs with Win 7 laptops with a Year 6 class with past experience of Scratch.

First  issue was that I only managed to get 8 set-ups working before the start of the lesson!

This was due to driver issues – I thought I was OK as I’d used Arduinos in the past in the school but all machines wanted to install a new driver and only 4 of them decided that the ones it had already were suitable.

With all the others, I had to download the latest Arduino package and use the drivers in that.

After that, plain sailing using ScA (Arduino version of ScratchGPIO) to make some Red/Yellow/Green LEDs go on and off.

All groups had a full pedestrian crossing LED Sequence and Scratch cat walking across a Zebra crossing on the screen with quite a lot having cars going back and forth and one group had a baby doing cartwheels across the road!

Technically ScA throws an error if they save and then reload another script meaning it has to be killed and restarted but I’ll hopefully sort that out and get some input handling added to it so be can use switches as well.

As usual, forget to take pics – will try and do better next time.


Post Post

There may be as easy solution to the driver probelm!


Pin Trigger handling in ScratchGPIO

I was looking at some of William Bell’s RPiScratchIO scratch code and notice that he had the concept of receiving broadcasts on inputs pins changing state.

So I’ve started implementing this (in dev version)

Basic concept is that if an input changes state – it will send a broadcast message to Scratch.

So if Scratch has a hat block that says

On Trigger received
Say “An input has changed state” for 2 seconds
Broadcast ResetTrigger

then if  any pin changes from 0 to 1 (or 1 to o) then Scratch will run the On Trigger block.

To make sure that only one event is dealt with at a time, my handler will not send any more changes until it receives a ResetTrigger from Scratch which is why that Broadcast ResetTrigger command is at the end.

As well as this global trigger, each pin has its own trigger so to just detect a change in pin7 use

On TriggerPin7 received
Say “Pin7 has changed state” for 2 seconds
Broadcast ResetTriggerPin7

If using an addon that names the inputs (say Switch) then the following would be triggered on a switch press

On TriggerSwitch received
Say “The switch has changed state” for 2 seconds
Broadcast ResetTriggerSwitch


Sending messages between Raspberry Pi using ScratchGPIO

NB Only available in dev version

For a while I’ve wanted one computer running a Traffic Light program to easily talk  to a  Pi running as robot vehicle so that the robot can respond easily to the traffic light  sequence.

And extending this to another robots crossing the lights as vehicles or pretend pedestrians!

Link1I’ve added in a basic syntax for this

Link is used to setup connection to any other computer running Scratch with Remote Sensor Connections (RSC) enabled.  (All ScratchGPIO instances running on Pi are and it can be simply enabled on other computers by users)

Then simply, prefix any broadcasts that you want to send to the other computer with Send.  The other computer will receive the broadcast without the Send prefix e.g  Stop or Go

Ideas to enhance/change

Let Link work with just last octet of IP (ie. assume on the same LAN and automatically determine first 3 octets. (easy)

Try and come up with using last 4 digits of serial num like SID uses (hard)

Or something to make it less geeky if possible (hard)

Add a prefix/identifier to the broadcast so that receiving computer can identify/filter/differentiate non-local broadcasts (to prevent advanced Scratchers from taking control of a remote Scratch device without permission and make it possible to have more 1 computer sending messages into a remote computer)

The code is in the dev version (but liable to change) but please have a play and pass on your ideas/thoughts

Please remember my target audience are 7-11 year olds and their teachers :)



Change to Ultrasonic behaviour

Dev Version only

My ultrasonic handling code now runs in its own separate thread and defaults to returning an update distance measurement every 1 sec.

This can be change by creating an UltraDelay variable and using

set UltraDelay to xxx

If xxx = 0 then it will run as fast as possible which is currently about 5 times/sec or equivalent to xxx =0.2


You can also use

Broadcast UltraDelayxxx

if you prefer

Raspberry Pi SID testing using LAN->Laptop cable

Today I took my 6 Pi into one of my schools and connected them up to the school Win7 Pro laptops using a direct CAT5 cable and powered them from USB charger batteries.

They all performed fine technically and it was a great success (if I say so myself) :)

[I could only get 5 to work in the lesson as the last SD card imaging took over and hour instead of the usual 15 mins but that's a separate issue ]

By connecting them direct to laptops via cat5 cable means they didn’t need WiFi adaptors and therefore no configuring of the laptop was needed.

This is because I used this post to set up the Pi to automatically try and use an IP addressing the same range that Windows machines default to ( to

The reason for doing it this way is that I only have 1 school left with pupil desktop machines – all the rest use laptops/net-books/IPads so there is no chance of plugin any Pi into any monitors/main network ports.

The Pi are also set up to be SIDs  and therefore for the pupils to get controlling the addon boards plugged into them (mixture of PiDie,Pibrella,PiStep and Ladderboards) all they had to do was run the Handshake.py program (which I’ve wrapped up into a windows .exe file), saved it to a network folder and stuck a shortcut toit on the desktop.

Once they’d handshaked with SID running on their Pi, They simply loaded their normal Windows Scratch 1.4 and loaded in a basic script to switch all the LEDs off and On to test that everythgin was working.

From the start of a 1 hours lesson (and considering this was the first time we’d done it) 3 out of the 5 groups (only a 15 intake class luckily) had got a full Traffic Light -  Green/Amber/Red/Red and Amber/Green sequence going.

Lessons Learnt

All the AddOns responded to LED1..n On/Off commands except for the PiStep (as its normally used as a stepper) and that group needed to use Pin11,12 and 13 and it meant separate instructions for them so better to make sure all boards can respond to same commands of make sure individual help-sheets for each board are printed out.

Once group didn’t push their CAT5 cable in until it clicked (obviously as a tech I knew this and didn’t bother telling them – so I’ll make sure that’s in the instructions next time)

Also, I’m going to make a tick sheet of plug in sequence just to make sure its all done in a good order.

Laptop on and logged in, cat 4 plugged into laptop,cat 5 into Pi, power lead into Pi (this is the fiddly bit they found) and then power lead into battery supply.


LMC Emulator in Scratch

Updated 14May14 to combine instruction and data in one array value

There is a basic microprocessor concept called the LMC http://www.yorku.ca/sychen/research/LMC/ which introduces the concepts of basic assembly language programming using only 10 instructions.

I’ve made a Scratch version that is a working copy of the simulator.

The way to use it is to enter your instructions into the list called Memory and then press the run button.

The main difference between this version and the original LMC is in he entering of pure data.  To just enter a number into memory you would use:


The LMC computer will then ignore the DAT instruction and move the program counter to the next instruction

The INP instruction will pause and let you enter a value into accumulator.

The OUT instruction will cause the cat to say the value of the accumulator

All other LMC instructions (LDA,STA,ADD,SUB,BRA,BRZ,BRP and HLT ) should work the same as the original simulator.

The simulator is available here

Any feedback greatly appreciated :)


Version 5 updated to 5.1.26

New features

Support for Dr Simon Monk Raspirobotboard 2

Support for 4Tronix Pizazz Robot Kit

Input handling now responds to short lived events – very useful for Line Following Robots and Burglar Alarms etc

Camera Support courtesy of Matt Venn

Broadcast photo

This will take a photo and stick it in /home/pi/photos using Raspberry Pi camera

Basic Minecraft support

Broadcast MinecraftStart to connect to running Minecraft program

set MineX setMineY and setMineZ followed my MinecraftMove should move you to a the co-ordinates defined

or you can do Broadcast MineCraftCamMove to move the camera to those co-ords instead

Thats all it does so far

Other changes,

ADAFruit and 4Tronix I2C PWM and analogue input boards bow supported at a ll times wit ha ll add-ons

ScratchGPIO 5.1.15


This version made it to the stable version tonight.

New features

broadcast photo

This will take a photo and stick it in /home/pi/photos using Raspberry Pi camera

broadcast MinecraftStart to connect to running Minecraft program

set MineX setMineY and setMineZ followed my MinecraftMove should move you to a the co-ordinates defined

or you can do Broadcast MineCraftCamMove to move the camera to thos co-ords instead

Thats all it does so far

Other changes,

ADAFruit and 4Tronix I2C PWM and analogue input boards bow supported at a ll times wit ha ll add-ons


Uniqueness of Rasperry Pi Serial Numbers

My SID project needs the user to type in the last 4 digits of a PI’s serial num in order to connect remotely.

I chose 4 as I thought (intuitively) that it was enough digits to almost certainly be unique in a class of 15 Pi.

Of course,  if it takes one year 7 child x attempts in t time to get it right then imagine the number of attempts a class of 30 children working in “teams”  of two takes to do this.  then make it 2 digits – then 3 – then 4.

…….. so 30 mins later, all pupils have connected their laptops to their Pi :)

OK – lets try and me more scientific and find the probability of 1/2/3 or 4 digits letting us uniquely identify a Pi.

Turns outs to be one of those great maths problems called the “birthday problem” and that the probability of any 2 Pi out of 15 having same last digits of serial number is (in %) where r = num of Pi and d = number of hex digits is given by

a = (16^d)!
b = (16^d)^r)
c = ((16^d)-r)!

P(%) = 100*(1 – a/(b * c)

Substituting r = 15 and d =2 gives 34%

Substituting r = 15 and d =3 gives 2.5%

Substituting r = 15 and d = 4 gives 0.2%

So maybe we could get away with 3 digits but with say 30 Pi the numbers change to

Substituting r = 30 and d = 3 gives 10%

Substituting r = 30 and d = 4 gives 0.7%

So in a class of 30 you’d have to use 4 digits and so for compatibility for rich schools  – I’ve stuck with 4 digits :)

Just for an exercise, I wondered if I converted the serial num into Base 30 (using 0,1,2….x,y,z) for digits I get this

Substituting r =15 and d =2 gives 11%

Substituting r =15 and d =3 gives 0.4%

and even for a class of 30

Substituting r =30 and d =2 gives 38%

Substituting r =30 and d =3 gives 1.6%

So I’d say we could get away with three Base 30 digits.

But since its a lot of trouble to convetr from Base 16 to Base 30, I’m going to stick with 4 hex digits and stick them on the side of each Pi :)