C=64 Light Pen

Back in the 80’s, light pens were not a very common thing. At least not for home computer owners, even though the concept was already 30 years old. There were a few commercial products, but very little software supported them, so it was rare to find a home user who had one. What did they do? You point at the screen and the computer magically knows where you’re pointing, sort of a predecessor to a modern stylus.

How they worked was completely different though. Most (but not all) modern touch screens are resistive or capacitive. Light pens instead relied on the rasterized nature of a CRT computer monitor or – as was frequently used by home computer owners – their TV.

CRT’s “paint” the image by moving an electron beam across each row, one row at a time. Due to the high update rate, the phosphoresce of the tube, and the persistence of vision nature of our eyes, we just perceive a complete image instead of a fast moving dot. The electron beam at any instant is only painting a single pixel at a time. This is exactly what old-school light pens took advantage of.

The one I built was for my Commodore 64, so let’s dive a bit more into how they worked in that system, though the concept is fairly universal. The video chip in the Commodore 64 controls the rasterized painting of the screen, and therefore knows exactly where the beam is at any instant. If an external device were able to detect the beam as it flew by across the screen, it could ping the video chip and ask it where (i.e. an X,Y coordinate) the raster is right now. That essentially identifies where on the screen the sensor was pointing. Commodore engineers provisioned the C=64 to support a light pen by designing in an external light pen trigger input on the DB9 joystick port and logic in the video chip to latch the raster X,Y location when triggered.

How do you detect where the raster is? I can’t see it. You can’t see it. However, a fast phototransistor can. As the beam flies by and paints pixels, they are slightly brighter right as they are painted. A phototransistor placed very close to the screen can detect this. With a little signal cleanup thanks to a logic chip, this is then sent to the light pen pin on the joystick port. Once the video chip latches the X,Y based on the trigger from the phototransistor circuit, your program can then check the relevant registers and obtain the coordinates. For the Commodore 64, check out memory addresses $D013-$D014. Other computers operated the same way – the key is that the video chip reacts to an external input asking “Hey, where are you on the screen right now?”

The main caveat with this approach is that the area you’re pointing to needs to be producing some light. Enough light for the phototransistor to detect it. If the area you are pointing at is black for example, the light pen circuit won’t trigger and the video chip won’t be able to tell you anything about where the light pen is. As a result, the programs that I wrote for the light pen, always had a generous amount of white pixels in the areas I wanted to detect. Since I mostly used it as a way to navigate simple menus, it was easy enough to put a white square next to each menu item. At some point, I need to dump the contents of my C=64 5 1/4″ floppy disks before they go unreadable. It would be fun to find those old programs and see what sort of code I wrote way back then.

The light pen I built was based on a project from a magazine article, probably in “Radio Electronics“. It’s a very simple design. Because of the features in the C=64’s video chip hardware, the the light pen hardware itself is pretty trivial. Just “peek” those registers in the video chip, and do a little math to convert to screen X and Y locations. I remember it was a fairly noisy measurement, I assume mostly due to the sensitive area of the phototransistor being significantly larger than the pixels. I don’t remember experimenting with any software filtering to see what sort of accuracy/response-time tradeoff I could get. Then again, I didn’t know what FIR or IIR filters were at the time.

Anyhow, it was fun to find this and take it apart. Love that my standard construction technique – masking tape – is well represented in this build as well. The case was a gutted felt tip pen, which was still a bit small and it looks I clipped off a fair amount of the unused pins to cram the IC in there. I do remember how big of a deal it was to get the 74LS157 IC I needed. Radio Shack didn’t carry it so I had to go mail order, probably Jameco or DigiKey. Like, real mail order. Back when mail order meant: fill out a list on paper, snail mail it, and wait 2 weeks!

Looking back, I’m surprised the design didn’t use a Schmitt trigger gate to help clean up the signal. Reverse engineering the schematic shows that a ‘157 wasn’t really required. It’s only used to square up the output. Had I known better at the time, I suspect I could have used a 74LS00 that I probably had lying around!

For the curious, more detail on how the Commodore 64‘s VIC-II video chip implements the light pen feature can be found here.