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

  7. hello, If the position is reversed, the transmitter is placed in the kart and the reciever is placed on the track, does it still work properly?
    sorry i am newbie..

    1. Yes it works fine. It doesn't matter which is the fixed side and which is the moving one as long they line up when passing.

  8. Thanks for the reply, your project really helped me a lot in learning about microcontroller.
    I have another question, Can I use this project For More than 1 user with 1 receiver in 1 display?
    And how can the timing record be displayed on the led panel P10?


  9. Hello friend, I'm from Argentina and this is my first project in Arduino. I have 2 questions. 1. SD memory is required or it can be stored in a same microprocessor. ?
    2 in the diagram I see, or I think, a crystal or similar. it is right?? thanks x the project. !!!

    1. Hello, There isn't any crystal in the diagrams but the Arduino does have a standard 16MHz crystal. Which component are thinking is a crystal? You can record lap times to the internal memory of the arduino but the flash memory is small. It is also more difficult to read the times back from the flash memory - maybe through a "serial print" could be programmed in? The good thing about the SD card is it has big memory size, the SD card can be removed and plugged easily into any PC with SD card slot and the lap times are saved as a .CSV file format which can be directly opened by most spreadsheet programs.

    2. Excellent. in the image of the board with the components i see one that is silver. what is that?? how many laps it stores without sd memory. Because I do not need to download them. that they can be seen on the display is enough.

    3. Ahh yes that's the 16MHz crystal that is required for the 328 chip to work when not on the arduino board. If you used a nano or pro mini arduino board then you wouldn't need it as it's already on those boards. As for storage, I've not looked into it but I guess it wouldn't be much as there's only 1k of eeprom (non volitile) memory on the 328 chip unless you can use some of the flash memory (where the program is stored) but this is also small - 32kb total minus the bootloader and program size

    4. This comment has been removed by the author.

    5. Take a look at this if you haven't already
      This one is using the internal memory and getting about 250 laps stored but only a time - no lap count

    6. thanks for all. at next days, i will procces to create your proyect. thanks for all. !!

  10. Really nice project! Thanks a lot.
    I'm doing a lot of go-karting and i'm would like to built one as well. I do have some ideas. Matt is it possible to send you a pm?

  11. Not sure if PM's can be done through blogger. I'll look into it and let you know unless you have any suggestions?

  12. How effective will this lap timer be on an oval track for stock car racing as I am interested in building one for my team? How close must the car get to the wall where the beacon will be fixed? Will speed of car influence the signal as it passes by?

    1. Hello. The signal is reveived very fast so theoretically speeds of 150mph or more would be fine. The exact maximum would need to be calculated based on the code length (the IR code),the LED beam angle and the distance from the transmitter. I'll do some calculations later on my system and let you know. It would work fine for oval racing. The transmitter difference on this example is about 15 meters, so as long as you pass within 15 meters of the pit wall it will be picked up

    2. Ok, So I managed some further calculations. The maximum time to receive the correct signal is 9mS. The IR LED's I've used have a 20 degree beam angle so at 5 meters from the pit wall the beam diameter is 1.76 meters therefore the maximum speed is 195 m/s or 436mph - should be enough. After 5 meters distance from the pit wall the beam diameter doesn't really get any bigger as the as the beam starts to loose intensity at the edges so it stay about the same until 12 - 13 meters where it will start to get smaller until 15 meters where it becomes to weak to trigger the system. Different IR LED's will have different characteristics and can be driven with different currents so distance could be much greater etc.