0
\$\begingroup\$

edit: uploaded schematic: I am a novice, this diagram may not be correct but should convey the attempt.

I want to turn-on power to an arduino microcontroller when it is dark.

I white-boarded an example and uploaded the diagram below.

Power

Using a photoresistor and a transistor, I plan to switch the high side that carries power to the arduino from a 5v dc adapter (eventually a battery pack).

LEDS

I plan to control 5v power to the LED's through the arduino's output ports, and use 200Ω resistors to regulate the current on the ground side.

Question:

How do I determine the resistor values for R1 and R2. I'm new to EE and ohm's law, sorry.

Is my logic sound? Is this terribly inefficient?

Reference

enter image description here

enter image description here

\$\endgroup\$
6
  • \$\begingroup\$ Use the tool to draw a schematic, this one is unreadable, make sure you provide designators for resistors and transistors. to know what R2 would be we'd also need to know what port of the arudino this is going to and a detailed explanation \$\endgroup\$
    – Voltage Spike
    Aug 3, 2018 at 21:20
  • \$\begingroup\$ It appears that one end of your photoresistor isn't connected... \$\endgroup\$
    – Daniel
    Aug 3, 2018 at 21:25
  • 1
    \$\begingroup\$ A darkness sensor may not work well unless well-isolated from the LED light sources you're controlling. Photoresistors collect light from a broad area. \$\endgroup\$
    – glen_geek
    Aug 3, 2018 at 21:25
  • 1
    \$\begingroup\$ Great points, I'll try to draw out the diagram using an online tool. You are correct, I would connect the other side of the photoresistor to the common ground. ( I think this is correct ) \$\endgroup\$ Aug 3, 2018 at 21:27
  • 1
    \$\begingroup\$ It may be a better idea to have the Arduino monitoring the light level, so that it can make its own intelligent decisions. If you're concerned about power consumption, you can put the Arduino in a lower power mode when it's not dark. \$\endgroup\$
    – Daniel
    Aug 3, 2018 at 21:27

5 Answers 5

0
\$\begingroup\$

A problem such as this can be more accurately implemented with photodiode and TIA Op Amp design or an active “light sensor”. PD's have far more accurate tolerances than LDR's, so you do not need a pot to adjust.

A single transister is a comparator using the Vbe reference voltage but the gain is not very high compared to a comparator and the reference voltage is subject to variations with bias current but it may be adequate.

Then all you need to do is aim it in the right direction to avoid night light errors and determine the thresholds for On and Off. Instead of a wide angle detector, it can be a narrow tube of heat shrink to block unwanted light.

If the reflected light detected from a night light is lower than ambient light then the sensor optical design is simpler.

I suggest to analyze existing designs and find how error tolerances are discussed to understand more. Then study comparators for threshold with hysteresis. The answers to these depend on your keyword searches.

A simple design must meet all the detail requirements, so keep asking yourself, what is input & output tolerance, so you understand the transfer function tolerance , where it matters. https://www.google.com/search?q=ldr+light+switch+design&num=50&client=firefox-b&tbm=isch&tbo=u&source=univ&sa=X&ved=2ahUKEwjk04r39dHcAhVH1qwKHSWRD3gQsAR6BAgBEAE&biw=818&bih=810

\$\endgroup\$
2
  • \$\begingroup\$ I like the idea of overcoming the variable reading of the photoresistor using better circuit design to gate it to an On/Off reading.. really admire the ability of you guys. I think, however, that is too advanced for me at the moment :( \$\endgroup\$ Aug 3, 2018 at 22:07
  • \$\begingroup\$ I will mark this as the answer, due to the details and my needs changing. Thank you for the explanation. \$\endgroup\$ Aug 3, 2018 at 22:23
1
\$\begingroup\$

Daniel has an excellent approach - keep the microcontroller powered and alive always.
Since a photoresistor has high resistance in darkness, use it to charge a capacitor on one of the microcontroller's input pins. It will charge slowly in darkness, much faster when illuminated. Any microcontroller is very adept at measuring this charge time.

schematic

simulate this circuit – Schematic created using CircuitLab


Perhaps the photoresistance rises to 500Kohms as darkness approaches. If C1 starts at zero volts (discharged), it's voltage will rise exponentially to about 3V in half a second. In total darkness, charge time will be longer.
Leakage currents of the input pin have not been accounted for, but are usually quite small.
Once a logic high has been sensed at the input pin, you can change it to be an output pin, so that C1 can be discharged back to zero volts. This will take about 500 microseconds. Once discharged back to zero volts, the pin is changed back to an input pin, and a timer is started. When the input pin changes from logic 0 to logic 1, the timer is stopped - the time taken is a measure of "darkness".

It is also possible to use the microcontroller's analog-to-digital converter to measure the speed of voltage rise on C1.

Most microcontrollers have a low-power state that draws very little current from its supply (V1). If current consumption from the supply is to be minimized, the microcontroller can spend most of its time asleep, awakening once in awhile to test for darkness. In this case, C1 might be made much smaller to minimize the time taken for its voltage to rise.
Do keep LDR1 optically isolated from the LED light sources.

\$\endgroup\$
0
\$\begingroup\$

The main problem with your plan is that the resistance of the LDR changes gradually as the light level changes, which means that the power supplied to the Arduino would also change gradually. What you need is something that snaps fully on when it's dark and then snaps fully off when it's light.

You could cascade several stages of transistors to increase the gain. Use a power PNP for the final stage and omit R2...it should be zero ohms. I understand that this advice may not lead you to an actual circuit, but I fear that this is a little over your head.

\$\endgroup\$
2
  • \$\begingroup\$ correct, I believe my best bet is to use the emitter side as an input to the arduino and control the ouputs based on a threshold of that reading. Then leave the arduino powered on, without switching. \$\endgroup\$ Aug 3, 2018 at 22:05
  • \$\begingroup\$ No, Q1 should be a PNP transistor and its collector should be connected to the arduino. The transistor's emitter should connect directly to the 5V supply, with no series resistor. You'll have to reverse the connections of the LDR so the PNP's base is pulled low in the dark. \$\endgroup\$ Aug 3, 2018 at 23:41
0
\$\begingroup\$

The approach of turning power to the Arduino off only makes sense if you are using something like an Arduino UNO or the like where there are devices (Serial to USB) and indicator LEDs that would result in unavoidable current draw.

If you are using just the ATMega328P (with no ancillary chips) it makes more sense to simply put the MCU to sleep.

The logic you propose using the LDR with a transistor will not work well since the Arduino could be turned off at any time (an unintended consequence) if the light level drops (such as a hand over the LDR). You have very poor control unless you have definite hysteresis points defined.

I would suggest that you need some form of logic on the input and would be much better using a FET rather than a transistor as the power switch. Perhaps something along these lines:

schematic

simulate this circuit – Schematic created using CircuitLab

Here I've suggested a TinyLogic device, the NC7SV57. This device is configurable and shown here it's a 2 input NOR gate used to drive the gate of a P-Channel FET.

The configuration of the NC7SZ57 is as follows:

enter image description here

The second input is used by the MCU to ensure that once you've powered up you can use a DIO pin from the Arduino to ensure the power switch remains on (set the IO pin high) until you decide to change the status. When the Arduino is powered is off this pin is held low by R2 so that only the LDR input is used.

It's also worth reading about using an LED as the optical sensor in a solution where you can put the MCU to sleep. This has the advantage that you don't need high gain op-amps, level comparators, LDR or photodiode. All you have to do is use two DIO pins to drive one of your LEDs (a bit like an H-bridge). You can then use a timing loop to ascertain the light level.

\$\endgroup\$
-1
\$\begingroup\$

enter image description here

void setup(){

Serial.begin (115200,SERIAL_8N1);

pinMode(7,INPUT_PULLUP); pinMode(2,OUTPUT);

}

void loop(){

if(analogRead(7) <= 450 ){ digitalWrite(2,HIGH);

}

else { digitalWrite(2,LOW);

}

Serial.print("a7="); Serial.println(analogRead(7));

}
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to EE.SE. There is a code formatting button {} on the editor toolbar. Please edit your question to format the code properly. There is also a schematic editor button that is easy to use and creates professional looking schematics. \$\endgroup\$
    – Transistor
    Aug 5, 2018 at 10:35
  • \$\begingroup\$ Select all the code then press the {} code button. (It adds four spaces to the start of each line.) Look at the preview before submitting the answer and fix it if it is still not right. Remove blank lines and indent properly. \$\endgroup\$
    – Transistor
    Aug 5, 2018 at 14:05
  • \$\begingroup\$ -1 This answer doesn't address the original question about controlling power to the Arduino. You don't even attempt to explain what the code is supposed to do. Finally, I think your logic is backwards...you will turn on the LED when it is light, not dark. \$\endgroup\$ Aug 5, 2018 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.