microbit Lap Counter For Scalextric Like Track
If you were at the BETT show this year, you might have seen the microbit controlled cars that we had on our stand. It was a fairly simple set up but it did get a lot of attention. Our good friend, David Booth, has gone one better and designed and built a microbit lap counter for a Scalextric type car track and we think it's fantastic.
After gaining a degree in Electrical Engineering at Imperial College David went through a graduate apprenticeship and then worked on an autopilot for the Sea Dart Missile. He designed and patented a digital test set for military servo systems. David spent 6 years in Canada doing similar work, but for different companies, before returning to the UK where he opened a UK branch of IP Sharp Associates and introduced APL to the UK market for commercial and engineering applications.
The launch of the ZX81 and later the Spectrum prompted David to take up programming for fun and he learned to code in machine code, Visual Basic, C and more recently, Python and Scratch. David also enjoys breathing new life into old toys, such as old Scalextric sets, which he traditionally did with 80’s electronics. Since the launch of the BBC microbit he has revisited his old electronics projects as well as ensuring that all of his grandchildren have their own microbits and a healthy interest in learning how to use them.
microbit Lap Counter For Scalextric Like Track:
Racing Scalextric cars is much more fun if you can keep a count of the laps, have a fastest time to beat and get caught out if you jump the gun. So here is a microbit script to do just that.
This lap counter uses a BBC microbit to:
- Count the laps of a race for each of two cars in a race from 5 to 35 laps long (6 options).
- Time the fastest car. Display the winning car and its time.
- Start the race with a Formula 1 style start with five LEDs lighting up in sequence. The race starts when all the lights go out.
- Catch and indicate any car that jumps the gun. That car has to do a Stop/Go penalty to reset their LED or be disqualified.
- On start up - select the number of laps in a race by tipping the microbit.
This project is programmed in PXT and is fully documented and easy to modify.
These functions were first designed and implemented using 16 Dual In Line (DIL) components on three circuit boards with numerous resistors, capacitors, two 7 segment displays and a hacked sports stopwatch, taking months to build and test and totally inflexible. In contrast, today it can all be done by one microbit, two sensors and 16 components (max) and took 3 weeks to design and build. What is more, it can be improved just by re-flashing. And this is why microprocessors have taken over the world, to be found in every product from a washing machine to the many used in automobiles and industrial plant.
- BBC microbit with its battery or source of power.
- Edge Connector Breakout Board for BBC microbit - Pre-built.
- 10 M/F jumper wires.
- Two QRD1114 Reflective Object Sensors or equivalent
- Two 3.3V Zener Diodes.
- Two 1N4001 silicon diodes or 1N4148 (smaller) or equivalent silicon diodes.
- Two of the following; 120 Ω resistors, 10 kΩ resistors.
- Components that may vary depending on your track include; two 100 µF 25V electrolytic capacitors, two resistors of 4k7 or 1k2 depending on the maximum voltage of your track and two optional resistors of about 10k.
- MDF 3mm sheet to make a simple bridge over the track to hold the proximity sensors, see dimensions below under the section Construction plus 3 off, M3 nuts, bolts about 2cm long and washers.
- Various coloured wire.
- Strip Board or small prototype breadboard PBU301.
- Scalextric or similar rail-car set.
(Note: The PXT microbit editor does not show the working of the.hex code correctly.)
The lap counter works by shining an infrared LED light onto the top of a racing car (best if it is a white top) and getting the reflected light to make a transistor conduct like a switch. There should be about 3-5 mm between the detector and the top of the car.
Beware however that if the racing track is in strong sunlight the sensors will pick up the sun's infrared and saturate. You will be able to tell if this happens; if after the start sequence LEDs at x = 1 y = 0 (1, 0) and x = 3, y = 0 (3, 0) come on and stay on and the count does not progress as cars pass under the sensor. So just shade the sensors.
The LEDs in columns 0 and 1 are used to count car A’s laps and LEDs columns 3 and 4 are used to count car B’s laps. Fig 1 shows how laps are counted (up to 35) see examples below.
Each lap a LED is lit. So for car A lap one is shown by a LED at x = 1 y = 0 (1, 0).
Lap two lights the LED at (1, 1)
Lap five lights the LED at (1, 4)
Lap six lights the LED at (0, 0) and puts out all the LEDs in column x = 1
This is like counting in 10s only this is counting in 6s.
A similar thing happens in columns x = 3 and x = 4 for car B. See fig 1 above.
A False Start is detected by measuring the track voltage during the start sequence and feeding that to pin P2 for car A and P8 for car B. Since the maximum track voltage can be from 5 V to 12 V a 3.3V Zener diode must be used to protect the microbit and the leads to pin P2 and P8 must be tested with maximum input voltage before they are connected to the microbit pins. As soon as the voltage on pin P2 (or P8) drops below about 2V the LED goes out. So assuming the car is at full speed = 5V for some tracks or 12V for Scalextric then the car voltage has to be significantly reduced to reset the False Start LED which can only be extinguished by stopping the car for about 1 second determined by components, shown in the circuit diagram below and explained later. This is a Stop – Go penalty.
Initial testing before construction work:
Load the script into the PXT editor and see the documentation attached to many of the blocks.
The PXT editor microbit emulation shows the Start Up correctly but when button A is pressed it fails to count touches to pins P0, P1, P2 and P8. So it is best to see its action by flashing the code onto a microbit and use a flying lead from the +3V tab and touch the pins P0, P1 to see the lap count work and clip onto pin P2 to see the false start work.
Load the Script’s .hex file onto your microbit. When a source of power is applied to the microbit an ON START script asks you to set the maximum number of laps in a race. You do this by tipping the microbit to the right (positive x acceleration). LED’s are lit to indicate laps for car A as stated above (B gets the same length race automatically). Column x =1 (the second column) will always light up with 5 LEDs indicating 5 laps. You then add an extra 6 laps at a time for each LED lit in column x = 0, by tipping the microbit to the right. Use the formula max laps = (n * 6) + 5 where n is number of LEDs in column x = 0.
Press button B when you have the desired number of laps and a message confirming the number of laps appears. So you can select 5 or 11 or 17 or 23 or 29 or 35 lap races. If you want to change the number of laps press the microbit reset button. This number of laps remains while power is on the microbit or the reset button is pressed. While it might sound complicated just looking at the lit LEDS tells you how many laps have been selected. For testing purposes select a 5 lap race.
Put a banana plug (or crocodile clip) and red lead onto the 3V tab to test the script. You will use the other end to simulate laps and false starts. Press Button A to see the top five LEDs come on at 1-second intervals and then they go out together after a random interval to indicate the start of the race, like Formula 1 starts. Use the other end of the 3 V lead to touch P0 and see the lap count for car A increase by one (more than one LED may come on if your contact is intermittent). Try the same with pin P1 (car B). Keep touching the pins until all 5 LEDs in column x = 1 are lit for one of the cars and, assuming you selected a 5 lap race, you should get a message saying which car has won and how many seconds it took.
Now clip the other end of the 3V lead to pin P2 simulating a false start and press button A. After the Start LEDs go out, the LED at (2, 0) lights to indicate that car A jumped the start. Remove the 3V from pin 2 and the LED at (2, 0) goes out since the car has been stopped, a Stop/Go penalty. If you have access to pin P8 through a breakout board, test Car B using pin 8 as above. Failure to stop causes a disqualification to be indicated by a X.
Fig 2 shows the total circuit in three parts. The lap counter uses two Fairchild QRD114 (see the specification sheet). Be very careful when reading the physical configuration to identify the correct pins as the sheet shows pins from above and below the component without indicating which is which. Also, other proximity sensors may have different pin configurations. The transistor and LED are push fits into their case and can come out. Make sure they are both flush with the case top when in use.
The second part of the circuit detects false starts. Before coupling this circuit to pins P2 and P8 be sure to test the voltage across both 3.3V Zener diodes to make sure that when the full car supply voltage is applied to the track the voltage across each Zener does not exceed 3.3V relative to GND. If it does it could burn out the microbit which is specified at 3.6V maximum. It may only be 3V which is fine. If the voltage does exceed 3.3V try another Zener.
The capacitor C1 serves two functions. Firstly it smoothes out the erratic voltage from the track that could easily look like the car had stopped when it had not. But more importantly, along with resistor R3, it determines how long the car has to stop before its False Start LED goes out. On my Scalextric track, a 100 µF electrolytic capacitor with no resistor R3 forced the car to stop for just over a second, but a lot depends on how your car controller is wired up, so experiment. The time is determined by (but not exactly) R3 in ohms x C1 in Farads (that’s the micro Farad number divided by 1000,000) and is called a time constant. E.g. 5k6 x 100µF = 0.56 seconds.
This circuit has a diode D1 (1N4001 or 1N4148 or equivalent) to help protect the Micro:bit if you get the track voltage the wrong way round.
The third part of the circuit is your choice of racing car track, controllers and cars and not part of this design.
You will need to make a “Bridge” to hold the two GRD1114 proximity detectors about 1 mm over the top of a car on the track.
If you have a, “My First Scalextric” set, then you may well have a bridge already, that takes the “figure of 8” track over itself, fig 3. Whatever you construct, arrange for the proximity sensors to face down over the car so that when a car passes underneath the sensor face is 3 - 5 mm above the white car top and centrally located.
If you have to make a bridge, Fig 4 a & b, is the simplest and quickest way to do it. This can be knocked up in 20 minutes from a small sheet of 3mm MDF and the dimensions are given in fig 5. The pieces do not even need to be glued, if you are careful, as the three bolts secure the sensors and vertical pillars making it very easy to adjust the sensor height. Just make sure my dimensions are wide enough for your track.
Assemble the top of the bridge and clamp the sensors in place. Then slip insulation sleeves over the pins 1 and 3 and solder them together (+3V). Solder the resistors on top of the sensors so you only need four leads running off the bridge.
The four leads are, +3V, GND and two leads to pins P0 (car A) and P1 (car B). Solder the +3V ends of R1 and R2 together which will couple to GND. Solder the four leads. Fit heat-shrink protection over the bare leads or paint them (not as good).
Wire the circuit up to a Kitronik pre-built breakout board 5601B using the pack of 10 jumper wires which have one female end that fits over the pins on the breakout board. Solder these to the four wires from the bridge and the three leads, GND and two track connections from the racing track (or their controllers).
The bridge will be the finish line. So make the start line two cars lengths past the bridge.
The Stop/Go Circuit:
Solder the 10 components to a small piece of Strip Board. One possible layout is shown above in fig 6 and the end result, in fig 7.
Wire up the connection to the track. In the case of a Scalextric track, remove the backing plate of the power section of track, fig 8. They have used black wires for GND and red wires for the control input. You need to connect one wire to the black which is common to both tracks and a second and third wire to the two red leads. If you have some other race track system you will have to identify the ground/earth connection and the two live rails with a meter.
Wire the circuit up to a Kitronik pre-built breakout board 5601B using the pack of 10 jumper wires which have one female end that fits over the pins on the breakout board. Solder these to the four wires from the bridge and the three leads, GND and two track connections from the racing track (or their controllers). See figs 2 & 6.
The bridge will be the finish line. So make the start line two cars lengths past the bridge.
Now test the setup. Plug a battery into the microbit. Flash the Lap Counter hex file if not already done. Set the maximum race length to say, 5 laps for testing purposes. Then plug the microbit into the Kitronik pre-built breakout board. Check that the cars run around the track and pass freely under the bridge.
Press button A on the microbit and wait for the 5 LEDs to go out and start racing. Hopefully, the counters will work and when the correct number of laps have been completed the winner and its time will scroll out.
Now try the False Start. Press button A and before the lights go out start car A racing. The LED at (2, 0) should come on to show the false start. So stop the car and the LED should go out. Then continue racing.
Check car B in the same way.
- Download the zip file here.
Note: Before sending the dxf file to be laser cut, measure the height of the car and the track to ensure that the height of the DIY bridge is suitable. Adjust accordingly.