This page looks best with JavaScript enabled

Week10: Input Devices

 ·  ☕ 5 min read


For my final project racetrack I plan to use IR phototransistors and IR LEDs as proximity sensors to detect the race cars crossing the finish line. I will make a prototype circuit with a blinking LED and a phototransistor. I will use a rudimentary version of synchronous detection to monitor the difference in the output of the phototransistor when the LED is on and off.

Circuit Design

I designed a simple circuit in kiCad based on an Attiny412 microcontroller with a phototransistor and resistor, and LED and resistor, an FTDI header, and a power filter capacitor. I wanted this board to be a simple as possible because for my final project I knew this sensor board would have to fit into the 1.5 inch wide racetrack and have a connector to bring it back to the main controller board.

To design the phototransistor circuit I found this diagram helpful.

I connected VCC through a 10k resistor to an analog input pin and that same pin through the phototransistor to ground. I guess I should be able to use an internal pull up resistor instead of this external one, but this works fine.

I had two different phototransistors available PT15-21B-TR8 and PT15-21C-TR8. Reading through the data sheets there are only two major differences between them. The 21B has a black lens and a smaller sensitivity range 730-1100nm. The 21C has a clear lens and a larger sensitivity range 530-1030nm. The 21B is aimed more towards the IR spectrum 700-1000nm where as the 21C is aimed more towards visible light 400-700nm. I planned to use IR LEDs at 850nm so I went with the 21B.


I discovered an interesting quirk of kiCad that my board was so small ~25x30mm that the labels for some components extended beyond the margin I had drawn around the board. This mean that when I exported the SVG files to be cut the margins were larger than I expected.

I went back into kiCad and edited the text labels so they didn’t extend past the margin and exported new board files with more reasonable margins.


After populating the board I then realized that my minimalism went a bit too far and I forgot to connect anything to the UPDI programming pin on the Attiny412. Fortunately the FTDI header has two more pins than I need for serial communication so I added a patch wire from the UPDI pin to an extra pin for programming. Check out my electronics design week documentation to see the wiring hookup for the UPDI programmer. It’s basically the same here just using my improvised UPDI connector instead of a dedicated connector.

Systems Tests

I started with a basic serial program to test my assembly and power circuit. I got responses over serial so I knew that was ready to go. Next I tested the IR LED with a simple digital write blink program. Because the IR LED doesn’t produce visible light I pointed my phone camera at the LED and I could see it blinking on the camera screen even though I couldn’t see it with my eyes.

Next I dumped the analog reading of the sensor pin to the serial output. Initially I wasn’t getting any variation on the reading from the phototransistor but after a closer look at the circuit diagram above and I realized that I had my phototransistor installed backwards.

Sensor Readings

After flipping the sensor around the right way I started getting good sensor readings.

Those readings every other one the IR LED is turned on. When it reads ~1024 or 5V that is the signal through the pull up resistor. When the value is low that’s the reflection of the IR LED bouncing off my finger held over the board.

Next I started experimenting with super sampling by adding up 100 readings of the sensor. Watching these values on the serial plotter made the detection of something passing over the sensor much more obvious.

I was confused for a bit as to why the scale on the chart went below 0 but I quickly realized it was because I was overflowing the int that stored the value. I switched this to a long and corrected that error. I also hadn’t formatted my serial messages correctly for the serial plotter to recognize the values with the LED on and the LED off. They need to be comma separated on the same line.

LED on: 1234, LED off: 4321

With that formatting change you can see the two signals on the serial potter. LED on in red and LED off in orange. You can clearly see spikes in the LED on signal when I wave something over the board.


Lastly I wanted to look at these signals using an oscilloscope. I connected the probe to ground and the LED. This shows me the square wave of the LED turning on and off.

This shows the 1ms delay I put between changing the LED state and making the reading.

The more interesting reading is looking at the analog output of the phototransistor. I moved the input from the LED to the sensor and you can see the signal spike when my hand gets close to the board.

Design Files

Board Files and Code

Nick Anastasia
Nick Anastasia