All posts by laker

.NET Light Meter via serial port

This is a quick implementation of an idea I had. Someone online asked for the cheapest way to read light levels into a Windows application.

The lowest cost way I could think of was to avoid a microcontroller or other logic component and use whatever was already on a modern PC. My idea was to use a Cadmium Sulphide (CdS) photocell. CdS cells are used in many camera light meters and work by changing resistance in response to ambient light. By using the current through a photocell to charge a capacitor, we can tell the light level by measuring the time taken to charge. In days of old we could have used a joystick port to do this, but those are long gone, now replaced by USB-connected joysticks.

On PCs without joystick ports, we could have done the same thing with the printer port. However, those are also a thing of the past.

Many PCs still come with serial ports, so we’ll try those. If not, a USB to serial converter will work. The Clear To Send (CTS) and Data Terminal Ready (DTR) lines can be controlled from a Win32 or .NET application. There is some concern about the responsiveness of Windows, but as long as we avoid very short time constants, we can get usable data.

The concept is to build an RC circuit from a photocell and a 200uF capacitor. The photocell is driven from the DTR line and the junction of capacitor and photocell is read by the CTS input. Here’s the schematic: it’s pretty simple.

The results are surprisingly useful: discharging the circuit for two seconds and then charging it while polling the CTS line every 5 milliseconds shows a clear difference as the photocell is pointed at various areas of a lighted room.

Code to use this circuit is on GitHub.