Light Detection Using A Phototransistor and Voltage Comparator

This page describes an example project that turns on a red LED when light is dim and a green LED when light is bright. Or more to the point, changes color when objects (such as a fan blade) pass in front of it.

Because the lighting required to enable either LED is controlled by individual potentiometers, they can be set such that either, neither, or both LEDs turn on. That is, the red LED doesn’t have to turn on simply because the green LED turned off.

This is also a good example of how to use a phototransistor, rather than a cadmium sulfide photocell to detect light. Phototransistors react much more quickly, and are much more sensitive.

Hmmm... Couldn’t you create a simple robot that seeks light (or dark) by turning on motors rather than LEDs?

Why did I make this?

For a prototype hand held tachometer project, a microcontroller analyzed a phototransistor and a pair of potentiometers using three built-in analog-to-digital (ADC) converters. Unfortunately, that design required the microcontroller to spend most of its time reading the phototransistor’s voltage in order to detect a passing line or mark.

It turns out that a dedicated comparator chip is a superior solution. Comparators constantly compare pairs of voltages and provide a digital indication ('1' or '0') of which voltage is higher. Using the dedicated chip frees the microcontroller, which is now only interrupted when the digital signal changes.

If your project requires a microcontroller, but the microcontroller doesn’t have any available ADCs, perhaps adding a comparator chip would provide a faster, less expensive solution.

Solderless breadboard with sensor, comparator, and LEDs

Solderless breadboard with sensor, comparator, and LEDs


The LM239 is a quad, single-supply comparator.

Quad: Can compare four different pairs of voltages.

Only half the comparator inputs were used since this example compares the phototransistor (U1) to one potentiometer (R1) and also compares the phototransistor (U1) to the other potentiometer (R2). The four unused inputs (lower-right of the IC) are connected to ground. The two unused outputs (lower-left of the IC) remain disconnected.

Single-Supply: The same power source provides the ground for the inputs, the chip itself, and the outputs. If necessary, use a dual-supply chip to prevent ground noise from one circuit at the inputs from affecting the other circuit at the outputs. Electrical noise isn’t an issue for this light detector, but could be for a circuit involving motors, spark-plugs, RF, or amplifiers.

This comparator can operate up to 36 volts (or +18 V to -18 V). Since I intend to connect it to a microcontroller, I made this example +5 V and GND.

Comparator: Connect two wires as inputs. If input A has a lower voltage than input B, the output goes low (to ground). If input A has a higher voltage than input B, the output disconnects. Oh no! I wanted low and high, not low and disconnect. Using a pullup resistor (R5) allows the disconnected output to go high (to +5 V).

The LM239 is pin compatible with MC3303, LM339, and LM2901 chips. Although their operating temperature ranges differ (and a few other differences) they’ll all work fine in this project.


The 0.1-microfarad capacitor stores a small amount of power so that the comparator (IC1) has a stable supply. This is a very common use of a capacitor. It is called a “decoupling capacitor” in this usage.

A decoupling capacitor also absorbs or smooths short-lived higher voltage spikes. In this application, the capacitor doesn’t prevent the comparator from oscillating due to noise when the inputs are nearly identical.

R5 and R6

These 10-kilohm resistors are used in a very common way. They provide a +5-volt signal unless something to which they are connected provides GND.

A resistor in this configuration is called a “pullup resistor”. It pulls up the line to +5 V unless something stronger pulls it down.

Oddly enough, the comparator chip only provides an output of GND. So, the pullup resistors provide +5 V to the comparator outputs.

In this circuit, LEDs are hooked up to the outputs instead of a microcontroller or logic chips. So, R5 and R6 could be discarded from this circuit since only GND is needed to power the LEDs.

R3 and R4

These 470-ohm resistors limit the current going through each LED (LED1 and LED2). The LEDs can be made brighter with a lower value resistor (such as 220 ohms) or dimmer with a higher value resistor (such as 1 kilohm).

LED1 and LED2

Standard red and green LEDs.

They’re placed in “backwards”, so that the power goes from +5 V through each LED into the outputs of IC1. This is because the comparator chip can sink (ground) up to 16 milliamps of current, but can’t source (positive supply) any. No big deal, we just need to remember that the LED lights up opposite to what we’d normally expect on the output.


This phototransistor is sensitive to normal visible light (800 nm). Infrared phototransistors can be used, but won’t be as sensitive to flashlights and other household sources.

The detector is classic for my projects (see Sweet the line follower and Bugdozer the Sumo bot). The sensor can be purchased from Jameco Electronics, part number 120221, product number BPW77.


This combination of 22-kilohm resistor and phototransistor (U1) forms a voltage divider. If the light is bright, then the phototransistor uses very little voltage and R7 uses almost all of it. If the light is dim, then the phototransistor uses most of the voltage, so R7 gets almost none of it.

You'll notice the green wire coming from between the phototransistor and R7. This green wire is connected to two comparator inputs. The voltage of this wire is the same as the voltage that resistor R7 gets.

R1 and R2

These 10-kilohm potentiometers are variable resistors acting as voltage dividers. Five volts is connected to one end and GND to the other. The middle pin provides some voltage in between as the dial is adjusted back and forth.

The middle pin of each potentiometer is connected to input pins on the comparator (IC1). The comparator now has everything it needs to perform a comparison! It compares the middle pin of a potentiometer to the green wire in between U1 and R7. It compares the middle pin of the other potentiometer to the same green wire in between U1 and R7.

A Trick

In order to have the “backwards”-installed green LED turn on when the phototransistor is bright (using no voltage) and have the red LED do the opposite, the input wiring needs to be experimented with a bit.

The obvious condition is that the red inputs are wired the opposite of the green inputs. If the LEDs light up opposite of desired, swap the input from the potentiometer with the inputs from the phototransistor.

You can carefully think your way through the process to determine the correct layout or experiment on a solderless breadboard. This isn’t a serious problem, since the worst case is the LEDs aren’t lighting up when desired; they aren’t going to explode anything like that.

Click to see a movie of the comparator circuit in action

Click to see a movie of the comparator circuit in action.

See For Yourself

The sensor can detect objects based on the shadow cast. In this case, the phototransistor is acting more like a photo interrupter than an ambient light detector.

A finished version of this circuit appears in the second-generation hand held tachometer.