Maker Pro
Maker Pro

LDR based relay switching with the help of Very Low Cost, Low Power Microcontroller

scrollock

Apr 17, 2021
5
Joined
Apr 17, 2021
Messages
5
Hello everyone,

I hope everyone is doing great!! I am having one query hope anyone from this forum might be able to help me,

I want to switch the relay ON/OFF based on the voltage readings obtained from LDR , I want to do this task with very low cost, low power micro controller as mentioned in the subject.

Suggestions on a well to do microcontroller fitting in the mentioned criteria would be really really pleasant!!

I believe PIC10F206 can be a good selection(suggestions on better alternatives, if any are highly acceptable). PIC10F206 does have comparator, But I am unsure how to implement code for the mentioned task,

For e.g,

I want to switch ON the relay when LDR sends the voltage reading of 4.52V to PIC10F206.

I've visited the datasheet of PIC10F206, and found a block titled, "Analog Input Connection Considerations"(pg 33 , Section 8.9) but it fails to explain the working of Analog conversion or any relevant sort of thing!!

Also if code for the mentioned task is implemented, will it give accurate results?

Anyone's opinion/help in this matter from forum is highly appreciated!!, I've attached the datasheet of PIC10F206 for reference

Thanks in advance!!
 

Attachments

  • 40001239F.pdf
    1,009.8 KB · Views: 6

shumifan50

Jan 16, 2014
579
Joined
Jan 16, 2014
Messages
579
This does not require a microcontroller. There are many circuits all over the internet to switch LEDs on/off based on ambient light. Unless there is a requirement for the exact 4.52v and a microcontroller, it can be done with a voltage divider and transistor. By using a potentiometer in the voltage divider, the sensitivity can be adjusted as required.
 

scrollock

Apr 17, 2021
5
Joined
Apr 17, 2021
Messages
5
This does not require a microcontroller. There are many circuits all over the internet to switch LEDs on/off based on ambient light. Unless there is a requirement for the exact 4.52v and a microcontroller, it can be done with a voltage divider and transistor. By using a potentiometer in the voltage divider, the sensitivity can be adjusted as required.

Really Appreciate your quick response.
Yes I do have implemented similar sort of circuit which you are stating but I want to specifically do it on microcontroller that too as stated in thread.
 

Bluejets

Oct 5, 2014
6,901
Joined
Oct 5, 2014
Messages
6,901
Why exactly 4.52V...?

Arduino pro mini is around $3.00 and will allow addition of many input/output easily and easy to write code.

Don't think the analog input will stretch to the accuracy of 10's of millivolts though.
Think it is more like 5mV (on a 5V unit) but even then, not stable. Averaging might help a bit but digital far better approach.

Sounds like you don't have a lot of experience.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
@scrollock: There must be more about this "project" than you have revealed so far if the title of your thread,
"LDR based relay switching with the help of Very Low Cost, Low Power Microcontroller" is any indication of what you are trying to accomplish with a PIC.

I suspect that you want to put the PIC to "sleep" to conserve power until the LDR output is greater than or less than 4.52 volts. After the LDR voltage crosses this "trigger" level, the PIC will "wake up" and change the state of a relay, controlling who knows what and for what purpose. The PIC can then go back to sleep until another change in the comparator state occurs.

This "sleep, awaken and do something, go back to sleep" algorithm is a powerful way to minimize power consumed by the PIC, especially if the program execution task while the PIC is awake is short and simple.

One problem I see you might have is reducing the power drawn to energize the LDR, and the power drawn by a possible voltage divider that establishes the 4.52 volt reference threshold for a change in comparator state. The maximum impedance of a voltage source connected to the comparator input pins (there are two) is 10,000 ohms. This voltage divider will have a strong influence on the total current requirements, assuming the LDR is excited from a 5 VDC supply and the PIC is also operated from the same supply. Of course, since you are driving the coil of a electro-mechanical relay, its current requirements when energized are probably orders of magnitude greater than the PIC, the LDR, and whatever voltage divider you use to establish the 4.52 volt reference level for the comparator. This may not be a problem if the relay coil is energized from its own separate power supply, and you are not interested in a truly low-power implementation.


Sounds like you don't have a lot of experience.
I must agree with Bluejets on this, vis a vis experience, but the PIC 10F206 is a good PIC to learn with if you equip yourself with the right parts.. Be sure you get the 8-pin PDIP package so you can breadboard your LDR circuit with the PIC. You will also need a PICKit3 or PICKit4 programming pod. Purchase one of these directly from Microchip.com or reputable electronics distributor such as DigiKey. Do not purchase a cheap Asian knock-off product.

A small solder-less breadboard is essential, along with insulated, tinned, 22 AWG solid "hook up" wire. Don't try to use larger diameter wire by forcing it into the holes on the breadboard because you will ruin those holes for later use. Limit resistor sizes to 1/4-watt or less for the same reason. If you need to insert components with leads larger than 22 AWG, solder solid "hook up" wire to their leads.

LDRs are high-impedance, highly non-linear, low-light sensitive, resistive devices made with cadmium sulfide. Their "dark resistance" can be ten megohms or more, quickly dropping to a few tens of ohms upon exposure to normal daylight conditions. In other words a six order of magnitude change in resistance is to be expected for LDRs. This can make the LDR problematical if a specific light intensity is required for triggering the relay. Even if you get the attenuation ratio correct, the resulting impedance may be too great to properly operate the comparator in the PIC 10F260. That's where the bread-board comes in handy for trying different LDR input circuits, as well as voltage divider circuits for the 4.52 volt reference applied to the other comparator input.

BTW, WTF did you come up with the need for a 4.52 volt comparator switching threshold? And are you aware that practical circuits, i.e., circuits that actually work, require hysteresis in the comparator? This is normally accomplished using positive feedback to the non-inverting comparator input from the positive-going output when the comparator changes state.

My final advice for the moment is for you to actually print out a hard copy of the PIC datasheet. Sit down somewhere that you won't be disturbed and carefully read it. Make notes in the margins. Use a high-light pen if you spot something you think is important or that you don't understand. You must fully understand the PIC registers: the function of each bit and how it is set or cleared. Microchip is famous for cramming ten pounds of electronics into a one pound integrated circuit, using I./O bits and status bits for multiple purposes. Only by fully understanding the datasheet will you be able to produce even a simple program.

Putting the PIC to sleep is easy. Waking it up and knowing why it was awakened is another matter entirely. Your program must ignore wake-up events that did not originate from a change of state of the comparator, do whatever "housekeeping" tasks are required, and then immediately put the PIC back into sleep mode..


Also if code for the mentioned task is implemented, will it give accurate results?
Please define what you mean by "accurate results."
 

scrollock

Apr 17, 2021
5
Joined
Apr 17, 2021
Messages
5
Why exactly 4.52V...?

Arduino pro mini is around $3.00 and will allow addition of many input/output easily and easy to write code.

Don't think the analog input will stretch to the accuracy of 10's of millivolts though.
Think it is more like 5mV (on a 5V unit) but even then, not stable. Averaging might help a bit but digital far better approach.

Sounds like you don't have a lot of experience.


Thanks for Reply,

I will like to clarify ur doubts line by line,

There is nothing to do specifically with value 4.52V, The value I mentioned here is just for understanding/reference about what exactly I want, if I am changing the resistor in voltage divider then it is quite obvious that now trigger value I want will change from 4.52 to some other respective value accordingly.

Arduino pro mini is around $3.00 and will allow addition of many input/output easily and easy to write code -- yeah it will be easy to code but just for ldr based triggering I don't think it will be great to spend $3.00 where other cheaper variants can accomplish the task

Don't think the analog input will stretch to the accuracy of 10's of millivolts though.
Think it is more like 5mV (on a 5V unit) but even then, not stable. Averaging might help a bit but digital far better approach. -- Can you please bit more explain in detail?, Thanks

Sounds like you don't have a lot of experience. - Yes I do not have but I wanna learn how exactly things are done in details to gain one. Thanks for your reply!!
 

scrollock

Apr 17, 2021
5
Joined
Apr 17, 2021
Messages
5
@scrollock: There must be more about this "project" than you have revealed so far if the title of your thread,
"LDR based relay switching with the help of Very Low Cost, Low Power Microcontroller" is any indication of what you are trying to accomplish with a PIC.

I suspect that you want to put the PIC to "sleep" to conserve power until the LDR output is greater than or less than 4.52 volts. After the LDR voltage crosses this "trigger" level, the PIC will "wake up" and change the state of a relay, controlling who knows what and for what purpose. The PIC can then go back to sleep until another change in the comparator state occurs.

This "sleep, awaken and do something, go back to sleep" algorithm is a powerful way to minimize power consumed by the PIC, especially if the program execution task while the PIC is awake is short and simple.

One problem I see you might have is reducing the power drawn to energize the LDR, and the power drawn by a possible voltage divider that establishes the 4.52 volt reference threshold for a change in comparator state. The maximum impedance of a voltage source connected to the comparator input pins (there are two) is 10,000 ohms. This voltage divider will have a strong influence on the total current requirements, assuming the LDR is excited from a 5 VDC supply and the PIC is also operated from the same supply. Of course, since you are driving the coil of a electro-mechanical relay, its current requirements when energized are probably orders of magnitude greater than the PIC, the LDR, and whatever voltage divider you use to establish the 4.52 volt reference level for the comparator. This may not be a problem if the relay coil is energized from its own separate power supply, and you are not interested in a truly low-power implementation.


I must agree with Bluejets on this, vis a vis experience, but the PIC 10F206 is a good PIC to learn with if you equip yourself with the right parts.. Be sure you get the 8-pin PDIP package so you can breadboard your LDR circuit with the PIC. You will also need a PICKit3 or PICKit4 programming pod. Purchase one of these directly from Microchip.com or reputable electronics distributor such as DigiKey. Do not purchase a cheap Asian knock-off product.

A small solder-less breadboard is essential, along with insulated, tinned, 22 AWG solid "hook up" wire. Don't try to use larger diameter wire by forcing it into the holes on the breadboard because you will ruin those holes for later use. Limit resistor sizes to 1/4-watt or less for the same reason. If you need to insert components with leads larger than 22 AWG, solder solid "hook up" wire to their leads.

LDRs are high-impedance, highly non-linear, low-light sensitive, resistive devices made with cadmium sulfide. Their "dark resistance" can be ten megohms or more, quickly dropping to a few tens of ohms upon exposure to normal daylight conditions. In other words a six order of magnitude change in resistance is to be expected for LDRs. This can make the LDR problematical if a specific light intensity is required for triggering the relay. Even if you get the attenuation ratio correct, the resulting impedance may be too great to properly operate the comparator in the PIC 10F260. That's where the bread-board comes in handy for trying different LDR input circuits, as well as voltage divider circuits for the 4.52 volt reference applied to the other comparator input.

BTW, WTF did you come up with the need for a 4.52 volt comparator switching threshold? And are you aware that practical circuits, i.e., circuits that actually work, require hysteresis in the comparator? This is normally accomplished using positive feedback to the non-inverting comparator input from the positive-going output when the comparator changes state.

My final advice for the moment is for you to actually print out a hard copy of the PIC datasheet. Sit down somewhere that you won't be disturbed and carefully read it. Make notes in the margins. Use a high-light pen if you spot something you think is important or that you don't understand. You must fully understand the PIC registers: the function of each bit and how it is set or cleared. Microchip is famous for cramming ten pounds of electronics into a one pound integrated circuit, using I./O bits and status bits for multiple purposes. Only by fully understanding the datasheet will you be able to produce even a simple program.

Putting the PIC to sleep is easy. Waking it up and knowing why it was awakened is another matter entirely. Your program must ignore wake-up events that did not originate from a change of state of the comparator, do whatever "housekeeping" tasks are required, and then immediately put the PIC back into sleep mode..


Please define what you mean by "accurate results."

I really appreciate u for replying on my thread, Hereby I will be informing you about what exactly I want to accomplish. I guess you have understood only half of the mentioned thread.

Specifically there is nothing to do with 4.52V value which is concerning everybody. Its just the value I preferred to trigger the relay according to the voltage across LDR vs light intensity relation, I derived . Obviously changing the value the of resistor in voltage divider circuit will change that value. I used that value just for reference to bring more clarity on my concept or idea of implementation.

BTW, WTF did you come up with the need for a 4.52 volt comparator switching threshold? And are you aware that practical circuits, i.e., circuits that actually work, require hysteresis in the comparator? This is normally accomplished using positive feedback to the non-inverting comparator input from the positive-going output when the comparator changes state.

I have already implemented such LDR based lighting switch with help of specific comparator IC but not with comaprator embedded in microcontroller. While comparator IC implementation, I have also implemented Schmitt Trigger to avoid false triggering due to hysteresis.

My main query is that if I use PIC10F206 which does not contains ADC Peripheral, instead it contains comparator, correct me if I'm wrong but as per my understanding implementing voltage divider across this comparator will be acting as just another normal Voltage comparator ic.

Instead is it better to use a microntroller with ADC peripheral which will take voltage across LDR as an input and convert it to appropriate value and based on such value I will code to trigger relay.

In terms of accuracy which will be better ADC or embedded comparator in microcontroller?
Please define what you mean by "accurate results."

With respect to ADC referring to term accuracy I mean that, if due to change in light intensity, the voltage across LDR is changing from 3.12 to 3.25 (values mentioned are hypothetical just used for giving example for better understanding) then how much change in value of ADC will be observed? That is for value of 3.12 across LDR, ADC was converting it in to 300 (yet another hypothetical value) then for 3.25 how much change in the ADC will be observed? Will it be still giving 300 or supportive change in ADC value like 317 or 320 (yet another hypothetical value) will be observed.

If not exact but certain linear change in ADC values with respect to voltage values across LDR is obtained then it will be good accurate system.

In short, if graph for voltage across LDR vs light intensity and ADC values vs light intensity is plotted then slope of both the graph should be certainly equal.

Even I have referred different techniques of ADC such as SAR, Delta- Sigma, Dual Slope(Integrating ADC) etc.. . But as per my understanding for this LDR specific above quoted application , (correct me if I'm wrong) Dual Slope(Integrating ADC) will be most suitable. But I am unable to find a microcontroller with such ADC technique implementation.

With respect to microcontroller embedded comparator, referring to term accuracy I mean, there should be minimum effect of hysteresis on comparator ouput.
Obviously I will be offering delay to avoid any unwanted triggering, but that is secondary sort of thing.

"I suspect that you want to put the PIC to "sleep" to conserve power until the LDR output is greater than or less than 4.52 volts. After the LDR voltage crosses this "trigger" level, the PIC will "wake up" and change the state of a relay,"
Yes you suspected it correctly. I wanna do that too. But that is secondary implementation or another stage of application you can say...

Looking forward for ur valuable feedback on this .................. @hevans1944 and @Bluejets as well as other forum members

Thank You
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
how much change in value of ADC will be observed?
That depends on the resolution of the ADC. Typically small microcontrollers have 10 Bit or 12 Bit ADCs only.
10 Bit result in 210 = 1024 steps. If the input range is set to e.g. 5 V, that means each step is 5 V / 1024 = 4.9 mV. Plus tolerances and inaccuracies that are inherent in ADCs.
When you look at the datasheet of the PIC10F206, the internal comparator has
  • an input offset voltage of +- 5 mV (typical. +-10 mV max.).
    This means the accuracy of the comparator is on the order of a 10 Bit ADC
  • an input common mode voltage of VDD -1.5 V = 3.5 V at 5 V supply.
    This means you cannot use it with input voltages above 3.5 V.
  • an accuracy of the internal reference voltage (assuming you use the internal reference, not an expensive external reference) of +- 50 mV (!)
    This means that the absolute accuracy is limited to that value. This would be true for a microcontroller with internal reference, too (these usually aren't that precise due to concessions that have to be made for the manufacturing process which is optimized for the digital logic and small size of transistors).
An LDR per se is a rather inaccurate component. Look e.g. at this datasheet. It states light resistance at 10 Lux = 8 kΩ ... 20 kΩ. This is a variation by a factor of 2.5. It makes imho no sense trying to resolve the illumination sensed by such a component with the accuracy you envision. Some rigorous calibration would be required plus temperature stabilization as the resistance will also be temperature dependent. An LDR is not really the appropriate component to measure light intensity. Mostly suitable to distinguish brightness and darkness. Photodiodes (and the associated circuitry) are much better suited to measuring light intensity. It can be done with LDRs but with limited accuracy. You may want to study this article.

Dual Slope(Integrating ADC) will be most suitable.
Overkill considering the lousy specs of an LDR, see above.
 

scrollock

Apr 17, 2021
5
Joined
Apr 17, 2021
Messages
5
Thanks for sparing time to read the whole thread and sharing knowledge on the same!!

I want to implement this in open outdoors basically one of my prioritized application is Automatic Street Light and Automatic Outdoor lights switching. I am serious about this , I know there are more than 100 odd projects available on the internet (and copying neither of those) , important thing is those are projects and and I am focusing on product development for the same. I am very much confident that you understand the difference between them! Working on such application might still sounds crazy to majority of people but I still believe if its that lucid and easy then why I haven't seen its implementation on large scale and people do not have rock solid reasons for it. I welcome anyone who can provide me solid reasons for it not getting into implementation.

Getting back to our technical discussion,

This is a variation by a factor of 2.5. It makes imho no sense trying to resolve the illumination sensed by such a component with the accuracy you envision. Some rigorous calibration would be required plus temperature stabilization as the resistance will also be temperature dependent. An LDR is not really the appropriate component to measure light intensity. Mostly suitable to distinguish brightness and darkness. Photodiodes (and the associated circuitry) are much better suited to measuring light intensity

I've used photo transistor not an ordinary one which are easily available in any electronic shop, the one I have used was designed such way to give response and sensitivity to light as near as human eye. I've implemented voltage divider circuit with same voltage and same value of resistor in both LDR(CdS) and phototransistor mentioned above. The graphs of outdoors light sensitivity vs voltage across LDR and photo transistors were plotted respectively. In conclusion, not a big margin of slope was found between those graphs.

But still I don't have any hesistation to use Phototrasistor instead of LDR. My main query is which microcontroller can be used to achieve following statement,
In short, if graph for voltage across LDR vs light intensity and ADC values vs light intensity is plotted then slope of both the graph should be certainly equal.

So that based on converted value proper switching can be made!!

Surely looking for your valuable feedback on this !!

Thanks.
 
Last edited:

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
I welcome anyone who can provide me solid reasons for it not getting into implementation.
Mass marketing of commercial street lighting should be quite competitive. AFAIK, no one wants to add any unnecessary "bells and whistles" that will increase cost without a specific benefit that reduces cost. The greatest benefit for street lighting would be eliminating the time a streetlight is on when there is sufficient daylight to not need them.

OTOH, street lights that come on too late in the evening, well past the time when street lighting is necessary, is very undesirable from a safety viewpoint, even if that does save electricity. So, perhaps the world does need yet another light-level operated streetlamp product... on at dusk, off at dawn, plus or minus some margin of error as to just when dusk and dawn occur, and what is an acceptable amount of light during that interval. It may also be desirable to turn on streetlights during the day, after dawn and before dusk, if weather (influencing the amount of daylight present) dictates it is necessary.

So far, it appears that commercial street lighting depends mainly on ambient light intensity or a twenty-four hour timer or a combination of both to determine whether the street light should be lit or not. This leads to a random illumination of the street lights because of timing and level of ambient light tolerances.

It is possible, but no longer common, for a chain of street lights to be controlled from a single control point, requiring all lights in the chain to be all on simultaneously, or all to be turned off simultaneously. I think this is becoming uncommon with street lights now being equipped for autonomous operation using a simple light-sensor circuit. You would be fighting an up-hill battle to design and introduce a new product that replaces existing autonomous light control with "new and improved" light control. There is a strong NIH (Not Invented Here) bias against the introduction of new products from outside the existing manufacturer. OTOH, it may be possible to introduce a new, cost-saving, product that either replaces or works in conjunction with an existing street light. As incandescent street lights are being replaced by LED illumination, any further cost-savings from reduced electricity usage and less, or fewer, lamp replacements becomes minimal.

I've implemented voltage divider circuit with same voltage and same value of resistor in both LDR(CdS) and phototransistor mentioned above.
That seems unlikely, and at the very least is an "apples to oranges" comparison. A different circuit is required to operate a photo-transistor versus a simple CdS cell that requires only a voltage source and a current-limiting resistor in series with the cell. Photo-transistors have two modes of operation, neither of which remotely resembles CdS operation. So, unless you are willing to post some schematic diagrams of your "production" prototype, I doubt there is much more we can do for you here.

Good luck on making your fortune in industrial lighting! Someone should get all those zillions of street lights in order, turning on and off together instead of just semi-randomly, or failing to work at all. That someone might as well be you! You might also want to consider a Bluetooth or IoT local area network (LAN) to synchronize the lighting effort among a few dozen street luminaries in a neighborhood or along a stretch of highway. Try examining some of the web pages on this link for ideas. Or try this one for an idea of the competition from just one manufacturer.
 
Top