This post would provide a working example of how to make a simple Light Meter (PhotoMeter) using 16bit PIC microcontroller and a LDR (Light Dependent Resistor). Or how to interface LDR with MicroController (MCU).
You may download code and Proteus Simulation from the download section at the bottom of this post. I am using MPLAB X IDE, C30 Compiler and Proteus Simulation on v8.0. This code is written in C Language and will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing the code.
In this post I supposed that you know following things if not then please go through following posts before proceeding with this.
· How to Blink an LED. (Click here)
· How to interface an LCD. (Click here)
· How to configure ADC module. (Click here)
· Digital OhmMeter. (Click here)
The science behind measuring Light using LDR is very simple. I used some basic formulas as Intensity depends on distance too. So I was not able to find perfect formula for finding precise light intensity in case of LDR (resistance to Intensity relation). So I used resistance change maximum and minimum to make a guess of intensity of Light. This will help in basic LDR application. My formula is at minimum intensity LDR has Maximum resistance offered which is about 990kΩ which I measured in Proteus using Current and Voltage probes. Maximum resistance means minimum light (approaches to zero light intensity). Now to find Intensity in percentage I used this formula
“I = (ResistanceLDR(at current light)/ MaximumResistance) x 100”
This gives percentage but resistance decrease with increase in Intensity so I used simple mathematics
“I = 100 – I (Intensity measured from above formula)”
In the following figure 1. I used a fix resistor (R3 = 10kΩ) with a LDR. This creates a potential divider circuit. I connect a common point of both LDR and R3 to ADC channel 0 (AN0) for measuring Potential divider voltage. Now I have only one variable in Potential Divider formula which is resistance offered by LDR. Which I used later for calculating Light in percentage. I am using ADC channel AN0 for measurement of voltage Divider. I used LCD on PortB.
|Figure 1. Schematic Proteus|
In the following figure 2. Animation shows that when I increase distance between LDR and Light source the Intensity increases. And with the decrease in Distance Intensity decreases. If you notice the major jump in intensity from 6% - 80% at first step is because at 6% light source is OFF and when it turned ON after first step Intensity goes to 80%. One another thing keep it in mind is that the curve between LDR and Intensity is Non-Linear.
In the following figure 3. This is main function in which I first initialize LCD, then I configure ADC with Channel 0. And a loop which only calls a function named DisplayLightIntensity() which is responsible for displaying measured light intensity in percentage on LCD.
In the following figure 4. We have DisplayLightIntensity() which has a string that is printed directly and a variables named “VoltageOutput” has the value of Potential divider voltage which will change according to Light Intensity. With Potential divider formula “Vo = (LDR/ LDR+ R3) x Vcc”. We can find resistance of LDR at certain Intensity. Now by using formula that is explained above in this post we get the intensity in percentage. This formula is written in Line 33 in the figure 4. Don’t confuse with an additional multiplication with 1000. I just used this to make my variable in milli so that I can print it in decimal figures on LCD.
|Figure 4. Display Light Intensity.|
Now at this stage we measured intensity of light in variable named “Light”. For displaying this value on LCD we have function named WriteLongAsFloatUptoFiveFigures(). This function will display variable “Light” by passing it as a parameter.
You can download Code (MPLAB X and C30 compiler) and Proteus (v8) Simulation. Click here
For all topics Click here:
That’s all for this post hope you will learn please comment if you have any questions for upcoming posts please subscribe or follow.
All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission.
© Copyright 2013 IbrahimLabs. All rights reserved.