Electronic Design

Measure light intensity with an 8-bit microcontroller

Light sensors find their way into a host of interesting applications. For instance, a light sensor in a camera measures the amount of light that the film will be exposed to. Once the amount of light is known, the proper lens aperture can be calculated to make sure that the picture is taken with the proper amount of exposure.

In a smoke detector, a light sensor can be used to measure the amount of light transmitted by a known light source, such as an LED, through the air inside the sensor assembly. When the air becomes smoky, the amount of light received by the sensor changes. If the amount of light change goes above a preset threshold, then more than likely something nearby is burning, and a horn is activated to indicate there’s a fire in the building.

There are many other applications for light sensors, such as flame detectors, security systems, lighting control, robotics, etc. In these applications, many of us think that since the sensor produces an analog output, interfacing this type of sensor to a microcontroller will require a conventional analog-to-digital converter. Actually, though, by using just a few discrete components, interfacing a light sensor to an A/D-less microcontroller is very simple.

Photodiodes and phototransistors are two of the most popular and lowcost light sensors. These devices are readily available in the $1 range. Both devices produce current outputs as a function of light intensity. The operating range of such devices varies depending on the manufacturer. Many of these sensors are equipped with builtin lenses tuned to particular wavelengths, so they’re most effective for detecting or measuring light with those wavelengths. To get the best performance, the voltage across the sensor must be held constant during measurement.

The circuit shown represents a very simple method of interfacing a light sensor to the PIC12C620 microcontroller (see the figure). The light sensor selected in the example is Photonic Detectors’ PDB-C107 (available through Digi-Key). The PNP transistor (Q1) and resistors R1 and R2 are used to provide a constant sensor voltage (VS) within 1 to 1.3 V. The collector current of Q1 is approximately the same as the sensor current (IS). Capacitor C1 integrates IS and generates a voltage ramp with a slope that’s proportional to the light intensity seen by the sensor.

The microcontroller has two voltage comparators and an internal voltage reference. One of the voltage comparators and the voltage reference are used to interface to the sensor circuitry. The second voltage comparator, left unused in this example, is available for other application-defined tasks, such as temperature measurement using a thermistor, ac-line zero-crossing detection, etc. The RA3 pin has multiple functions. It can be configured as a digital I/O, or an analog connection to the inverting input of the voltage comparator. Both RA3 and RA0 pins are used to control the sensor. Initially, the system is in an idle state, where RA0 is a high output to disable the sensor, and RA3 is a low output to discharge C1 through R3. This idle state helps minimize power consumption.

To start a measurement, RA0 is set to a low output to activate the sensor circuitry. RA2 is now set to an input and connected to the voltage comparator’s inverting input. The noninverting input of the comparator is connected to the internal voltage reference. The capacitor voltage then starts to ramp up. The microcontroller now begins its timer while monitoring the state of the comparator’s output. When the capacitor voltage and the voltage reference are equal, the comparator output goes from a high to a low state.

As soon as the microcontroller detects this transition, it stops its timer. At this point, the measurement is completed, and the micro sets the RA3 and RA0 port lines back to the idle state. The time measured during the ramp is inversely proportional to IS. The microcontroller can process this information to meet whatever the application’s goal is, such as activating the horn in a smoke detector, running the motor in a robot, or simply sending the reading to the host computer.

Depending on the application, system calibration and linearization may be required. The size of C1 and the maximum measurement time depend on the light sensor, amount of light used in the application, and the internal voltage-reference setting.

In many other applications in which the measurement resolution and accuracy aren’t critical, such as detecting whether the lamp is 25% or 50% on, an even lower-cost microcontroller without any analog peripheral (such as the 8-pin PIC12C508) can be used. Instead of utilizing a voltage comparator with an internal voltage reference, the capacitor voltage measurement can be done on a regular I/O pin. The microcontroller now will measure the voltage ramp from 0 V to its high input threshold voltage. The system measurement error, stability, and repeatability will directly reflect the threshold voltage variations due to the system’s powersupply voltage, process variations, and the device temperature.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.