Feb 05 2018

Controlling Things Based on Light Intensity

A PhotoCell or LDR (Light Dependent Resistor), used in a circuit, allows controlling things based on light intensity.

PhotoCell

PhotoCell / LDR (Light Dependent Resistor)

Here is a circuit showing how to wire an LDR and demonstrates how controlling things based on light intensity is possible with a little help from a microcontroller.

Controlling Things Based on Light Intensity

Controlling Things Based on Light Intensity

The thing being controlled in this circuit is simply an LED which turns On or Off. However, it could just as easily be a relay to control some other device. (Get the Fritzing source HERE for the above circuit graphic).

An analog value of the voltage which varies with light intensity can be read by a microcontroller. In this circuit, a PIC12F683 microcontroller is used. Based on that value, other circuitry can be controlled (i.e. turned On or Off).

Here is the MPLAB X IDE C source code for the PIC12F683 in the circuit shown above.

The analog value varies from 0 to 1024 with 0 being no voltage and 1024 being max voltage. The code shows turning on the LED if at least 1/2 of the voltage  (1/2 of 1024 = 512) is being passed by the LDR. Since the source voltage is 5V,  1/2 of it would be 2.5 volts.  Depending on the application for this sort of circuit, some experimentation can be done to determine the optimum value for when to turn something On/Off.

The data sheet for the PIC12F683 is HERE.  It is essential reading in order to understand how to write code for the chip. Native C coding (using the MPLAB X IDE) for PIC microcontrollers is a bit harder than the ‘sheltered’ environment of the Arduino IDE.

Yes, you can do this sort of thing with an Arduino (and the code would be easier to develop) but at a higher component cost. The components in this sample circuit are minimal, small and low cost.

Leave a Reply

Your email address will not be published.