Wednesday, 17 February 2016

IR Laptimer using Arduino

So here’s my problem, I love karting and take part in lots of different races from corporate grand prix to the British 24hr and anywhere in between. I wanted to record and display my lap times but didn’t want the expense of buying an off the shelf unit at over £500 (cheaper ones are available but I needed one which would record 24 hours’ worth of racing and more than 1000 laps). I also like to tinker with electronics so I decided I would see if I could make something instead (and hopefully it would be cheaper too).
Firstly I needed to work out which type of system to make, Infra-Red or magnetic pick up. Most karting tracks have a magnetic loop fitted in the track so in theory that meant using a hall effect sensor to pick it up. The problem is some track don’t have a loop and some have 2 or 3 loops (for intermediate timing) so this wouldn’t fulfil my requirements. I decided on the Infra-Red option as it was already used in professional systems and I could take it to any track, the down side is that I now have to build an Infra-Red transmitter as well.
Having then researched the IR option further I came across a very useful blog where Jim Ele had already built a similar timer which utilised an existing IR transmitter but used an Arduino as the processor. This would form the base of my build.

The Transmitter
The first part was to build the IR transmitter which would sit on the pit wall repeatedly sending out a set code. I simply built a home brew circuit board with the bare minimum to run the Arduino’s Atmeg328p processor as per the diagram below and attached 4 AA batteries for the power. I used 2 IR LED’s purchase from Maplin’s to send the signal out.
The power circuits have been simplified for the diagram
The next problem to overcome was how to avoid false triggers from other IR sources, I.e. other IR transmiters that could be at the circuit and also things like the Sun / strip lights etc. The answer was to use a 38Khz carrier frequency with a matched receiver and send out a specific code that the receiver would recognise (The same way your tv knows that you’ve pressed a certain button on the remote). Because the receiver isn’t in the beam for very long I needed a short code which could be received very quickly. After again looking at what Jim Ele had found out I decided to give 3 ‘On’ pulses each 416µs with a 1200µs ‘Off’ gap between each. After the 3 pulse there is a 6ms gap an then the code repeats itself. I’ve tried to show this visually below.

I housed all this inside a suitably side housing purchased from Maplin and attached an old mini tripod as a stand. Waterproofing is done by covering the whole box is a clear plastic bag with the 2 LED tubes sticking out. The total cost was less than £20 and has a range of about 20 meters. The AA batteries easily last for more than the 24hours I needed (more than 50 hours actually).
Finished Transmitter, The two small plastic tubes sit over the LEDs and are mainly for Aesthetics.

The Receiver
As the beam was using a 38Khz carrier frequency I needed an IR receiver that was set to that frequency so I opted for the TSOP38238 as it was easily available for a low price and had good performance and will run at 3.3v. The TSOP38238 receives the signal from the transmitter and inverts the signal as shown below.
 I housed this about half way inside a separate small box with a small window so that it could be attached to the front panel of the kart for best view of the transmitter. This also meant it wasn’t in direct sunlight. It was sealed closed to make it waterproof. A 3 core lead then connected it directly to the main unit on the steering wheel.

The Main Unit
Again I started with a basic circuit to support the Atmega328p processor. I then added a 16x2 LCD display module with LED backlighting (be careful here as the backlighting is the main current draw and can easily exceed 100mA with some models. I choose a module that had a single LED for backlighting to reduce the current consumption for optimum battery life). To record the lap times I added a microSD module. The microSD card also contains a text file which can be altered to tell the unit how long to ignore the receiver for (I’ll come back to this later)
The power circuits have been simplified in this diagram
Note: The system runs at 3.3v not 5 as per the diagram. Because of the change to 3.3v I also changed the IR receiver to a TSOP38238.

This was then housed into suitably sized box from Maplin with foam padding around the circuit board to protect it from vibration.

I then used a second identical box to house the batteries which was then mounted onto the back of the first box around the steering wheel. This meant it could be swapped from one kart to another very quickly and even attached to hire karts.
Originally I wanted to use 9v pp3 batteries for the power but an initial test showed these couldn’t stand the vibration and they destroyed themselves within an hour. I swapped these out for an RC car Ni-mh receiver pack battery – 6v 2500mAh and again wrapped it in foam. This works great and lasts over 30 hours per charge.

How the system works
As the receiver picks up the beam from the transmitter, the sketch checks the length of each pulse and counts them if they are the correct length. Once its counted to 3 it knows a ‘good’ code was received. If a pulse doesn’t fit the right time frame or no further pulses are received then the count is reset. After receiving a good code the sketch then works out the lap time (the time between the last ‘good’ code and this one) to the nearest 1000th of a second, increments the lap counter, works out the delta time to the fastest lap and adds it to the total time expired since the start of the race in hours and minutes. This is then displayed on the screen and also stored onto the micrSD card as a .csv file type and can be opened in excel. The system then ignores any signals from the IR receiver for a period of time, this period of time was set by the text file on the SD card as mentioned earlier. This time should be set to the best possible lap time minus 3 to 4 seconds. This way the IR cannot receive any false signals from other transmitters. About 4 seconds before you pass through the beam again the system starts looking for a beam again.

Receiver Sketch

A Note on using microSD cards: When reading and writing SD card can use quite a high current (200mA or more) so make sure your voltage regulator can handle it! Also they run at 3.3v not 5v so I run the whole system at 3.3v which means you need an LCD module capable of running at that voltage. I also used the SDFat library for this project not the standard Arduino one as the SDFat library (available from GitHub) puts the microSD card to sleep between read/write events which saves a lot of current and prolongs the battery life. The standard SD library keeps the card awake by default and it makes quite a difference.
Finally to make the unit rainproof I printed the front panel onto OHP transparency in reverse. The text was left blank and spray painted in silver afterwards. Turning the transparency over then reveals a nice shiny front cover with silver text and a clear window for the screen. This was double sided taped to the front and worked great.
The total cost for the unit was around £40

Further improvements coming.
I also tested a OLED 16x2 screen and this worked better than the standard LCD as no backlighting is required and its clearer in bright and dark conditions. I used blue text on a black background.

I’ve also started work on a unit with a 2.9inch graphical touch screen but its yet to make it onto a Kart. More on this one to come.


  1. Hello,
    totally interesting project!
    I'm also planning a lap timer for RC cars so this would be a little bit different from yours. Funny fact: The part with a short IR dummy code was also my thinking :)
    Here is my planned version:
    One (or more for partial results) stationary IR-LEDs (maybe with different codes) - like your system
    Each car will get a transponder: AVR + IR-receiver + NRF24 radio module (client)
    One "master" system: With Display and buttons also fitted with a NRF24.
    So if a car is passing the IR line a unique message via NRF24 is send to the master, the master records the lap time and the race position and counts the lap with audio feedback (buzzer). So the whole system is able to use with multiclients! The hardware is totally cheap: AVR-minis costs about 1.4 Euro as the NRF24 modules. Maybe I'm going to use a STM32 for the "master" (32bit, 72MHZ - so more power for recording accurate (multiclient) results) With it is also possible to program the STM32 with "arduino language" - the STM32 "maple mini" clones costs about 2 Euros on aliexpress

    1. Sounds Good. I would be quite interested in understanding how you get on with sending the times over RF. I've thought about this with the karting timer so the PIT team could also see my lap times. Keep us posted with your build

  2. It is totally easy: (I'm at work so I wont have access to my source code, so here the theory)
    The "master" is listening to several slaves. So if the slave (within the car) is passing the IR diode it sends a code to the master, like "CAR01".
    All NRF24 libraries I know "understand" a network of 254 nodes, should be enough :)
    Example for a multiclient (nodes) code is like this:
    The intersting stuff will be shown in practise: The NRF24 is a 2.4GHZ module as most of the modern remote controls.
    So I hope the are no indifferences between them.

  3. Ok, I cannot edit my post, just delete, so here a little addon:
    Another, maybe cheaper and better solution would be using an ESP8266 instead of "arduino" and NRF24. Since ESP's can be programmed with "arduino language" also and we only need one pin (getting the IR signal) this would be a super compact size solution. Another benefit would be, with an access point "master" ESP8266 act as web server you would be able to show all racing data on a homepage in real time in a local intranet (or internet!). But I'm not sure if the range of (even the powerful) ESP's would be enough.

  4. Hello!
    I just finished my first version of my lap timer, please visit:


    1. Looks great. As I said before the RF side is very interesting. I think I can definitely make use of this in the future. Love the fact that you 3D printed some of the parts too!!
      One issue I can see (Maybe you already have the solution), is if 2 cars pass through the beam at the same time. Can car 1 block the IR beam to car 2? and therefore car 2 misses a lap time / count? Could mount the beam high up angled across the track maybe so all cars can see the beam, or even above the track?

  5. Hello!
    I didn't have tested the system very much, so I cannot say if this problem occurs with RC cars. RC cars are much smaller than karts and the possibility missing an IR signal is much less. For sure: An angled IR beamer (maybe in arc shape with up to 10 IR LED's?) would help a lot. A complete IR-LED bar over the whole track would be the best solution, but this wouldn't be very flexible anymore. Another approach would be using multiple IR receiver (about 3) on different positions of the car. But getting the code from 3 receivers would be a big challenge in code (I would use 3 timers on a STMF1 for that). Or a "code repeater" on each transponder. Getting the signal left and send it on the right side (bad precision...)

  6. Hello. First of all, congratulations for your project. I'm also intrested in building one of this for my kart. I see in the transmiter part you made your "arduino board" yourself. I´m quite new with electronics and im not sure if id be able to do the same so I was thinking that maybe getting an arduino nano board
    to connect the ir leds following your diagram would have the same effect. Am I right?
    Thank you very much.

    1. Hi, Yes the Arduino nano will work fine. If I was to build it again I would use an ATTiny85 chip instead to run this (it can be programmed from the arduino software and the sketch is the same), I would remove the 220r resistor and power LED (as they don't really serve any purpose) and I would change the BD237 transistor to an IRL520 Mosfet instead.
      Make sure to set the resistor value for the IR LED's to be suitable for the LED's you use. Good Luck