What is a light pen?

A project log for Light pen support for RetroPie

Can a Raspberry Pi with CRT display support 8-bit era light pen?

Maciej WitkowiakMaciej Witkowiak 06/07/2020 at 16:200 Comments

Let's start with the beginning. The C64 wiki says this

light pen is a pen-shaped input device which connects to a compatible computer (such as the C-64): The device contains a light sensor which, when pointed at a cathode ray tube screen, generates a signal each time the electron beam raster passes by the spot the pen is pointing at.

The image on CRT is built by the elecron beam that scans the whole screen area 50 (for PAL) or 60 (for NTSC) times per second. It's invisible to the human eye due to persistence of vision but the place, where the beam hits the screen is a bright spot that goes line by line, from left to right, from top to bottom to draw every frame.

You can learn more about it from a helpful YouTube video.

The idea of a light pen is very simple - start the timer during vertical sync (when the beam goes back to the top left corner) and wait until the light sensor pointed at the screen detects a bright spot.

On a C64/128 this is done in the hardware. The VIC chip has internal counters for lines (Y axis) and screen columns (X axis) and their contents are latched whenever there is an impulse on the joystick port (Control Port 1) FIRE line.

On a C64 the X resolution is supposed to be 2 pixels and 1 pixel for Y axis.

Practically this was not possible to achieve. I have only used by lightpen for GEOS and even with calibration routine (which had to be done during every boot) the X resolution was more like 8-16 pixels, but Y was much more stable: maybe 2-4 pixels.

It's perfectly good enough to be used for pointing at something, but you won't draw anything with it.

Note that the idea here is more complicated than the light gun (Zapper) used for NES games. The light gun had a simple light sensor and was supposed only to detect light or lack of it. There was no expectation of finding out where the lightgun was pointed at the screen.

The C64 wiki has an example circuit diagram of a light pen. It also provides reference to a more detailed article. There is one important detail that you should know about: the button should be wired to pin 3 of the control port instead of pin 1, so direction LEFT instead of UP. It's clearly visible in the plug itself that from the top row only the middle pin (number 3) is wired to anything:

Here is how my Turbo Computer Lightpen looks inside. It's enclosed in a metal tube. The part on the left is a plastic cover with a plastic lens inside. There is also a spring there inside that pushes whole thing outwards.

It's clearly much more complicated than just a phototransistor and some pull-up resistors.

There is a LM319N comparator, a bunch of capacitors and resitors and much more transistors than I expected. The red/white elements on the left create the light barrier that detects button press. When the whole pen is pressed against the screen the plastic cover retracts inside and a tab breaks the light path. This is counted as a button press.