You are on page 1of 10

instructables

Lap Counter

by Niubit

We are going to build a lap counter for the nish line of a circuit. Instead of a presence sensor, we are going to detect the
passage of cars with a simple LDR resistor and a LED. All this connected and controlled by micro:bit that will also serve as
display.
Supplies:

micro:bit
Yellow LED
LDR resistor
220Ω resistor
10KΩ resistor
Cable
Finish line (3D printed or cardboard)

Lap Counter: Page 1


Step 1: Finish Line

To arrange micro:bit and electronics in a convenient way, we are going to design a nish line that we will print on plastic
with a 3D printer.

https://www.tinkercad.com/embed/dEs0Yf1sKvK?editbtn=1

The upper piece will accommodate micro:bit and it will allow making the connections in a very simple way as we will see
in next step.

Lap Counter: Page 2


Lap Counter: Page 3
Lap Counter: Page 4
View in 3D Download

https://www.instructables.com/ORIG/F6V/WDBU/KTSNTM75/F6VWDBUKTSNTM75.stl

View in 3D Download

https://www.instructables.com/ORIG/F8X/63RJ/KTSNTM76/F8X63RJKTSNTM76.stl

View in 3D Download

https://www.instructables.com/ORIG/FGW/X0OF/KTSNTM77/FGWX0OFKTSNTM77.stl

Step 2: Circuit

We are going to connect an LDR resistor and a LED to micro:bit, following the circuit you see in pictures and in this
Tinkercad simulation:

https://www.tinkercad.com/embed/1RNDNdAhOv9?editbtn=1

To simplify assembly, we will directly solder the resistors to the special components (LED and LDR) and glue the cables
directly to the nish line frame. The nal micro:bit connections will be made by trapping the cables that will pass through
the holes provided in the micro:bit holder.

Lap Counter: Page 5


Lap Counter: Page 6
Lap Counter: Page 7
Step 3: Code

The control that we'll do to determine that a car has passed the nish line is based on detecting the decrease in the
brightness measured in the LDR resistor. Previously we will calibrate the installation by measuring the value obtained
from the LDR resistor with and without the car between LED and LDR. We will calculate the average value that we will
use as a threshold in the code to do the calculations.
Once we have determined the threshold, the code will be as simple as nding the rising edges, that is, when the
measured luminosity crosses the threshold from less to more light. At that time we will increase the counter and update
the micro:bit display.
After several tests with a simpler version of the code, we found that the measurements of the LDR resistor value show a
sinusoidal pattern that can confuse our calculations, by causing increases in brightness when the general trend is
downward. The solution to this problem was to store a series of previous values and take the mean to use as the nal
measure.
We use the MicroPython rmware for micro:bit and the Thonny editor to write and run the attached code.
To calibrate the code, load this program, cross several times the nal line with the toy car, and press A to get the
threshold.

Lap Counter: Page 8


import microbit
import utime

p0 = microbit.pin0
maximum = 0
minimum = 1023
reading = 0

while not microbit.button_a.was_pressed():


reading = p0.read_analog()
if reading > maximum:
maximum = reading

if reading < minimum:


minimum = reading

print("Threshold: " + str(round((minimum + maximum) / 2)))

Write down the threshold obtained and insert in the nal code:
import microbit
import utime

THRESHOLD = 713
DEBOUNCE_ARRAY = 12
DEBOUNCE_VALUE = 5

readings = [1024 for i in range(DEBOUNCE_ARRAY)]


index = 0

p0 = microbit.pin0
lap_count = 0
reading = 1024
last_reading = 1024

microbit.display.show(lap_count, wait=False, loop=True)

while True:
index = 0 if index == DEBOUNCE_ARRAY - 1 else index + 1
readings[index] = p0.read_analog()
reading = sum(readings) / DEBOUNCE_ARRAY
if abs(reading - last_reading) < DEBOUNCE_VALUE:
reading = last_reading

if last_reading < THRESHOLD and reading >= THRESHOLD:


lap_count = lap_count + 1
if lap_count < 10:
microbit.display.show(lap_count, wait=False, loop=True)
else:
microbit.display.scroll(lap_count, wait=False, loop=True, monospace=False)
utime.sleep_ms(200)

last_reading = reading

Download

https://www.instructables.com/ORIG/FAQ/5FVS/KTSNT4D1/FAQ5FVSKTSNT4D1.py

Download

https://www.instructables.com/ORIG/F4E/QSH7/KTSNT4DA/F4EQSH7KTSNT4DA.py

Step 4: Final Result

In this video you can see the nal result:

Lap Counter: Page 9


https://www.youtube.com/embed/fy1F_-ILsOY

This is so awesome! I love using a Micro:BIT in class, but I always felt that when you use a
breakout board to complete a project you lose a bit of the awesomeness of the device - it was
designed to be a cheap tool for teaching, but by having to purchase and add on components then
maybe you should just use something like a Humming Bird (IMO)... but this project fits in with my
idea of the Micro:BIT in education - the Board, an idea and cheap components to make the
project work - I will be making this for sure once virtual school is over and I have access to my
classroom again!

Lap Counter: Page 10

You might also like