Lap Counter

by Niubit in Circuits > Microcontrollers

1962 Views, 11 Favorites, 0 Comments

Lap Counter

IMG_20210920_130258.jpg

We are going to build a lap counter for the finish 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)

Finish Line

Finish line.png
IMG_20210920_131048.jpg
IMG_20210920_130959.jpg

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

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.

Circuit

IMG_20210920_130945.jpg
esquema_bb.png
IMG_20210921_102726.jpg

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:

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

Code

The control that we'll do to determine that a car has passed the finish 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 finding 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 final measure.

We use the MicroPython firmware 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 final line with the toy car, and press A to get the threshold.

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 final 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

Final Result

In this video you can see the final result: