[Rajendra Bhatt] wrote in to share the latest in a series of PIC tutorials, which covers the microcontroller’s Sleep mode – a very useful tool for limiting current consumption in battery-powered applications.
He discusses how to put the PIC in sleep mode, as well as some common mistakes to watch out for, such as accidentally allowing I/O pins to sink or source current while sleeping. [Rajendra] also walks through the various ways a PIC can be brought out of sleep mode, focusing the majority of his tutorial to the mcu’s watchdog timer.
Using a PIC16F628A, he constructs a test circuit which allows him to demonstrate the power savings gained by using sleep mode rather than the microcontroller’s built-in delay function. The circuit simply blinks an LED every 4.3 seconds, using the watchdog timer for the first 2.3 seconds, and a delay() call for the rest of the time.
The power savings are quite substantial, similar to the results we saw using AVR microcontrollers a few weeks ago. [Rajendra] found that using the sleep function limited current consumption to about 4.5% of the current used when calling the mcu’s delay function – a huge savings.
It should be noted that the microcontroller doesn’t have a built-in (implying hardware) delay function. delay() is done in software by making a huge loop of NOPs and leaves the uC fully powered and active; hence the higher power consumption than sleep mode.
Sometimes it is better to just use the processor idle commands instead of sleeping the cpu. It is a lot easier to program for and the gains between idle and full speed processing are pretty significant.
I get a bit the impression, that people here are giving lectures without being on top of things in the first place…
“grew up in the 80′s banging away…but always assumed that someone had to be some kind of assembly language genius to do that.”
Dude — were you in a coma the decade that the BASICStamp reigned supreme?
In any event.. welcome back!
Yeah, I looked into the Basic Stamp back in 2006, but it was much more expensive, much less flexible, and much poorer performance-wise than what was available for PICs. I also didn’t want to invest money and time in a platform that I would soon outgrow. I have no regrets.
I’m not an electrical engineer, just someone who grew up in the 80’s banging away in Apple LOGO, then BASIC, then Turbo Pascal and Turbo C++. I kind of drifted away from the hobby before object oriented programming (quite rightly) became a standard.
I’d always dreamed of playing around with embedded systems, building robots, home automation, etc – but always assumed that someone had to be some kind of assembly language genius to do that. When I learned that there was a compiler for BASIC available for PIC microcontrollers (picbasic pro, http://melabs.com ), I decided it was worth a try.
I bought a cheap Pic programmer (Pickit 2) — ordered some components from digikey, downloaded a bunch of datasheets and manuals… And spent my summer vacation trying to figure out how to build basic circuits, make LED’s blink, etc.
My second project was a clock that interfaced with an LCD panel. It was incredibly satisfying, except I didn’t know anything about the relative imprecision of on-chip oscillators — so the clock was very inaccurate (would gain or lose 10-15 minutes a day). This ended up being moot, however because it would burn through a 9V battery in less than 4 hours. I’ve come a long way since then.
I’m very thankful for posts like this one — most of this stuff is obvious to engineers, but to beginning-intermediate hobbyists like me, these kinds of articles are very helpful.
Even better for getting into pics is swordfish. It is a free basic compiler and very easy to learn and comes with libraries to support all kinds of hardware.
http://www.sfcompiler.co.uk/swordfish
This is a nice and useful article.
But while I don’t doubt that the PIC in sleep mode is going to draw less current I am a bit wary of the accuracy of the measurements here.
In all the photos the multimeter is showing the low-battery warning symbol, and some meters can give quite large errors with a ‘flat’ battery.
Very true, also the multimeter looks like its directly in series with the battery. When measuring stuff in the uA range, the shunt resistor value is quite high and give you a significant voltage drop called ‘burden voltage’. You should really have the thing hooked up to a variable supply to compensate for the voltage drop, otherwise your measuments could potentially be inaccurate.
The author used a 1:128 prescaler on the watchdog timer to stretch the 18ms the WDT takes to rollover to about 2.3s (0.018s*128 = 2,304s). Then he waits another 2 seconds using a delay loop (wasting energy in those 2 seconds).
4.3s/0,018s = 238,… rollovers. Wouldn’t it be absolutely possible to just let the Watchdog rollover every 18 ms, increase a counter variable and check if it is bigger than (or equal to) 238, so counting the number of wakeups instead of the time and then just spending the last milliseconds using a delay loop?
Considering the processor wastes power on wake-up as well, otherwise you could certainly get a better sleep/delay-loop-ration by using another prescaler? For example, using a prescaler of 64, rollover time would be 0,018s*64 = 1,152s. Let the processor wake up 2 times doing nothing, falling to sleep again. On the third rollover, 3*1,152s = 3.456s will have passed, so there’s only 4.3s-3.456s = 0.844s to be spend in a delay loop. More than a second of delay-loops saved at the cost of 2 more wake-ups. Saving power? Probably, I would guess so.
(btw, I’m totally not into watchdogs yet, just as a disclaimer. watchdog-noob if you want so.)
I think the point of this was more to show how much power is saved, than to implement it the best way possible. At your point, why not just sleep the watchdog twice at 2.3 seconds, leading to 4.6 seconds of sleep time. Since it’s only turning an LED on for .5 seconds at the end of the loop, an extra .3 seconds of delay is not terrible. But, again, I think it was just to say ‘look at how much power sleep mode can save!’
The author used the Delay_ms(2000) to measure the current consumption when the PIC is delayed by software so that it could be compared with the Sleep mode current (the first 2.3 sec of total 4.3 sec interval).
In college, one of the labs we did in a circuits class was to build a simple detector that would wake up within X seconds when you put an index card in front of it. The challenge of the lab was actually to be the group that could meet all requirements with the lowest power usage (longest battery life). It was a competition between about 10 groups and we ended up winning by using a low power PIC running off a simple voltage divider/follower off of a 9V. I think I learned more about power consumption in that lab than anything else I did in college.