You are on page 1of 25

HO Slot Car Lap Counter/Timer

Scott Neumann
scott.neumann@ieee.org
March 9, 2023

This design is for an HO slot car two or four lane lap digital counter and timer that uses a Raspberry Pi
(see https://www.raspberrypi.com) and phototransistors to detect the passing of a slot car through the
lap counter. This design includes overhead LEDs for an illumination source, noting that some
commercial electronic lap counters depend upon adequate ambient light.

Figure 2: Two Lane Configuration


Figure 1: Four Lane Configuration

There is also a ‘Light Bar’ component that can optionally be used with some additional wiring for three
colored LEDs. This provides the ability to show race status (e.g. running, caution, stopped). This can
easily be added later if desired.

Figure 4: Four Lane Figure 3: Two Lane


Configuration with Light Bar Configuration with Light Bar
Figure 5: Full Configuration

A graphical user interface is provided that runs on the Raspberry Pi. It is designed to allow for
operation without a keyboard, via a touch screen display. However, a mouse and keyboard can be used
if desired.
Using the ‘-lanes 2’ option on the command line, a 2 lane user interface can be generated, where data
for lanes 3 and 4 are not shown.

Notes:

• This document will focus on the four lane design. The two lane design simply uses less
components and circuitry, with some additional exceptions as noted.
• Supports Autoworld, Tomy and Tyco tracks… noting that a different base component is used for
Tyco tracks as they have slightly different cross section dimensions than other brands of track.
• This document will reference files on Thingiverse.com that can be used for printing needed
parts.
• The use of a Raspberry Pi is not required, but in such case custom software and/or circuity
would be needed. A version 3 or 4 Raspberry Pi is recommended. Due to recent world supply
chain issues, it will be difficult to purchase a Raspberry Pi until 2Q 2023.
• More features could be added to the circuitry and software, some additional features are already
in the works.
• There are many options for wiring the circuity of the lap counter to the Raspberry Pi. These are
discussed later in this document.
• In rare cases, some cars (particularly Formula 1 or clear colored cars) might not work well, as
they do not block out light when they cross the sensors.
• In the future the use of a Raspberry Pi Zero 2 will be examined. See
https://datasheets.raspberrypi.com/rpizero2/raspberry-pi-zero-2-w-product-brief.pdf

Parts List
This design requires the following parts:

• Four 20mA white LEDs (important that they have a 5mm diameter, which is common)
• One 100 ohm resistor (100 ohms recommended, could be in the range 50-200 ohms depending
on the LED characteristics). In the case of a two-lane configuration with only two LEDs, use a
200 ohm resistor instead of the 100 ohm resistor.
• Four LPT2023 NPN phototransistors (recommended due to needed small 3mm diameter).
There is a note later about using photodiodes instead of phototransistors.
• Four 220K ohm resistors (220K recommended, could be greater to increase sensitivity)
• Raspberry Pi, any model with 40 pin interface bus (e.g. 2, 3, 4). Other models might require
adjustments to wiring.
• Power supply for Raspberry Pi (2 amp minimum)
• Seven inch HDMI touch screen (or substitute)
• HDMI cable (connects touchscreen to Raspberry Pi)
• USB cable (connects touch screen to Raspberry Pi)
• Jumpers (e.g. Dupont breadboard jumper wires)
• Wire
• Solder
• Electrical tape
• 3D printed parts (created using STL files downloaded from Thingiverse.com)
• Two HO straight track sections
• Python program for user interface and detection of state changes on the Raspberry Pi GPIO bus

Optional Light Bar parts:

• Three 5mm LEDs, red, yellow and green


• Three 330 ohm resistors
• 3D printed LightBar (replaces the Cover)

3D Printed Parts
The 3D printed parts are defined using an OpenSCAD design. The parts are specifically:

1. A base for connecting to track and circuitry for phototransistors and resistors, noting that
there is a separate STL file for use with Tyco track
2. Two side supports with hollow paths for wires between base and beam (need to cut off the
skirts after printing)
3. Top beam that holds illumination LEDs and resistor
4. Cover for beam
5. Optional light bar replacement for cover, provides for three LEDs to show state of race (e.g.
start, stop, caution)

Note that if you are using Tyco track, there is a different base you need to print, as there are cross
sectional differences. All of the other parts are however the same.

You can download the STL files for these parts from the Thingiverse.com site at
https://www.thingiverse.com/thing:5805734. Parts for the two lane lap counter version are at
https://www.thingiverse.com/thing:5806620. These parts can be printed using PLA, PLA+, PETG or
ABS.
Figure 6: Design View in OpenSCAD Figure 7: 3D Printed Parts

These parts will all snap together without the need for glue or screws. The two HO slot car sections will
sit side by side on the base section. The electronic components will be placed on the base and overhead
beam, with wires between running through the hollow side supports.

Figure 8: Light Bar (optionally replaces Cover)

Lap Counter Circuitry


The following is the lap counter circuit diagram. It uses both the 5 and 3.3 volt sources from the
Raspberry Pi bus. The LEDs positioned in the overhead beam for illumination are powered by the 5
volt source and use R1 as a load resistor. The phototransistors are connected using a resistor connected
to the 3.3 volt source. Each connection between an R2 resistor and a phototransistor is connected to one
of 4 GPIO ports. The basic idea is that a slot car passing between the LED and the phototransistor will
cause the GPIO pin to raise from a 0 to a 1 state. Normally when no slot car is between the LED and
phototransistor, the GPIO pin will be at a 0 state.
Optionally, the cover for the beam can be replaced with a light bar. This would allow red, yellow and
green LEDs to be displayed for start, stop and caution modes.
The following is the general circuit diagram used for wiring the lap counter components. As can be
seen in the diagram the circuitry is reasonably simple.
Figure 9: Circuit Diagram

The key in the circuit is that the voltage levels of pins GPIO22-25 should go to an ON (high voltage)
when a car passes over the phototransistor and blocks light. Otherwise these pins are normally at an
OFF (low voltage) state.
Resistors
Recommended resistor values are:
• R1 = 100 ohms for four LEDs (i.e. 4 lanes), 200
ohms for two LEDs (i.e. 2 lanes).
• R2 = 220K ohms
• (optional) R3 = 330 ohms

Figure 10: Resistor Color


Codes
Phototransistors
The basic phototransistor circuit is as follows. This will cause the RPI GPIO pin to go high when a slot
car blocks the light from the LED.

Figure 11: Phototransistor Operation

This design uses four (or two2for a 2 lane configuration) LPT2023 NPN phototransistors (Jameco part
number 112176). These will be placed under the track where 3mm holes have been drilled. The higher
the resistor value, the more sensitive the circuit. Overhead LEDs are used to insure an adequate light
source.

Figure 12: Phototransistor Pins


Figure 13: 3mm Phototransistors
Note that a phototransistor does not have a base pin. These have a 3mm diameter. The detailed
specifications are found at https://www.jameco.com/Jameco/Products/ProdDS/112176.pdf.

An important note is that the phototransistors could be replaced with photodiodes, but depending upon
the parameters of the photodiode, the R2 resistors would likely need to be adjusted with a different
resistance value. Some testing has been done using a 3mm uxcell photodiode, where a 330K resistor
was used for R2.

LEDs
The illumination LEDs are inserted in the overhead beam of the lap counter in order to insure an
adequate light source for the phototransistors. Some commercial sensors unfortunately rely upon
sufficient ambient light, which is sometimes not adequate.

The pinout for the LEDs is shown by the following diagram. This design requires four 20mA white
LEDs. These should have a 5mm diameter.

Figure 15: Figure 16: LEDs (white)


LED Pins
Figure 14: Colored LEDs
in optional Light Bar

Each LED is connected to a 5v source voltage and use a common load resistor (R1). Resistors should
always have a load resistor. When the Light Bar is configured, it will use an R3 resistor for each of the
three LEDs, where red, yellow and green colors are used.

LCD Touch Screen Display


The user interface is driven by a Python program running on the Raspberry Pi. A touch screen display
is used to avoid the need for a keyboard. This connects to the Raspberry Pi using HDMI and USB
connections, where the USB connection uses power from the Raspberry Pi.

See http://www.lcdwiki.com/7inch_HDMI_Display-C for details and installation instructions on one


specific capable, but low cost product option. Optionally, any other HDMI touch screen can be used. If
a touch screen is not used, you would just need to also use a keyboard which might be inconvenient at
times.

Raspberry Pi
The Raspberry Pi is a low cost computer that runs versions of
Linux and is commonly used for projects. The Raspberry Pi has an
interface bus which will connect to the lap counter circuit and run
the software that provides the user interface (a Python program).
There are many versions of the Raspberry Pi. The version shown
here is version 2B. It has an HDMI port and four USB ports. It
uses a micro SD card for storage. It is powered by a 5 volt power
adapter (be sure to use one that provides at least 2 amps of power).
Without modifications, this design should support any Raspberry
Pi with a 40 pin interface bus.
See https://www.raspberrypi.com for details. There are a wide Figure 17: Raspberry Pi
variety of online resources.
Other processors (e.g. Arduino) could be used, but you would need
to write different software and change the bus mappings. The pinout of this bus is shown as follows
(See http://raspberrypi.com ):
Figure 18: Raspberry Pi Bus (source: http://raspberrypi.com )

Note that there are both 3.3 and 5 volt sources, and multiple ground pins. There are many general
purpose I/O (GPIO) ports. They have two parameters which deal with the output level:

VIL: The maximum low level voltage. (0.8V on the BCM2835)


VIH: The minimum high level voltage. (1.3V on the BCM2835)

This design will detect a slot car passing the lap counter by a change of a GPIO port from a low to a
high state. GPIO ports 22, 23, 24 and 25 will be used.

There are many designs for cases that are available, one example is
https://www.thingiverse.com/thing:559858.

Design Pin/Wire Mappings


The following table describes the mapping between the circuity on the lap counter and the pins on the
Raspberry Pi.

Table 1: Connection Mapping Table


Circuit Raspberry Pi Raspberry Lap Counter Wire color Usage details
connection ‘physical’ pin Pi pin ribbon pin (example)
name number name
Vcc 5 volt 2 5v Power 1 (white) Overhead illumination LEDs, tied
directly to theRaspberry Pi power
source.
Ground 6 Ground 2 (black) Common ground, use for 5v
circuit
Vcc 3.3 3 3.3v power 3 (orange) Phototransistor circuit
Ground 9 Ground 4 (grey) Common ground, use for 3.3v
circuit
Lane 1 state 15 GPIO 22 5 (yellow) Lane 1 state output, edge trigger
Lane 2 state 16 GPIO 23 6 (green) Lane 2 state output, edge trigger
Lane 3 state 18 GPIO 24 7 (blue) Lane 3 state output, edge trigger
Lane 4 state 22 GPIO 25 8 (purple) Lane 4 state output, edge trigger
Red LED 29 GPIO 5 9 (red2) Optional: Light Bar overhead
track state LED
Green LED 31 GPIO 6 10 (brown2) Optional: Light Bar overhead
track state LED
Yellow LED 37 GPIO 26 11 (orange2) Optional: Light Bar overhead
track state LED
TBD 12 spare

Note that while the ground pins all connect to a common ground, two ground pins are used for
convenience. Given that ribbon cables or jumpers may have different colors, the ‘wire color’ column
can be filled in as appropriate for your implementation. This currently shows the colors that were used
for the jumpers for the implementation shown in this document… you can choose other colors.

If after assembly and testing you find that you have wired the lanes incorrectly (e.g. Lane 1 is reporting
as Lane 2) all you need to do is reconfigure the jumpers on the Raspberry Pi for GPIO ports 22, 23, 24
or 25 as appropriate. It is simple to modify the python-based software to use different GPIO ports.

A useful command line tool on the Raspberry Pi is ‘pinout’. You can run it from a terminal window to
see the details of the bus configuration on your specific hardware version.

You can download a blank configuration worksheet you can fill in for your build at
https://www.dropbox.com/s/uhelua3eqnymhei/Bus%20Configuration%20Worksheet.pdf?dl=0.

HO Slot Car Track


Two HO slot car straight track sections are used. Each of
these sections needs to have two 3mm holes drilled for the
phototransistors. The 3D printed base section should be
used as a guide for drilling the holes (note that the hole
placement is slightly different for the two lane
configuration than for the four lane configuration). For best
results, drill through the upside down base through the back
side of the track.

Figure 19: Four Lane Track


Configuration
Figure 20: Note differences in Tyco Track

This design will support a wide variety brands of HO slot car track (Aurora, AutoWorld, TOMY). Even
though different brands of HO slot car track may use different connectors, they share common width
and height dimensions. However, if Tyco track is used, the spacing between the lanes is different (1 ½
inches, where lanes are farther apart) than what is used by other brands (1 3/8 inches between slots).
For this reason, a slightly modified 3D design (the OpenSCAD design) for the base section is available
to adjust the lane spacing in the base as needed for Tyco track.

Assembly
After printing the 3D parts and drilling the holes in the track sections, the next step is to insert the
phototransistors and R2 resistors into the base for each lane of the track. The careful to orient the
phototransistors with their collector on the same side.

Figure 21: Four Lane Base Wiring

You can now check to see that the phototransistors are seated properly by putting the track sections on
the base. The top of each phototransistor should be level with the track surface. You need to also be
sure that the polarity of each phototransistor is correct.
Figure 22: Four Lane
Configuration with Holes and Figure 23: Two Lane Configuration with Holes,
Phototransistors Phototransistors and LEDs

The side supports can now be inserted into the base, and the beam can be inserted into the side
supports. The next step is to insert LEDs into the beam. Again, be careful to orient the polarity of the
LEDs consistently. The LED circuit in the beam can then be wired together with the R1 resistor. It can
be tested temporarily connecting to the 5v and ground on the Raspberry Pi bus. All four LEDs should
be lit and you should be able to see the light over each phototransistor position on the track below.

Figure 24: Placing LEDs in Beam Figure 25: Wiring of LEDs


The next step is wire and verify the phototransistor circuitry in the base. Remember that each resistor
will be connected to the 3.3 volt output from the Raspberry Pi bus. The connections between each R2
resistor and phototransistor will eventually be wired to one of the GPIO pins on the Raspberry Pi bus. It
is important to verify the polarity of each phototransistor.
The following diagram shows the wiring of components to a jumper cable. Notice how the jumper
cables are threaded through the loops in order to avoid wiring from being pulled apart. All connections
should be soldered and taped.
Figure 26: Base Wiring Figure 27: Wiring of Optional Light Bar

Electrical tape should be used to cover connections on the base to avoid any inadvertent shorts. The
following pictures show the wiring from the underside, where duct tape is used to further insulate and
secure the wiring.

Figure 28: Taping of Connections Figure 29: Taping over Bottom of


Base

Raspberry Pi Connection
The 40 pin bus on the Raspberry Pi can be connected to the lap counter circuitry in a number of ways.
These ways include:

1. Directly wiring between the two. This could have some limitations, including the inability to
replace the Raspberry Pi without physically cutting wires,.
2. Use Dupont-style jumper cables to connect to pins on the Raspberry Pi bus. The problem here is
that jumpers could easily come loose unless an appropriate case is used. See
https://www.thingiverse.com/thing:2286741 as a case option.
3. Make a custom ribbon cable to connect specific wires from the lap counter to specific pins on a
40 pin connector for the Raspberry Pi.
4. Use a Raspberry Pi breakout board, which connects to the Raspberry Pi via a ribbon cable, and
then jumpers or wires are connected to the appropriate pins on the breakout board. A design is
provided on Thingiverse for an enclosure case for the breakout board. See
https://www.thingiverse.com/thing:5805549.

Ideally, you want to be able to disconnect the Raspberry Pi from the lap counter. You also may want to
be able to have the cable a sufficient length so that the Raspberry Pi and cabling can be easily hidden
from sight.

The following pictures show the connection of jumpers from the lap counter directly to the Raspberry
Pi GPIO bus. The cover for this box has enough room to enclose the jumpers securely.

Figure 30: Connections to Raspberry Pi via Figure 31: Cover Closed on Raspberry Pi
Jumpers

It is best to not wire directly from the lap counter to the Raspberry Pi bus, instead to have jumpers from
each that can be connected together. The jumpers from the lap counter can be securely connected to the
jumpers connected inside the Raspberry Pi case using the jumper clip that can be found at
https://www.thingiverse.com/thing:5825662.
.
Software
The user interface and event detection software is a Python program that runs on the Raspberry Pi. The
Raspberry Pi OS is a version of Linux, where the standard release package includes support for the
Python programming language. The software will track the following for each lane:
• Lap count
• Reaction time at start
• Time for last lap
• Time of best lap
• Average lap time
• Elapsed time
For each race, you can specify the desired number of laps. If the light bar is configured, it will display a
flashing light sequence to give a green at the start and a red light after the race has ended. It will also
determine the first to cross the finish line.
There will be two versions of the software that will be periodically updated on he DropBox. They are
both written in Python. These are:
• Laps.py, which uses a text-based interface. The current version is saved at
https://www.dropbox.com/s/q4f2r3es27q4k1w/Laps.py?dl=0
• LapsGUI.py, which provides a graphical interface that fully uses the touch screen. The current
version is saved at: https://www.dropbox.com/s/vffu9om5s28i4ox/LapsGUI.py?dl=0
Python programs can be initiated from the command line using the ‘python’ command, or through a
development tool, such as the Thonny IDE, which is on the standard Raspberry Pi OS release. This has
been tested with Python v3.9 and newer. See https://thonny.org.
IMPORTANT: Check for software updates periodically on Drop Box!

Text-based User Interface


The text-based Laps.py program will generate output to the command line, as in the following
example:
Race again? (Y/N or number of laps):4

Racing for 4 laps or 120 seconds


GO on GREEN light...

RACE STARTED at Friday, February 03, 2023 08:55:37 AM

Lane 1 : RT= 1.156


Lane 2 : RT= 2.913
Lane 1 : laps= 1 LT= 2.018 ET= 3.173
Lane 2 : laps= 1 LT= 2.613 ET= 5.527
Lane 1 : laps= 2 LT= 2.684 ET= 5.857
Lane 2 : laps= 2 LT= 2.063 ET= 7.590
Figure 32: Text-based Interface
Lane 1 : laps= 3 LT= 2.032 ET= 7.890
Lane 2 : laps= 3 LT= 1.944 ET= 9.534
Lane 1 : laps= 4 LT= 1.678 ET= 9.568
Lane 2 : laps= 4 LT= 1.868 ET= 11.402

RESULTS at Friday, February 03, 2023 08:55:46 AM


-- Winner is lane 1 --
-- Finished 4 laps in 9.568 seconds --
LANE 1 completed 4 laps, average= 2.392 sec, best= 1.156 sec ET= 9.568 sec
LANE 2 completed 4 laps, average= 2.850 sec, best= 1.868 sec ET= 11.402 sec
-- 2 cars completed at least one lap --

Race again? (Y/N or number of laps):

The program can be stored in a directory of your choosing, such as /home/pi. You can use the
Preferences→Main Menu Editor to create an icon to run the program. The command you would
configure is:
python /home/pi/Laps.py

You then check the box that you want to launch it in Terminal. When you run it, you can used the
Edit→Preferences option to change the text font, font size, foreground color, background color,
number of columns and number of lines. Good options (depending on screen size) are Monospace
Bold, 14, yellow, black, 120 and 32 respectively.

Graphical User Interface


This program is found at https://www.dropbox.com/s/vffu9om5s28i4ox/LapsGUI.py?dl=0.
The program LapsGUI.py can be installed and configured similar to Laps.py. The program can be
stored in a directory of your choosing, such as /home/pi. You can use the Preferences→Main Menu
Editor to create an icon to run the program. The command you would configure for use on a screen
with a width of 1024 pixels is:
python /home/pi/LapsGUI.py

If you have a screen of a width greater than 1024 pixels (e.g. 1280), for might use the following
command with an option to use a larger font size:
python /home/pi/LapsGUI.py -fs 32

As shown below, it is configured with a desktop icon with the name ‘LapCounter’. It can then be
executed via the touchscreen without a need for a mouse or keyboard. A mouse and keyboard can be
used if desired.
Figure 33: Lap Counter GUI Display

When the program is initiated from the desktop icon, you will likely see this dialog, simply select the
‘Execute’ option.

Figure 34: Dialog When Starting Manually


However, you can avoid this by having the program
start automatically when you start up your Raspberry Pi by using the nano text editor to add the
following entries to the file /etc/xdg/autostart/display.desktop:
[Desktop Entry}
Name=LapCounter
Exec=/usr/bin/python3 /home/pi/LapsGUI.py

Once the Lap Counter program is started, by default, it will set the duration of a race to 5 laps. You can
easily change this by simply picking another number of laps from the option menu. Note that even
though your configuration may only use 2 lanes, the user interface will show statistics for up to 4 lanes.
There are several options that can be added to the command line:
• ‘-lanes 2’ to show two lanes in the user interface, instead of the default of 4.
• ‘-laps <n>’ to set the default number of race laps, e.g. ‘-laps 12’. The number of laps can
always be changed using the option menu, which will provide a default set of options.
• ‘-fs’ to change default font size, e.g. ‘-fs 28’ to better use available screen size. If too large,
some columns may not show on the screen as this will increase or decrease the application
window size accordingly.. Should be at least 10. A screen width of 1024 pixels will
accommodate font sizes of up to 26, which is the default. A smaller screen could simply use a
smaller font size.
• ‘-x’ to place application window within display (pixels from left of screen), e.g. ‘-x 10’, default
will be to center.
• ‘-y’ to place application window within display (pixels from top of screen), e.g. ‘-y 150’, where
the default will be 150 from the top of the screen.
• ‘-guiPoll’ to change the GUI refresh rate in milliseonds, e.g. ‘-guiPoll 500’. Too small of a
value will cause performance, responsiveness issues.
• ‘-bt’ to change the sensor bounce time. This is the number of millseconds during which a
sensor will sense a change in value. This should typically not be changed.
• ‘-debug’ to activate debug output.
• ‘-help’ to show full list of command line options.

The following screen shows the application ready to start a race, where the desired number of laps has
been set to 10 (as an example) from the option menu.

Figure 35: Initial Display, Ready to Race

To start a race, you then hit the ‘Start’ button. If configured, the LED status lights will have a sequence,
where the yellow light will blink, then the green light will indicate the start of the race. Cars crossing
the starting line before green will simply have that lap be ignored.
Figure 36: After Starting Race

During the race, lap counts and statistics will be captured and displayed. The race will end when the
first car achieves the required number of laps, the race is stopped, or the race time limit expires.
By default, it is assumed that a race should finish within a minute for each lap. However, you can
indicate a delay to negate the timer by going into caution mode. In caution mode, time, laps and other
statistics will accumulate, but the race will no longer be time limited. A yellow light will also be
displayed. You can only go into caution mode if a race is running.

Figure 37: Caution Mode

When in caution, you can select to ‘Restart’. This simply turns off the yellow caution light, and turns o
the green light. Restart only works when in caution mode.
Figure 38: Restart from Caution Mode

When a car achieves the required number of laps, LEDs lights will flash to indicate that the race is
finished. The message line will indicate the winner of the race.

Figure 39: Race Finished

At any point you can use the ‘Stop’ button to terminate a race. This will stop accumulation of laps and
calculation of other statistics. Once stopped, a race cannot be restarted.
Figure 40: Race Stopped

The following is a view after a ‘Reset’, which simply erases statistics from the last race. This is done
automatically when a new race is started.

Figure 41: After Reset

Please feel free to take the Python source code and adapt/extend it as you see fit. Obviously it is
currently dependent upon the Raspberry Pi GPIO library, but otherwise very generic with minimal
dependencies.
Test Program
The following simple Python program can be run on a Raspberry Pi to test the circuitry and
connections to the Raspberry Pi GPIO bus. It will track the number of rising edge events on each of the
four GPIO ports (i.e. ports 22-25).

"""
GPIO Test: Tests GPIO ports 22-25 for state changes
Tests LED output on ports 5, 6, 26

For use with LapCounter

Scott Neumann
February 3, 2023
"""
import RPi.GPIO as GPIO
from time import sleep

bt = 50
channels = [22, 23, 24, 25]
leds = [5, 26, 6]
counts = [-1, -1, -1, -1]
RED = 1
YELLOW = 2
GREEN = 4
blink = 0.25

def ioEvent(channel): # Catch sensor events


i = channel - 22;
if GPIO.input(channel):
counts[i] +=1
print("Rising edge on channel", channel, "counts=", counts[i])
else:
print("Falling edge on ", channel)

def light(l): # Manage light bar


if l & RED: # RED
GPIO.output(leds[0],GPIO.HIGH)
else:
GPIO.output(leds[0],GPIO.LOW)
if l & YELLOW: # YELLOW
GPIO.output(leds[1],GPIO.HIGH)
else:
GPIO.output(leds[1],GPIO.LOW)
if l & GREEN: # GREEN
GPIO.output(leds[2],GPIO.HIGH)
else:
GPIO.output(leds[2],GPIO.LOW)

def lightSequence(seq): # Sequence LEDs


for s in seq:
light(s)
sleep(blink)
light(0)
sleep(blink)

# INITIALIZE
GPIO.setmode(GPIO.BCM) # Defines how GPIO pins are mapped
for n in channels:
GPIO.setup(n, GPIO.IN) # Lane sensor

for n in leds:
GPIO.setup(n, GPIO.OUT) # LED

for c in channels:
GPIO.add_event_detect(c, GPIO.RISING, callback=ioEvent, bouncetime=bt)
print( "Channel", c, "state is", GPIO.input(c))

# TEST
print("Get ready...\n")
lightSequence([RED,YELLOW,GREEN,RED+YELLOW+GREEN])
lightSequence([YELLOW,YELLOW,YELLOW,YELLOW])
print("GO!")
light(GREEN)

try:
sleep(10)
finally:
light(RED)
sleep(3)
for c in channels:
print( "Channel", c, "state is", GPIO.input(c), "counts=",counts[c-22])
GPIO.cleanup()

print("Done.")

The current version can be found at https://www.dropbox.com/s/fw3ujtwswjwfpex/ioTest.py?dl=0

References
Here are some useful references…
• Lap Counter documentation and software on DropBox:
◦ Current version of this document: https://www.dropbox.com/s/ghpnk3cirz2iqgq/Lap
%20Counter%20Design.pdf?dl=0
◦ Text based software: https://www.dropbox.com/s/q4f2r3es27q4k1w/Laps.py?dl=0
◦ GUI-based software: https://www.dropbox.com/s/vffu9om5s28i4ox/LapsGUI.py?dl=0
◦ Test program, useful for testing circuitry:
https://www.dropbox.com/s/fw3ujtwswjwfpex/ioTest.py?dl=0
◦ Raspberry Pi bus configuration worksheet:
https://www.dropbox.com/s/uhelua3eqnymhei/Bus%20Configuration%20Worksheet.pdf?
dl=0
◦ OpenSCAD file for 3D printing:
https://www.dropbox.com/s/1zm202v6o4jgrso/LapCounter2-4.scad?dl=0
◦ Tyco version OpenSCAD file for 3D printing:
https://www.dropbox.com/s/u1m4n1u6plp7wx5/LapCounter2-4Tyco.scad?dl=0
• Raspberry Pi: http://raspberrypi.com
• Raspberry Pi pinouts: https://pinout.xyz
• 3D Printing Thingiverse site: http://thingiverse.org
◦ Lap Counter OpenSCAD and STL files: https://www.thingiverse.com/thing:5805734 (four
lane version) and https://www.thingiverse.com/thing:5806620 (two lane version)
◦ Banked track supports: https://www.thingiverse.com/thing:5749355
◦ Track supports: https://www.thingiverse.com/thing:5743731 , supports for Tyco track are at
https://www.thingiverse.com/thing:5842260
◦ Track skirts: https://www.thingiverse.com/thing:5732903
◦ Track SAFER barrier: https://www.thingiverse.com/thing:5874984
◦ Track fasteners: https://www.thingiverse.com/thing:5728079
◦ Jumper clip files: https://www.thingiverse.com/thing:5825662
• Slot Car Central: http://www.slotcarcentral.com
• Python programming language: http://python.org
• Thonny IDE for Python: https://thonny.org
• Recommended LCD Display: http://www.lcdwiki.com/7inch_HDMI_Display-C
• Automatically starting Raspberry Pi program on startup: https://www.makeuseof.com/how-to-
run-a-raspberry-pi-program-script-at-startup/
• nano text editor documentation: https://www.nano-editor.org
• OpenSCAD site: https://openscad.org
If all else fails, please feel free to contact me at scott.neumann@ieee.org.

You might also like