Micro:bit Speed Gate

As part of our recent code club lessons we have been investigating how to measure acceleration using our BBC micro:bits.

The micro:bit has many different ways to measure acceleration so it seemed appropriate that we should enter ourselves into the fantastic Bloodhound SSC Race for the Line competition which involves constructing a car from a kit containing a foam block, two axles and four wheels and then participating in a race day where the Army attend and fire the cars down a track using real rockets!

You can find out more about the Race for the Line competition here, or watch the short video below if you just want to jump to the exciting bit!

Before we go and race our cars I wanted to be able to test them and give the pupils an idea of what speed they were travelling at. For the actual race day the Race for the Line team have an officially designed speed gate (see below) – but I didn’t want to purchase one just to test our cars, so I set about making my own using the BBC microbit!

ir_sensorsA few weeks earlier when I visited the BETT show and had spoken to the Race for the Line team they had said that the timing gates used IR sensors – so a quick internet search and I purchased two sets of these IR sensors.  Each set contains 1 x transmitter and 1 x receiver.  The reciever works fine from 3.3v so can be wired directly to the micro:bit +3.3v and Ground pins as well as one of the analogue input pins for the signal.  The transmitters will work best if powered from a 5v source so I chose to power them from two 2xAAA battery boxes wired together in serial (and using rechargeable batteries) to give me 5v.  I suspect normal batteries providing 6v would probably be ok too.batteries

In order to measure speed accurately I needed to have a fixed distance between the sensors so I cut two lengths of wood just over a metre in length and fixed the two transmitters exactly 1 metre apart on one length of wood – and then the recievers in a similar manner to the other piece.  The sensors are provided with a mounting hole which makes them easy to secure in place.
The two transmitters can be then wired to the battery packs (in serial) or some other 5v source as you see fit and that should be the transmitter bar complete.

For the receiver bar the sensors can be fixed in exactly the same way.  Wiring is a little more complex as you need to power the receivers with +3.3v from the microbit, and then connect the signal wire from each sensor to a different input pin on the microbit.  I resorted to using a few terminal blocks to keep things tidy, and also used a micro:bit with a mi:power board attached to keep things compact and portable.

The two yellow wires are wired to the yellow signal wires from the IR receivers and are connected to Pin0 and Pin1 on the micro:bit.  The 3.3v and Ground are then connected to both of the receivers too.

The result should be that you have two 1-metre long sensor bars – one powered by 5v for the transmitter LEDs and the other wired to the micro:bit to act as the receiver.

With the hardware completed I could turn my attention to the code.  I chose to use micropython as I wanted to show the speed in miles per hour and required some floating point calculation which is not supported by Blocks/PXT editors currently.


The first few lines (1-14) are setting up variables for gate1/2 which will be used to store the sensor input values (from the pin0/1 connections) as well as defining an image I wanted to use.

main_codeI then enter a loop (15-29) where I check the input from both Pin0 and Pin1 to provide the opportunity to line up the two sensor bars so that you know an IR signal is being received by both sensors. Exiting this setup mode is performed by pressing button A and button B provides a sensor value that may help with calibration.
After exiting the loop the sensors are being continually scanned (32, 33) and when the first gate (gate 2 in this case!) detects a break  in the IR beam from the transmitter (analogue signal drops below 200 – line 35) the start variable is set and the code then waits again for the 2nd receivers IR beam to be broken.

Once this occurs then the speed is calculated in miles per hour by working out how long it took to travel between the two sensors.  We can then calculate speed by dividing the distance (1 metre) by the time taken.  Lines 43-47 refer.

The speed is then displayed 3 times on the microbit before resetting and being ready for the next car.

The idea will be to fire our rocket cars (we’ll be using the air launcher to do this) between the two gates and to then get a reading from the microbit as to what speed the cars are travelling at.  I shall post a video when we do our test launches next week!

One thing worth mentioning – from my initial testing – is that the IR receivers are not filtered to block all non-IR light so this seems to work fine indoors but not in daylight.  Have fun finding out how fast things go!

Here’s the video of us doing our test launches – a big thank you to Peter Harrison and Mike Ford for all their help in getting this set up!

C a l e n d a r
S e a r c h
S o c i a l   M e d i a
S u b s c r i b e