Maker Pro
Maker Pro

More efficient LDR "dark" switch?

hevans1944

Hop - AC8NS
It is certainly possible for digital logic circuits to bite you in the ass, especially if you don't read and understand everything that is important to know... like if you want to use the I/O pins for digital I/O instead of using them as analog comparator inputs, and not knowing they default to being analog inputs, and that you have to disable the comparator to use those two pins as digital I/O. But once you do understand, it's a done deal and will never rise up to bite you again. That is NEVER true with analog circuits, whose operation is always dictated by Murphy's Law: if something can go wrong, it will. And this has a corollary: Murphy was an optimist.

You can still get your circuit to work in analog fashion. You just need to increase the power supply voltage a little to make things easier. There is probably no need for an LED to turn on when it gets dark, or if there is, it could be a small LED with limited brightness... after all, it will be dark so you can easily see it! If you arrange the circuit so that bright light saturates the output transistor, you can PROBABLY (remember Murphy, though!) use that to hold the 555 timer in reset until it gets dark and starts your timed interval. If you need some hysteresis at the day/night switching point, there are some simple Schmitt trigger circuits to play around with on the web, or you can purchase two 555 timers in one 14-pin DIP package and use one of them as a set/reset latch with hysteresis.

Your outline of how to use the PIC is exactly how I would do it. I would maybe add a "calibrate" button that you press after the delay starts. When the delay is long enough to your satisfaction, momentarily press the button and the program records the time interval in a non-volatile RAM location... well, non-volatile as long as power is applied. So, the next "day" it operates un-attended when night falls.

There are other PICs that have true non-volatile EEPROM storage that persists even after a power failure... but let's not go there yet!
 
That is interesting - I thought analog were more robust than digital! I know that analog is more "noisy", but I thought it was digital that suffered from the noise where analog ignored it until it passed a certain threshold. Very different perspective, thanks!

I would prefer to stay at one battery max, 2032 preferably since they are cheap and ubiquitous. I will abandon the analog for now, perhaps returning to it to learn about schmitt triggers, playing with the voltage, etc

Your outline of how to use the PIC is exactly how I would do it. I would maybe add a "calibrate" button that you press after the delay starts. When the delay is long enough to your satisfaction, momentarily press the button and the program records the time interval in a non-volatile RAM location... well, non-volatile as long as power is applied. So, the next "day" it operates un-attended when night falls.

Cool, great minds think alike!! Hahaha
As for the button press to record delay time? I was satisfied to leave the LDR to determine the appropriate time to turn on based on ambient light. Since this can be used in multiple locations of varying light intensity, I would leave the LDR as an active input to turn the circuit on/off. I could alternatively program it to turn off after the initial turn on for a period of x hours or whatever, but I think the LDR should be the control. Are you stating this for concern on battery life? Didn't you just come up with a sleep function that was promising low battery consumption?

I think I will have to have a look at the nano power series and see what kind of power consumption one can assume.

Thanks again for the lively contributions!
 
Oh, I think I found a suitable chip - PIC10LF320T
The L stands for Low voltage I believe as this unit functions down to 1.8v - I think that this is an important feature - the program can run longer on the battery before it runs down. Another cool feature is the draw, much lower than the 0f200:

• Standby Current:
- 20 nA @ 1.8V, typical
• Operating Current:
-25μA @ 1 MHz, 1.8V, typical
• Watchdog Timer Current:
- 500 nA @ 1.8V, typical

vs the 10f200

• Operating Current:
- < 175μA @ 2V, 4 MHz, typical
• Standby Current:
- 100 nA @ 2V, typical


Of course the limiting factor will be having sufficient voltage for the LED to operate, so I guess that once the battery drops below 2.2v the LED's wont light. Perhaps its a mute point, but I thought it was pretty cool.
 

hevans1944

Hop - AC8NS
I misunderstood. I thought you wanted the LDR to trigger a time delay after it got dark. Why else would you use a 555 timer as a timer, while objecting to using two of them so one could serve as a Schmitt trigger? And why program the PIC with a timer?

It has been my experience that both analog and digital designs can be robust. It is a matter of gaining the experience and knowledge of what you have to do to make a robust design. Noise has almost nothing to do with it. There is always noise present. You just deal with it. For example, when I interface a computer to the real world, I like to use optical isolators. These devices are slow compared to a direct-wired connection, but they prevent ground loops from occurring that can inject enough noise to make a digital signal unintelligible. Same thing can happen with analog signals too, but the solution there is to raise the source signal in amplitude high enough to ignore the noise, or encode (modulate) it in such a manner that noise cancels over the communication path, or at least averages out to zero, Either way increases the signal-to-noise ratio, which is always a good thing... unless it's at the expense of having to increase power immensely. Think of the problems of doing two-way communication with objects orbiting out beyond Neptune. Sure, we can apply brute force to the transmitters on our end, but the other end is severely limited in the power it can bring to the communications session. And that's where digital and analog join hands to solve the problem. It's a fascinating area involving mathematics I probably no longer understand, plus new mathematics I sure as hell will never understand. Ever hear of a qubit? Who ordered that!

I am seeing on the order of ten nanoamperes (my meter resolution) from the PIC10F206 when it is asleep doing nothing... because it is asleep and can't do anything until something wakes it up... pin change or the watchdog timer, typically. Can also wake up on comparator change of state. Someone sent me this link recently (you?) on some very low power PICs. Vellly interesting!

I finally got almost all of the "logic" worked out for the Flashlight Project. It's a lot a words (two pages, single-spaced), but the code should be simple. Would you like to see it after I polish it up a bit? It will be interesting to compare the written description of what the logic does to the actual assembly code that implements it. You might even want to have a crack at programming the logic description in C. I will see if I can draw up a schematic. Pretty much as Kris envisioned, except I use an output port to enable and disable an external 10 KΩ pull-up resistor on the push-button switch.

Off to bed now...
 

hevans1944

Hop - AC8NS
Oh, I think I found a suitable chip - PIC10LF320T
The L stands for Low voltage I believe as this unit functions down to 1.8v - I think that this is an important feature - the program can run longer on the battery before it runs down. Another cool feature is the draw, much lower than the 0f200:

• Standby Current:
- 20 nA @ 1.8V, typical
• Operating Current:
-25μA @ 1 MHz, 1.8V, typical
• Watchdog Timer Current:
- 500 nA @ 1.8V, typical

vs the 10f200

• Operating Current:
- < 175μA @ 2V, 4 MHz, typical
• Standby Current:
- 100 nA @ 2V, typical


Of course the limiting factor will be having sufficient voltage for the LED to operate, so I guess that once the battery drops below 2.2v the LED's wont light. Perhaps its a mute point, but I thought it was pretty cool.
This is the chip that Adam used for the Flashlight Project. I have ten of them at home in 8-pin PDIP package that I will use to test Adam's code.
 
This is the chip that Adam used for the Flashlight Project. I have ten of them at home in 8-pin PDIP package that I will use to test Adam's code.
Cool! We are along similar thoughts again :) I was reviewing the datasheet last night, nearly 200 pages! Daunting.
You are right in what you said earlier, half the battle is knowing what you need to address and setup first. I wish microchip had better documentation, especially for non engineers as to how to do a basic setup on the pic. I think after church, I will search the web and their site for some kind of pic primer. I have read over some of microchips introductory material, but I don't find it to be engaging or offer the information I am looking for.

(I have looked over Gooligum's primer, which is the best to date.)
 

hevans1944

Hop - AC8NS
Well, actually, I think Adam used the PIC10F320 instead of the low-power version. I bought the low-power version from Digi-Key and then noticed from the datasheet that the maximum Vdd spec is only 4.0 V for the PIC10LF320 versus 6.0 V for the PIC10F320. So I gotta be careful when I get around to powering that puppy up on a breadboard setup. Back to using two "D" cells in series maybe, to get a nice safe 3 V. Those should last for a year or two!

I didn't comment earlier when you posted it, but I did go visit this website. I wonder if there is anything comparable available for sale in the states? It looks like a nice range of useful products for the electronics hobbyist, but it's all a little pricey. I am still looking for an inexpensive LCR tester that will do what the Peak Atlas LCR45 does. I just don't want to have to pay £90.00 plus shipping from the UK for it! Might have to do that, though. I found only one LCR meter at Hamvention® this year, from Hong Kong and selling for $200.00.

I just now registered on the Gooligum website and can now download all their free tutorials. Looks like an excellent resource.
 
Well, actually, I think Adam used the PIC10F320 instead of the low-power version. I bought the low-power version from Digi-Key and then noticed from the datasheet that the maximum Vdd spec is only 4.0 V for the PIC10LF320 versus 6.0 V for the PIC10F320. So I gotta be careful when I get around to powering that puppy up on a breadboard setup. Back to using two "D" cells in series maybe, to get a nice safe 3 V. Those should last for a year or two!
You seem to be reasonable stocked with components, maybe you have a LM317 handy? You can couple that to your existing 7805? and re regulate down to 3v. Beats batteries IMHO.

I didn't comment earlier when you posted it, but I did go visit this website.
Yes, that website had a lot of neat toys. Pricey, but I bet they are extremely well made and should last a lifetime.
I just now registered on the Gooligum website and can now download all their free tutorials. Looks like an excellent resource.
Cool! I hope you get better mileage out of them - the thing I notice with many of the tutorials is that I get easily confused since few or none are written for the particular chip I have on hand. So, of course, in trying to kludge the info together of whats presented and making it work with my chip there is some confusion and frustration. All's well, its the process ;-)
 

hevans1944

Hop - AC8NS
You seem to be reasonable stocked with components, maybe you have a LM317 handy? You can couple that to your existing 7805? and re regulate down to 3v. Beats batteries IMHO. ...
Yes, I picked up an LM317 at Radio Shack a few weeks (months?) ago, along with the 5 V 7805 3-pin series regulator. I've been using a 9 V DC "brick" rated at 300 mA but most of its current capacity is being used to light an LED to remind me that it is on. The LM317 is not a low-drop-out (LDO) regulator. It needs about 3 V input to output differential to work properly, so trying to re-regulate down to 3 V from 5 V is a non-starter. When the time comes to use the PIC10LF320, I will just pop out the 7805 and pop in an LM317 and a couple of resistors to set the output voltage.

The PIC10F206 has been operating in sleep mode most of the time lately while I learn how to program it. Current demand is barely noticeable unless I drive some real-world component such as an LED.

Right now I am wrapped around the axle trying to figure out how to initialize RAM just once and then go to sleep without re-initializing upon wakeup. The key seems to be testing the not-TO (bit 4) and perhaps the not-PD (bit 3) bits in the STATUS register, along with the GPWUF (bit 7) to recognize a key-press. This bit testing has to be done during the power-on initialization (where the RAM memory variables are initialized) so the program can branch to either a button-press or a WDT routine without executing the power-on initialization again.

I almost had it right a few days ago, toggling an output port every other WDT timeout! Then I found Application Note AN-720c from Microchip that explains how to do it by distinguishing between a POR and a WDT reset by examining bits in the STATUS register. They are using the temperature sensitivity of the WDT oscillator to measure chip temperature by calibrating said sensitivity against the internal timer, which is presumably not sensitive (or not as sensitive) to temperature changes. Pretty clever (I thought), but since they used the chip's internal timer they couldn't put the PIC to sleep since that would stop the internal timer (but not the WDT). I'm not using the internal timer for the flashlight project, but I am probably going to use the "wake up on pin change" feature, so I need to see if that was activated between WDT resets.

Another problem has to do with the TRIS register, which determines whether an output port is active or tri-stated. It turns out that if an output port is active, either sourcing or sinking current, it remains in that state during sleep. However, as soon as a wake up occurs, and the PIC resets back to address 0x00, the TRIS register is also reset to all ones! Of course that means all the outputs become high-impedance until you execute a TRIS GPIO instruction with the "right stuff" in the accumulator. This is a "feature" that apparently exists across the entire range of PIC processors. It protects the chip from errant external voltages feeding back into active outputs on startup. The OPTION register is also reset to all ones, meaning it needs to be re-initialized for each reset. I can easily solve both the TRIS and OPTION reset problems by re-initializing them each time the PIC wakes up from sleep.

Cool! I hope you get better mileage out of them - the thing I notice with many of the tutorials is that I get easily confused since few or none are written for the particular chip I have on hand. So, of course, in trying to kludge the info together of whats presented and making it work with my chip there is some confusion and frustration. All's well, its the process ;-)
I am not getting any mileage from the Gooligum because the free tutorials are not helpful, and I won't pay for the other tutorials until I decide they offer something I cannot get by carefully reading the datasheets. But I agree with you 100% on the tutorials not being chip-specific enough. I have spent months trying to find so-called "tips-and-tricks" used to program just the 10F200 series. You would think there would be lots of information on using the WDT with sleep mode, and how to use the STATUS register to skip around initializing memory, after doing it once during initial power up. Maybe I should write a tutorial after I get it all sorted out.
 

Attachments

  • WDT 00720c.pdf
    121.6 KB · Views: 96
Don't give up yet on the analogue approach ;). I think the reason you weren't getting much LED current was because you didn't do the Darlington mod Colin suggested in post #8 ? Without the mod the transistors drop ~ 0.7V. With the mod they drop only ~0.1V.
Here's a sim showing the current boost you get by doing that mod. LED current is plotted as a function of LDR resistance :-
DarkSwitch.gif
 
Don't give up yet on the analogue approach ;). I think the reason you weren't getting much LED current was because you didn't do the Darlington mod Colin suggested in post #8 ? Without the mod the transistors drop ~ 0.7V. With the mod they drop only ~0.1V.
Here's a sim showing the current boost you get by doing that mod. LED current is plotted as a function of LDR resistance :-
View attachment 20872
I'll have to give that another go - as I recall, I had followed Colin's direction to troubleshoot the circuit and with the first transistor going to +3v as he mentioned. We noticed the LED was brighter and measured 2mA. This was with the LDR removed from the circuit.
 
Top