How should a beginner balance theory and practical - LDR project - Page 1
Forum section: electronics / beginners

author: AH1234
author_rank: Contributor
timestamp: May 02, 2020, 07:35:12 pm
content: This is a 2 part post1st part is with regards to specific problem2nd part is more a request for advice1st PartHaving come up with the idea of trying a nightlight project, I feel like I've hit a brick wall with regards to my understanding of the fixed resister value used within a voltage divider alongside an LDR. I've gone through weeks of tutorials on the theory of circuit analysis, voltage dividers, transistors, voltage bias which reference many formulas on the saturation point of a resistor, the active region and cut off alongside general circuit analysis, I've also viewed tutorials that replicate what I do but they seem to indicate stick any fixed resistor value, some suggest 15k whereas others suggest 100k, it would be easy for me to copy their solution though I would rather understand the formulas and be able to apply them to problems with other variables. I want to use the transistor as a switch not as an amplifier at fixed values whereas there always seems to be some value of the ldr resistor where the transistor amplifies in the active region.Darkness - 180k resistanceLight - 506 resistanceI want the night light to activate at over 5k and not underI'm using a 9v batteryIs there a specific formula or method I can use or is simply a case of experimenting with various resistor values in a simulator until I can mock up the desired result. I'm using Falstad and tinkercad for simulation and then hopefully I will move onto the practical with my own ldr which will have it's own resistance values hence why I would like to understand the process.2nd PartMy interest in electronics is mainly in the practical though within this seemingly simple problem I've delved extensively into theory without being able to connect it to the practical so essentially obsessed about circuit analysis without making any progress on the practical side, how do you guys deal with disconnect in electronics between the theory and the practical sometimes, my resources are mainly youtube videos so some are overtly theoretical whereas others are wholly practical skipping any knowledge of theory. My interests are low power components so I don't want to go for the easy way of simply providing a conditional within an microcontroller.I have a multimeter, ldr and all the tools to do the project but me not being able to have certainty within my circuit analysis understanding is preventing me from starting the project outside of a simulator.Any advice in this regard would be greatly appreciated, I'm open to changing my approach to learning.

author: Manul
author_rank: Super Contributor
timestamp: May 02, 2020, 08:24:43 pm
content: Well, I have a question, are you really interested in doing this with discrete components? If you are really serious about learning electronics and willing to become a true master, thats fine. On another hand, time is changing and in most cases building complex things with discrete components is a thing of the past. In my opinion it is better to focus on using ready made building blocks like op amps, comparators, logic gates, especially for hobbyists.Your circuit with one transistor can be made to somewhat work and turn on a relay for example. But it is lacking a lot. It is hard to calculate. Depends on power supply voltage and temperature. It does not have a sharp turn-on point. It does not have any hysteresis (turn on and turn off points are same). So what it means that if you want to build a nice and reliable circuit, you will need to add more and more to it making it complex. Or it can be easily and reliably done with a standard analog comparator and this is what any professional engineer would do.

author: rstofer
author_rank: Super Contributor
timestamp: May 02, 2020, 08:51:33 pm
content: Take a 100k resistor (or any other recommended value) and wire it in series with the LDR and put the two series elements across the 9v battery. Now, measure the VOLTAGE at the junction of the resistor and LDR as a function of light level. Maybe create a plot with Excel if you have a way to vary the light level.100k is nice for low power consumption because, the very most is could dissipate is 92/100k or 810 uW. But the range of output voltages may not be what you want. The voltage comparator should have very high impedance so it shouldn't affect the voltage divider. You want the top resistor to be as high as possible consistent with getting what you want out of the comparator.Here is a paper on designing a comparator with hysterisis.http://www.ti.com/lit/ug/tidu020a/tidu020a.pdfFind a comparator with low power consumption and you are good to go. Section 3.1 recommends a TLV3201 but it's voltage limited to 5.5V and designed for 5V systems.https://www.ti.com/product/TLV3201I suppose a simple voltage regulator would handle this because the quiescent current is only 40 uA. Make sure the regulator is reasonably efficient, it will probably use more power than the rest of the circuit combined. Or design the system to work off of 3 AA batteries. That would be my approach. Or maybe just a single CR123A 3V Lithium battery. Two in parallel if battery life is too short.

author: AH1234
author_rank: Contributor
timestamp: May 02, 2020, 09:28:52 pm
content: I am a hobbyist, I shall take up your advice and work at a higher level. You are right in that working with discrete components only could increase my understanding of electronics but would detract from any projects I work as it has in this regard. An analog comparator seems perfect.I'd previously thought of high level as being an arduino or a microcontroller which would require a lot more power with functionality I wouldn't necessarily use, I suppose with the building blocks you've recommended I'll only use as much functionality as I need.

author: tggzzz
author_rank: Super Contributor
timestamp: May 02, 2020, 10:14:34 pm
content: It is good that you try to balance theory with practice; both are needed.A bipolar junction transistor is principally a current amplifier. It is not a switch unless it is overdriven into saturation, but before it gets there it will operate in a linear region. That is probably undesirable for a light switch. In addition, the amplifier characteristics (principally hfe) are very variable between device and with temperature. Much analogue electronics theory is devoted to determining how to reduce that practical variability.In your case I suspect an analogue comparator is a good starting point for a switch. If you like transistors rather than ics, note that a comparator input is two transistors in a long-tailed pair configuration.

author: rstofer
author_rank: Super Contributor
timestamp: May 02, 2020, 11:20:39 pm
content: Quote from: AH1234 on May 02, 2020, 09:28:52 pmI'm also a hobbyist who just happens to avoid analog at every opportunity. I'm more interested in digital...Most of us just want to get our projects done so we can move on to the next project. If the job calls for a voltage comparison, especially if hysteresis is required, a comparator is the right thing to use. Frankly, if I could find an 8 pin uC with ADC, I might be tempted. The problem with that solution is the requirement on the driving source to be a relatively low impedance (sometimes just a couple of k Ohms). This implies the divider is going to use a lot of power and that isn't desirable. There are exceptions, of course and I could buffer the resistor-LDR chain with an op amp and maybe the uC even has an exposed op amp but at some point the idea just blows up. If a comparator is the right thing then use it.

author: Zero999
author_rank: Super Contributor
timestamp: May 03, 2020, 12:06:56 pm
content: Quote from: AH1234 on May 02, 2020, 09:28:52 pmAs mentioned above, an analogue comparator is a good solution. A comparator is just a circuit which compares two voltages and turns its output on, when one of them is higher than the other.There are a few catches for the beginner. For a start most comparator ICs limit on the input voltage range to slightly less than the power supply rails, with a range of 0V to +V-1.5V being pretty common, although some are more restrictive and others will work slightly outside the supply rails. Another name for this is common mode range. Another catch is the output of many comparator chips only pulls down to 0V, not up to +V i.e. it's a current sink, not source. This means to get a positive voltage out, a resistor needs to be connected to +V, also known as a pull-up resistor. Finally the maximum output current drive is typically only 10mA or so, before it either overheats or the output voltage starts to rise.Going back to the original post: you didn't say how much power the light uses? If it's just a small 5mm LED, it can be driven directly by the comparator, otherwise a small relay or transistor will be required.Here's how I'd do it. Because the comparator can only pull its output lower, a PNP transistor is used to drive the load, as it turns on, when its base is pulled below its emitter voltage, i.e. when U1's output goes low. R1 & R2 and R3 & R4 form two potential dividers. The voltages made by each potential divider are compared by U1. When the - input is higher than the + input, its output will go low, pulling the base of Q1 low, turning on the load. R5 is a positive feedback resistor which provides hysteresis. When the output of U1 is low, it's in parallel with R4 and when the output is high, it's in parallel with R3 (R6 and R7 are in series with R5 but are a low enough value to be ignored).http://physicsnet.co.uk/a-level-physics-as-a2/current-electricity/potential-divider/https://www.electronics-tutorials.ws/resistor/res_4.htmlI selected R4 to be around the same value as you want your LDR to be when it turns on and 5k1 is the nearest E24 value. R1 = R3 and 68k were selected to ensure the voltage would still be 1.5V under the supply voltage, when the LDR is at its highest resistance of 180k, even if the battery voltage is 6V when it's nearly flat, so 4.5V. V = V1*R1/(R1+R2) = 6*(180+68) = 4.35V. I didn't worry about the value of R5 much, just select a resistor with a high value. If it oscillates, select a lower value resistor.comparator basic pot divider.asc(2.1 kB - downloaded 61 times.)EDIT:Oh and on the subject of which comparator IC to use: it depends on the requirements. There are lots of good comparator ICs, which hardly use any power and will run of 9V, but many are in surface mount packages only. The LMC7211 and TSX3702 spring to mind, both of which have a push-pull output, which means R7 on my schematic can be omitted. If you want through hole, then there's the LM393, but it uses a bit more power.

author: AH1234
author_rank: Contributor
timestamp: May 03, 2020, 07:11:54 pm
content: https://www.tinkercad.com/things/cmpodXorHpPI managed to get it working so that an led only turns on when the LDR resistance is above 1K ohm. Ideally I'd like to use a voltage regulatorfor increased precision though this seems to fry the comparator intermittently, from the simulation when I've attempted to use a 5V LM7805 regulator. I'll need to read up on voltage regulators to understand why.Next steps are to increase precision and also use in conjunction with a PIR sensor.@Zero999 I've not used PNP transistors before, why the need for R5, R6 and R7. How would you compare your schematic with my approach in my simulation. Feel free to openly criticism, I am keen to gain an understanding on your schematic.

author: Manul
author_rank: Super Contributor
timestamp: May 03, 2020, 07:34:37 pm
content: This circuit can not work, because LM393 has open collector output, it can only sink current. You need to connect led resistor to +9, not ground.

author: Zero999
author_rank: Super Contributor
timestamp: May 03, 2020, 09:29:35 pm
content: Quote from: AH1234 on May 03, 2020, 07:11:54 pmI'm not familiar with that simulator and couldn't get it to work on my machine, even though I did disable no script for that page. Try to get used to using the proper symbols for schematics, rather than pictures. It makes it easier to understand for most people. It should work with the LM7805 with no problem, although there's no need for a regulated supply for this circuit, as the voltages at both of the comparator inputs will change proportionally with the power supply, so it won't make any difference.As mentioned above, the LM393 has an open collector output. This means it needs a resistor connected between its output and +V, otherwise it won't work. Here's a simplified schematic of internals of the LM393. Focus on Q8, which is the output transistor.QuoteR5 is nothing to do with the transistor. It adds hysteresis, i.e. positive feedback. When U1's output is high i.e. connected to +V, via R6 & R7, R5 will be in parallel with R3, lifting the voltage at the non-inverting pin, so the voltage at the inverting input will need to be a little higher, for U1's output to go low. When U1's output is low, i.e. connected to 0V, via the internal transistor, R5 will be in parallel with R4, so the voltage on the inverting input will be a little lower. For more information Google comparator hysteresis.PNP transistors work the same way, as NPN transistors, it's just that the voltages and currents are all negative. When the base voltage falls below the emitter voltage, the transistor will turn on. This means, Q1 will turn on, when U!'s output goes low. R6 limits the base current to Q1 and R7 pulls up Q1's base, when U1's output transistor turns off.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: May 03, 2020, 10:40:24 pm
content: There are a bunch of tradoffs to be made.For best sensitivity of the sensor itself, you want a series resistor that is equal to the resistance of the LDR in your area of interest. So in your case apparently 5k.This is easy to deduce if you look at the extremes:*If your series resistance is a near short (say 1Ohm) then there will always be 9V (your battery voltage) over the sensor, now matter how bright or dark it is.* If your series resistance is very big (say 100MOhm), then there is almost no current through the LDR and the LDR voltage will always be very low.* if your series resistor is about the same value as the LDR in your area of interest, then there will be around 4.5V over the LDR during twilight and that voltage will vary a lot when it gets brighter / darker.Another factor for the tradoff is power consumption. Especially when fed from a small battery you want a very low power consumption. With a 5k resistor there will be 9V/5k = 1.8mA through the resistor during daytime. This is quite a lot for a 9V block battery.Detecting twilight does not have to be very precise. So by increasing the resistor from 5k to 100k, the power consumption during daytime will be 20x lower. With this decrease in sensitivity, the relative sensitivity for other factors such as temperature will also get worse.It's also possible to add a microcontroller for lower power consumption. Then you can power the resistor and LDR from a microcontoller pin, and then put the microcontroller in sleep mode most of the time (which will only need a micro amp or less) and then once every few minutes the uC can wake up, make the I/O pin high, and read the LDR voltage, and then make the output pin low again. Depending on the reaction speed of the LDR and the time needed to take a sample this whole process probably takes less than a milli second. So if you take a sample every ten minutes the power consumption for the LDR + resistor is reduced by a factor of 1000 * 60 *10 = a very big number. This probably means that the main power consumption is for the Idle current of the voltage regulator.For real low power design the leakage current of a bad Elco can dominate.Also, if you buy a (cheap) PIR sensor, then there is usually a light sensor included. LDR's are old school and were made of Cadmium Sulfide, which is not RoHS compliant. In these days it's more common to find a silicon based light sensor in these things.

author: Zero999
author_rank: Super Contributor
timestamp: May 04, 2020, 04:29:12 pm
content: Quote from: Doctorandus_P on May 03, 2020, 10:40:24 pmYou forgot the common mode range of most comparator ICs doesn't extend all the way to the positive rail. If you just select 5k for the upper side of the potential divider, then when the LDR reaches its maximum resistance of 180k, the voltage will be very close to the supply voltage, which will exceed the input voltage range of the LM393. If you have a comparator with a rail-rail input then this will be fine, but I think you'll get way with it with the LM393, as its output transistor will be on anyway, with the non-inverting input close to the +V rail and inverting-input at half the supply voltage. Still it's not good to blindly do this without considering the repercussions first. Other comparators might not play so nicely.QuoteA microcontroller is a reasonable suggestion, in which case it would make more sense to scrap the comparator and use an ADC, but don't forget the current consumption of the voltage regulator, as no modern MCUs will work directly from 9V. Some have an internal shunt regulator option, but that requires a relatively high current.

author: wizard69
author_rank: Super Contributor
timestamp: May 04, 2020, 11:56:29 pm
content: Quote from: AH1234 on May 02, 2020, 09:28:52 pmPeople get involved in electronics for a variety of reasons, as such there may be good reason to go through the learning experience of analog design with discrete parts if the goal is to develop a deep understanding of analog electronics. If you are trying to come up with a solution then yeah grab the right components that make for a quick and reliable design.QuoteEven this might not be true depending upon what you are trying to achieve. Cheap low power microprocessors are available these days with analog inputs. If your solution needs adjustable set points for turn on, hysteresis or whatever, a microprocessor might be a good solution. Good solutions start with understanding what you are trying to achieve.Whatever you choose don't knock a single transistor discrete approach because sometimes it is the right approach. In an industrial setting we had an engineer come up with a discrete transistor solution to object detection that has worked reliably for years. It was the right approach years ago and would likely be the right solution done today. That solution sits right next to a high end vision system running on a PC. With time you will have a better idea what is the best tech for a given problem.

author: AH1234
author_rank: Contributor
timestamp: May 06, 2020, 12:06:31 am
content: @wizard99 I'm learning a lot but at the same time I'd like to progress towards completing a project. You're right with time I'll understand what to use, I'm still developing an intuition for the basics.I've ordered a bunch of LDRs so I'll go with an LDR for now, I'll likely use with a PR sensor and an ATTiny85 with some kind of sleep mode settings as recommended by @Doctorandus_P.I've learnt a lot with the suggestions provided here, I may come back to using a comparator if need be as a learning exercise or as a progression.After some reading I managed to figure out what you guys meant by the open collector output and pull up resistor. I also managed to get it working with a voltage regulator once I understood this concept though decided to omit this. Changing the LDR resistor to 500 turns off the LED, within daylight the current through the resistors is very lower, far lower than 1mA. I've moved to LTSpice for simulations.

author: Zero999
author_rank: Super Contributor
timestamp: May 08, 2020, 11:09:26 am
content: Quote from: AH1234 on May 06, 2020, 12:06:31 amThat will turn on at 1k, not 5k though. The voltage across the LED, when it needs to turn on will be only 9*1/(180+1) = 50mV which makes it more susceptible to noise. You also forgot the hysteresis resistor which reduces the chance of oscillation.Unfortunately LTSpice doesn't have a variable resistor model to simulate this. One way round this is to use a voltage controlled resistor. Here's a plot of the current through the LEDs vs the LDR resistance. Note the hysteresis resistor cause slightly different on and off thresholds, which is desirable and helps to prevent oscillation caused by some of the light from the LEDs shining on the LDR.night light schematic.asc(2.75 kB - downloaded 105 times.)