Maker Pro
Maker Pro

Microcontroller controlled by pushbutton for LED flashlight

Update: I am having a right nightmare trying to get this PIC to go to sleep and wake-up on the watch dog. With the watch dog enabled I am in a tight software loop trying to go to sleep but the watch dog keeps resetting the PIC instead of resuming the program from where it left off. I have spent far to long on this PIC so I have ordered the next version up of the PIC (P10F320) and a PICKIT 3 and I am going to install MPLABX tonight. @hevans1944 if you can get it to go to sleep I'll give you a big kiss from the UK :). Sorry it's taking me so long. I am a week behind due to work at home.
Adam
Adam,

That is how sleep works on the baseline PICs, which the 10F200 is. It resets the PIC on wakeup. That is one reason why I recommended the 10F320 instead, it is an enhanced midrange PIC and will wake up after the sleep instruction.

Bob
 
Adam,

You can check the ~TO bit in the STATUS register to tell the difference between power up and wake up from sleep on WDT timeout.

Bob
 
Adam,

That is how sleep works on the baseline PICs, which the 10F200 is. It resets the PIC on wakeup. That is one reason why I recommended the 10F320 instead, it is an enhanced midrange PIC and will wake up after the sleep instruction.

Bob

Duhh that's why I couldn't get it to work LOL. I must have missed it. Not used to such a basic PIC, sorry. I got the other PICs today and guess what they work :)
Adam
 
Adam,

You can check the ~TO bit in the STATUS register to tell the difference between power up and wake up from sleep on WDT timeout.

Bob
Hi Bob
I don't want the PIC to reset I want to use the sleep command from the start because the way Jeff wants the button to work is the light follows the button position up until 500 ms, if I don't use sleep we would need a capacitor large enough to allow 500 ms of runtime. Then we need to sleep when it's on fully and pump the capacitor.
Adam
 
Hi Bob
I don't want the PIC to reset I want to use the sleep command from the start because the way Jeff wants the button to work is the light follows the button position up until 500 ms, if I don't use sleep we would need a capacitor large enough to allow 500 ms of runtime. Then we need to sleep when it's on fully and pump the capacitor.
Adam
I see that you are now using the 10F320. The post you were replying to was the workaround for using the 10F200. On the 10F320 sleep does indeed work they way you wanted it to.

Bob
 
Hi Bob
I don't want the PIC to reset I want to use the sleep command from the start because the way Jeff wants the button to work is the light follows the button position up until 500 ms, if I don't use sleep we would need a capacitor large enough to allow 500 ms of runtime. Then we need to sleep when it's on fully and pump the capacitor.
Adam

The quick press of the switch is used for mode changing of the LED driver. The switch would need to be able to be repeatedly pressed for less than 500ms up to 30 times.
 
The quick press of the switch is used for mode changing of the LED driver. The switch would need to be able to be repeatedly pressed for less than 500ms up to 30 times.

Ok I assume the modes are taken care of by the LED light? In normal operation when the LED lamp turns off briefly to charge the capacitor will this not confuse the LED light and change it's mode? It was planned to be turned of every 100 ms so as far as the LED lamp is concerned it's only on for 100 ms. Do you see any issues with this?
Adam
 
I see that you are now using the 10F320. The post you were replying to was the workaround for using the 10F200. On the 10F320 sleep does indeed work they way you wanted it to.

Bob
Thanks Bob, good job I got 5 spares I think I just stuffed one. It thinks the port is always low, btfss PORTA, 1 doesn't work any more. :(
Adam
 
I don't know the exact minimum off duration required to perform a mode change, but I'm pretty sure it's in the range of 500ms also. One ms would be invisible to the driver.
 

hevans1944

Hop - AC8NS
The quick press of the switch is used for mode changing of the LED driver. The switch would need to be able to be repeatedly pressed for less than 500ms up to 30 times.
Oops! This appears to be a new requirement: pressing the switch for less than 500 ms and then releasing it to indicate a mode change to the LED driver module.

One could hope that the release interval has some minimum requirement, say on the order of 500 ms, to avoid the 1 ms LED OFF duration that occurs every 100 ms (when the LED is ON) from being interpreted by the LED driver as a switch release and hence a request for a mode change. @TenderTendon, can you send me an LED driver module so I can test this?

The fact that for the PIC10F20x series there is only one "interrupt" ...a full-blown reset... is a royal PITA. The reset condition can occur in just four ways: power on reset (lasting for about 18 ms); master clear reset (if enabled); watch-dog timer timeout; and input bit-transition event. I suppose it is blessing that bits in the STATUS word allow the program to determine which of these four conditions reset the processor.

Of course there are only two reset conditions we are interested in after putting the PIC to sleep: watch-dog timer timeout and input bit-transition event. The latter to detect when the switch changes state, the former to time the one millisecond "blink" every one hundred milliseconds when the LED driver is on.

Unfortunately it is not possible to put the PIC to sleep AND use the TIMER0 function because TIMER0 operates off the microprocessor clock, which stops during sleep mode. It is possible to use the watchdog timer. The watchdog timer is a "feature" intended to recover from a software glitch by starting over from a fresh beginning. It is a rather crude timer without the accuracy of the PIC's main clock, but it is "good enuf" for its intended purpose. And it should be good enough for this project, timing not only the 100 ms intervals but also the one millisecond "MOSFET OFF" intervals too. We just need to do some housekeeping, set the watchdog timer, go to sleep. Sleep most of the time!

The project must also keep an eye on the push button switch, since this determines what state the machine will be in: OFF, ON, Sustained ON, and Forced OFF. The normal idle mode is OFF with the energy storage capacitor fully charged through the Schottky diode, the LED Driver module and the flashlight battery. In the OFF state we will let the PIC go to sleep while we wait for an input state-transition from the push button switch. No need to pulse the MOSFET off because it is already off. No need to mess with the watchdog timer yet.

When a button press occurs, the PIC wakes up. No need to "debounce" the switch. We just assume that it will be pressed for at least 100 ms and turn on the MOSFET, set the watchdog to bark after 100 ms, and go back to sleep. We are now in the ON state.

When the watchdog barks, we look at the switch again. If it is released the MOSFET is turned off, we revert to the OFF state, setup for a switch transition, and go back to sleep. If the switch is still on, things get a little more complicated. Now we need to set the watchdog to bark after only one millisecond, still turn off the MOSFET, and keep track of how many times this occurs. After the millisecond expires and the watchdog barks, turn the MOSFET on, set the watchdog timer to expire in 100 ms and go back to sleep. Each time the watchdog barks in this mode, we need to check how many times we have been here and note whether the switch is still pressed. After five iterations, if the switch is still pressed, we move to Sustained ON mode.

The same operations occur in Sustained ON as occurred in ON (turn off the MOSFET, set watchdog to bark one millisecond later, go to sleep, wake up and look at the switch, turn on the MOSFET, set watchdog to bark in 100 ms, and go to sleep again. However, while in Sustained ON state we must recognize when the switch is released and store that information in a non-volatile memory variable. We also enable, after the switch is released, the reset-on-input-transition (which we disabled when we left the OFF state and entered the ON state) so the PIC resets when the push button is pressed again, whether or not the watchdog timer has finished with its 100 ms delay.

Pressing the switch again, after releasing it in the Sustained ON state, takes us to the Forced OFF state. We immediately turn the MOSFET off, (thereby restoring power to the energy storage capacitor), disable the watchdog timer, perform whatever "housekeeping" is needed, and revert to the OFF state, waiting for another button push.

I haven't actually coded any of the above yet. I would really like to see how Adam has approached the problem so I don't re-invent his wheel. I do know there is a coding problem in the board that @TenderTendon sent me because the MOSFET gate is always at common potential, there is no pull-up to Vcc for the normally-open push button switch, and pins 3, 5, and 6 are always at Vcc potential, in this case +4.65 VDC from three series-connected AA cells providing power. Pins 1, 2, and 4 are always at Vss (common potential).

@Arouse1973 would you upload the latest version of your code so I can try it out on the board that @TenderTendon sent me?

I know Adam is now programming for the PIC10F320 (and I have recently received a "hobby kit" with five 10F206 PICs), but the differences may not be important. You still can't use the internal timer(s) while the PIC is in sleep mode, although apparantly the -320 can wake up from a watchdog timeout without resetting the PIC.

I have the MPLAB X IDE installed on my laptop now, and it appears to communicate with the PICkit 3 programming interface. I also have a PIC10F206 soldered to an 8-pin carrier and installed on a small breadboard, along with five leads to connect it to the PICkit 3. I am using the same three AA cells to power this as I was using on the flashlight prototype board. All I need to do now is get up to speed on how to use the MPLAB X IDE! It's been quite an adventure so far...

73 de AC8NS
Hop
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Just popped back in to see how things are going.

I had an idea that might make things far simpler. If you connect a voltage detector with an open collector output which pulls low when the supply voltage reduces below (say) 2.5V so that it turns the mosfet off when triggered, this would automatically take care of recharging the capacitor.

The issues with this are:
  1. One additional component (SOT-23 should be available)
  2. time delay between the voltage dip and the activiation of the output (you need a fast one)
I have some of these at home, I'll wire up something to test it later today if I can find the time...
 
Oops! This appears to be a new requirement: pressing the switch for less than 500 ms and then releasing it to indicate a mode change to the LED driver module.

One could hope that the release interval has some minimum requirement, say on the order of 500 ms, to avoid the 1 ms LED OFF duration that occurs every 100 ms (when the LED is ON) from being interpreted by the LED driver as a switch release and hence a request for a mode change. @TenderTendon, can you send me an LED driver module so I can test this?

The fact that for the PIC10F20x series there is only one "interrupt" ...a full-blown reset... is a royal PITA. The reset condition can occur in just four ways: power on reset (lasting for about 18 ms); master clear reset (if enabled); watch-dog timer timeout; and input bit-transition event. I suppose it is blessing that bits in the STATUS word allow the program to determine which of these four conditions reset the processor.

Of course there are only two reset conditions we are interested in after putting the PIC to sleep: watch-dog timer timeout and input bit-transition event. The latter to detect when the switch changes state, the former to time the one millisecond "blink" every one hundred milliseconds when the LED driver is on.

Unfortunately it is not possible to put the PIC to sleep AND use the TIMER0 function because TIMER0 operates off the microprocessor clock, which stops during sleep mode. It is possible to use the watchdog timer. The watchdog timer is a "feature" intended to recover from a software glitch by starting over from a fresh beginning. It is a rather crude timer without the accuracy of the PIC's main clock, but it is "good enuf" for its intended purpose. And it should be good enough for this project, timing not only the 100 ms intervals but also the one millisecond "MOSFET OFF" intervals too. We just need to do some housekeeping, set the watchdog timer, go to sleep. Sleep most of the time!

The project must also keep an eye on the push button switch, since this determines what state the machine will be in: OFF, ON, Sustained ON, and Forced OFF. The normal idle mode is OFF with the energy storage capacitor fully charged through the Schottky diode, the LED Driver module and the flashlight battery. In the OFF state we will let the PIC go to sleep while we wait for an input state-transition from the push button switch. No need to pulse the MOSFET off because it is already off. No need to mess with the watchdog timer yet.

When a button press occurs, the PIC wakes up. No need to "debounce" the switch. We just assume that it will be pressed for at least 100 ms and turn on the MOSFET, set the watchdog to bark after 100 ms, and go back to sleep. We are now in the ON state.

When the watchdog barks, we look at the switch again. If it is released the MOSFET is turned off, we revert to the OFF state, setup for a switch transition, and go back to sleep. If the switch is still on, things get a little more complicated. Now we need to set the watchdog to bark after only one millisecond, still turn off the MOSFET, and keep track of how many times this occurs. After the millisecond expires and the watchdog barks, turn the MOSFET on, set the watchdog timer to expire in 100 ms and go back to sleep. Each time the watchdog barks in this mode, we need to check how many times we have been here and note whether the switch is still pressed. After five iterations, if the switch is still pressed, we move to Sustained ON mode.

The same operations occur in Sustained ON as occurred in ON (turn off the MOSFET, set watchdog to bark one millisecond later, go to sleep, wake up and look at the switch, turn on the MOSFET, set watchdog to bark in 100 ms, and go to sleep again. However, while in Sustained ON state we must recognize when the switch is released and store that information in a non-volatile memory variable. We also enable, after the switch is released, the reset-on-input-transition (which we disabled when we left the OFF state and entered the ON state) so the PIC resets when the push button is pressed again, whether or not the watchdog timer has finished with its 100 ms delay.

Pressing the switch again, after releasing it in the Sustained ON state, takes us to the Forced OFF state. We immediately turn the MOSFET off, (thereby restoring power to the energy storage capacitor), disable the watchdog timer, perform whatever "housekeeping" is needed, and revert to the OFF state, waiting for another button push.

I haven't actually coded any of the above yet. I would really like to see how Adam has approached the problem so I don't re-invent his wheel. I do know there is a coding problem in the board that @TenderTendon sent me because the MOSFET gate is always at common potential, there is no pull-up to Vcc for the normally-open push button switch, and pins 3, 5, and 6 are always at Vcc potential, in this case +4.65 VDC from three series-connected AA cells providing power. Pins 1, 2, and 4 are always at Vss (common potential).

@Arouse1973 would you upload the latest version of your code so I can try it out on the board that @TenderTendon sent me?

I know Adam is now programming for the PIC10F320 (and I have recently received a "hobby kit" with five 10F106 PICs), but the differences may not be important. You still can't use the internal timer(s) while the PIC is in sleep mode, although apparantly the -320 can wake up from a watchdog timeout without resetting the PIC.

I have the MPLAB X IDE installed on my laptop now, and it appears to communicate with teh PICkit 3 programming interface. I also have a PIC10F206 soldered to an 8-pin carrier and installed on a small breadboard, along with five leads to connect it to the PICkit 3. I am using the same three AA cells to power this as I was using on the flashlight prototype board. All I need to do now is get up to speed on how to use the MPLAB X IDE! It's been quite an adventure so far...

73 de AC8NS
Hop

Hi Hop
post 143 zip file is my latest code . It doesnt have any sleep function, just normal operation. I am working on some simpler code for the 302 which uses sleep mode. I can use the watchdog timer as the delay functions and change them on the fly if I want to. I am also trying to make it as easy as possible for Jeff to modify later on by using macros, defines and equates.
Thanks
Adam
 
Just popped back in to see how things are going.

I had an idea that might make things far simpler. If you connect a voltage detector with an open collector output which pulls low when the supply voltage reduces below (say) 2.5V so that it turns the mosfet off when triggered, this would automatically take care of recharging the capacitor.

The issues with this are:
  1. One additional component (SOT-23 should be available)
  2. time delay between the voltage dip and the activiation of the output (you need a fast one)
I have some of these at home, I'll wire up something to test it later today if I can find the time...
Sounds good Steve.
 
Update. I have the software responding to the switch presses i.e on and off. Just need to add the interupt for off in the cap charge routine. My parrents are visiting tommorow, so wont have much time this weekend.

Dear Santa
Can I please have a bubble with mplabx, a pizza machine, cider machine, with no phone.
Thanks
Your little helper.
 
Oops! This appears to be a new requirement: pressing the switch for less than 500 ms and then releasing it to indicate a mode change to the LED driver module.

Actually it isn't a new. It is just buried very early in the thread, In post #47, I gave a link to the driver which explains the operation and gives instructions for changing modes and programming the turbo timer. You wouldn't have caught it unless you were closely following from the start.

I have 36 drivers here right now, but they are all spoken for. I have to ship them out Tuesday. I will order another and have it shipped directly to you.
 

hevans1944

Hop - AC8NS
Still on the learning curve using the MPLAB X IDE.

I did take a "time out" to review the PIC 10F200 datasheet current requirements for the PIC in sleep mode. My hope that the current would drop to just a few microamperes during sleep mode was confirmed. The watchdog timer (WDT) current, Ipd, typically varies from about 1 μA to 6 μA (depending on Vpp) during sleep. Microchip only samples these values on a few production chips. so they are "typical"not "guaranteed". The datasheet says the watchdog timer can use up to 13 μA when Vdd = 5 V. Hopefully we won't go anywhere near that much current.

If we take the "average" Ipd value of 6 μA for the WDT at Vdd = 4.5 V, the 1 μF capacitor will discharge at a rate of dV/dt = I / C = /0.000006 A / 0.000001 F = 6 V/s. During the 100 ms interval when the MOSFET is conducting and the capacitor is not charging, the capacitor should lose about 0.6 V of its terminal potential. This emphasizes the fact that the PIC must spend most of its time sleeping if we are to keep the capacitor charged up to a minimum operating voltage of about 2.0 V, and not drain the flashlight battery when the MOSFET is off.

Is one millisecond enough time to recharge the capacitor when the PIC is NOT asleep?

If the capacitor voltage has dropped to 2.0 V, and the battery in the flashlight is providing 4.0 V through the 330 Ω resistor in parallel with the LED Driver Module, then a current of 2.0 / 330 = 6 mA is available to charge the capacitor and operate the PIC. The datasheet says a maximum of 275 μA of current, Idd, is required to operate the awake PIC at 2.0 V Vcc. So, if true, that leaves 5.725 mA available to charge the capacitor, which will increase its terminal potential at an initial rate of 0.005725 A / 0.000001 F = 5725 V/s or 5.725 V per millisecond. Although this does not take into account that the charging current decreases as the capacitor terminal potential rises, or that the PIC current, Idd, increases as Vdd rises above 2.0 V, but there does appear to be ample time available to re-charge the capacitor in one millisecond while providing sufficient current to operate the PIC while it is awake.

Now, back to figuring out how to begin a Project in MPLAB X IDE, roll some assembly code to check out PIC functionality, assemble and link and download into the PICkit 3 programmer/debugger. I think the first thing I will test is basic input and output on ports GP0, GP1, GP2, and GP3. Maybe light up an LED or two. I guess that will also be a good test of my ability to solder the SO23 chip onto the 8-pin DIP carrier.

If all that goes well tonight, then onward to figuring out how the WDT works... and measuring some actual operating currents. It should be pretty simple to insert a small N-channel MOSFET and a 330 Ω resistor on the breadboard to simulate the rest of the flashlight. I have some Schottky diodes laying around somewhere, but I think an ordinary signal diode, or even a power diode, would be sufficient to keep the capacitor from discharging back through the MOSFET when I turn it on. The only reason to use a Schottky diode is to minimize the forward voltage drop when the capacitor is being re-charged.

More on this later...

73 de AC8NS
Hop
 

hevans1944

Hop - AC8NS
... I have 36 drivers here right now, but they are all spoken for. I have to ship them out Tuesday. I will order another and have it shipped directly to you.

No hurry on getting one of the drivers to me. You did mention in post #44 that you had no idea how much off time was required to change modes, so that is what I want to test, eventually..

Until I am convinced I understand how to program my breadboard version (PIC10F206 SOT-23 devices) using the MPLAB X IDE there will not be any need to test the LED Driver Module functionality. For now, I will just assume that interrupting the LED current (which is orders of magnitude greater than the current available through the 330 Ω shunt resistor) for one millisecond out of every 100 milliseconds will not effect a mode change. I will let you know when the breadboard testing is completed, after which I will go back to using your prototype board.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
The voltage detectors I have on hand are not easily available, and I don't have any suitable open drain outputs, nevertheless I will test with what I have to see if the concept is sound.

The type of device I'm using is similar to these: http://ww1.microchip.com/downloads/en/DeviceDoc/20001434K.pdf

The important things will be:

  1. It has the correct voltage range
  2. It has an open drain output
  3. It does not have an inbuilt delay
  4. It does reset "active low"
  5. It is available :)
There may be others cheaper than the one for which I have posted the datasheet, this is just to give you the idea.

Can someone verify the minimum input voltage (2.8V?) and the minimum voltage the PIC can reliably run at?
 
Top