How do lights know how long they've been off?

wyager

Flashlight Enthusiast
Joined
Feb 10, 2010
Messages
1,114
Hey guys,
I want to add PWM control to a project I'm doing, and I was wondering how the lights know how long they have been off (eg. less than two seconds off=brightness switch, longer=go to default mode). Does anyone know how this works?

Thanks,
will
 
Im just guessing here, but i believe that a capacitor would be used to store a little power running to the controller chip and when this power has been used it will forget which mode it was in and return back to its default setting.
 
You can either order a board that has these specs that you call for or you can use a pic(micro-controller) that can be programmed or flashed with specific specs. Take a look at sparkfun.com and doing it yourself is highly advised.

They know how long steps are as they have a small current draw from the powersource and this in turn keeps a clock going in whatevertime you want. The faster you want it to do it the more power draw. These parameters are set by the user if doing it yourself or by the manufacturer where you bought it.

Tip: If you can assemble Ikea furniture or any others you can pic up on assembly language, I promise you that. Also there is a program that comes with it that allows for a flow chart designated by you, just drag and drop(boxes etc with commands), and then it will convert it to assembly language which is cheating(it gets you there faster).

I would recommend you do it yourself and you can call yourself a computer engineer without the paper.:nana:

Then again if you just want to slap stuff together you can order them with specs that are around what you want and are going to have to pay a premium to have a specialized chip. If you order a one of you are going to pay a lot but if you order in bulk then it will be cost effective and I'm sure you could sell a few on here to make you money back or break even. This will make a few on here happy and you as well.

Also if you ask alot of the custom builders on here in the custom lights/modding section, I'm sure they will be happy to help direct you where there are good sources or stores to get what you want.

Also check deal extreme as they have drivers/boards that are cheap and have basic configurations. You just might settle there with great prices but you are going to have to wait a while as they take forever to ship stuff.

Hope this helps.
 
@ama230- I'm a self-proclaimed AVR expert, I can do basically anything with those chips :D

@acezone- I think this could be correct, that was one of my theories, I just wanted to make sure. I could run the micro at 128khz and use the CKDIV8 bit, that should result in minimal power draw.
Another one of my theories is that they use a capacitor/resistor combo, where the cap is charged up and then slowly bled out during power off by a resistor. On startup, the micro checks if the capacitor is a logical high-if yes, the off time was short. The problem is I have no idea how these micros behave without power (how the pins' resistances change, that is). I can store the previous mode in the EEPROM I suppose.

And as for >They know how long steps are as they have a small current draw from the powersource and this in turn keeps a clock going in whatevertime you want.
this is wrong for my project, as the power source is interrupted when you click the switch. Only a select few lights (like the nitecore D10 IIRC) actually maintain power to the controller when you turn them off.

I have a spare Attiny45 lying around that I will experiment with later, I'll try with the capacitor/resistor timer setup. I can't use a DX board as this project is with a sensitive laser diode and I need to use a specialized driver. And if any mods are looking can you move this to the custom lights/modding section please? Thanks!
 
Wyager.

The simple way is to set the real time clock (watchdog timer on the AVR) for the desired period, set a flag and go to sleep. When the timer goes off, it clears the flag at interrupt level. The next time through your service loop, if the flag is set, it has been less than the period of time. If the flag is cleared, it has been longer.

There are many variations on the theme depending on what else your code needs to do and what other events need to be timed. If you have several timers going, just add another timer for your flag - the timer being non-zero can be your flag.

Henry.
 
Alright-but how do I provide power for the timer to operate? I still need a minimum of ~3 seconds of power to do a measurement. Running the WD timer shouldn't take a ton of energy, but I'd still need a few hundred µA at 5v, right? And BTW, the resistor across capacitor method does not work-something about the way the pins work messes it up. Also, I managed to kill/disable reset on my attiny45 somehow D: so I'm testing all my code on the tiny2313 for now.
 
Remember farads = coulombs per volt, and amperes = coulombs per second. If you want to actually run the processor on a capacitor and it needs 100 µA for 3 seconds (it may actually need less than that), that's 300 µCoulombs, and if the dc-dc converter is making 3.7v and the processor can run on 2.7v then you can stand one volt of drop, so that's 300 µF, a fairly big capacitor for such a tiny board. So, your idea of just having an RC delay checked by one of the a/d inputs at boot time sounds more sensible. That is for a Fenix-style light where the micro is not powered when the light is turned off. If the micro is always running then you can just use a timer as HDS suggests.

Microprocessors don't have to use much power. Think of digital watches running for years on end with just a coin cell.
 
I can't seem to get the R/C circuit and ADC to work for some reason :thinking: it always ends up falling down to a value close to the maximum reading. I might have to mess with it a bit. And watches are pretty incredible at power saving, even if they only run at 30-some khz.

Edit: It turns out the micro pins basically lose resistance when I turn it off, so I'm now discharging the cap by putting it in series with a resistor. The problem now is that it takes a while to charge up. I'm wondering how fenix does this, it would be a million times easier if the micro had a constant power supply lol.
 
Last edited:
Yes, the RC time delay comes from using a resistor to discharge the capacitor. Remember I=V/R from Ohm's law, and I = -C dv/dt when you discharge a capacitor, so RC dv/dt = V

=> -RC/V dv = dt

=> integrating both sides, -RC log V = T

=> exponentiate both sides and rearrange => V = exp(-T/RC)

So the voltage across the capacitor will be a decaying exponential with time constant controlled by RC. You want to choose R and C to make the turn-off period easy to notice with the a/d and C large enough that drain from the a/d's input impedance won't swamp the RC constant.
 
:duh2:That math is way beyond me right now-I don't even know what integrating means yet LOL. They refuse to teach me calculus for a while. I'm pretty tired and that is all just blowing over my head (it probably wouldn't matter if I was wide awake :p) and ideally I wouldn't even need to use an ADC, I would just check if the capacitor was a logical high (above either 1.8 or 2.4v depending on the chip IIRC) to determine off time. But anyway, I'm not sure what you mean I should do with the resistor/cap values... and I think the impedance of the pins drops to 0 when off, because the cap discharges really fast when the micro is off unless I stick a resistor between the micro and cap.
 
Wyager,

You can charge your RC in a few microseconds if you put a diode in parallel with the isolation resistor (the resistor between the pin and the RC circuit). In this case the R and C for the RC are in parallel and connected between the isolation resistor and ground.

Let the pin (input) determine the 1 or 0. Set the pin to output to charge the RC. Read the pin (input) during boot initialization to figure out what you have.

You can save data between power fails in parameter RAM. Although PRAM has a limited number of writes, this will not be a significant consideration in normal use.

PaulR,

An ATTiny24A pulls roughly 5uA when sleeping with the watchdog timer running. Roughly 50uJ energy will be consumed in 3 seconds. You can safely run it down to 1.8V - but figuring a 3V start and a 2V end will net roughly 5uJ per uF - so 10uF is the lower end and 22uF would provide better margin for a lower starting voltage.

As it has been clarified that this is a power cycled switching system, I agree that the RC is the smaller and simpler implementation.

Henry.
 
Alright, so I'm thinking something like this... Is this correct?
Picture6.png

problem is, I have no idea what to use as the values for each resistor or capacitor to get a decent timing scenario (I just guessed in the picture), especially with the conductivity of the powerless AVR thrown in there. Keep in mind the voltage used will be between 4.2 and 3.5 volts (probably never below 3.6). Thanks for all the help guys, I would be totally lost without your help LOL.

PS: not half bad for my first time using eagle, no? Those little buttons are hard to find, and I couldn't find one that placed a ground symbol lol. Also, I wasn't aware that AVRs had PRAM... I was planning on using the EEPROM.
 
wyager,

A good first schematic. By convention, ground is usually in the down direction.

A good starting point would be 10uF for C1 and 100k ohms for R2 for roughly 1 second. Depending on the gate threshold and stuff - and your desired time, you might need to increase R2 50% to 100%. And you might also end up decreasing R1 slightly to take advantage of the grounding affect of the pin when the chip is not powered and improve noise immunity - if needed. Testing will quickly show what is needed.

Henry.
 
Again - you can use a pic which uses very low power and the term you are looking for is volatile and non-volatile logic. In terms of what you are looking for is a NVM(non volatile memory), this is when the microprocessor still retains the logic you want. Also this is just a small chip that will do anything under the sun for about 5 bucks with you can use a serial cable or usb if you want to get fancy and change it anytime you want.

And for paul, you are crazy smart as that diff eq is way over 99.9999% on here.

You do not need all these components listed and it does not need to be this complicated unless you are protecting a design or want nobody to understand it, which is good for protecting IP(your ideas).

Look into using a non volatile microcontroller and you will be set. As for using simple 777timer would also be good using j-k flippys, then again you need to understand this stuff a little. Try youtube or something...

Also you are going to have to break apart a light with wanted specs as the board you want is going to cost alot more for a one of, then again you could order a 1000 to actually get them cheap. You are looking to pay at least 20 bucks for one board with adequate specs, emitter 5$ and materials $5. So you are going to at least be 30 in the whole with just the engine.
 
Alright thanks a ton henry, I'll try some stuff tomorrow.

@ama
I don't like pic very much for personal reasons, and IMO they are more power hungry than the Picopower AVRs I'm using. And agreed, those are some crazy equations. I am now busy learning calculus online thanks to paul XD
And I'm not trying to design some board to sell lol, this is just for a personal project. So no concerns about keeping this secret. And I already have everything including the driver circuit, I only need something to provide it PWM (I spent the last hour painstakingly moving the driver board to make room for a micro :D)
 
Wyager, don't worry about the integral. I just wanted to show how to reach the conclusion which is well known in electronics, V = exp(-T/RC). There, V is the voltage (as a fraction of the initial voltage), T is the time in seconds, R is resitance in ohms, and C is capacitance in farads. The "time constant" RC is the amount of time for the voltage in the cap to reach exp(-1) = 36.7% of the initial voltage. That should be enough to drop from a logical 1 to a logical 0. So if you want RC to be 0.5 seconds, you might choose R=100K and C=5 microfarads. Henry would know better than me whether those numbers are reasonable given the likely impedance of the microprocessor input pins and how fast you can read the voltage. Your R=2M, C=4.7uF examples will give an almost 10 second RC constant which is probably way too much for a flashlight.

I think I would use AVR rather than PIC because there is a good GCC compiler for the AVR, it has more registers and memory, etc. But is there one with EEPROM and a small package? Are EEPROM and PRAM the same thing?
 
Last edited:
Wyager, don't worry about the integral. I just wanted to show how to reach the conclusion which is well known in electronics, V = exp(-T/RC). There, V is the voltage (as a fraction of the initial voltage), T is the time in seconds, R is resitance in ohms, and C is capacitance in farads. The "time constant" RC is the amount of time for the voltage in the cap to reach exp(-1) = 36.7% of the initial voltage. That should be enough to drop from a logical 1 to a logical 0. So if you want RC to be 0.5 seconds, you might choose R=100K and C=5 microfarads. Henry would know better than me whether those numbers are reasonable given the likely impedance of the microprocessor input pins and how fast you can read the voltage. Your R=2M, C=4.7uF examples will give an almost 10 second RC constant which is probably way too much for a flashlight.

I think I would use AVR rather than PIC because there is a good GCC compiler for the AVR, it has more registers and memory, etc. But is there one with EEPROM and a small package? Are EEPROM and PRAM the same thing?
I'm a bit confused as to the exp() thing. What does that mean? But otherwise that makes sense, and I could probably treat the micro as a parallel resistor when discharging the cap. And why do you guys say that the micros have impedance instead of resistance?

And yes, Attinys have EEPROMs, plenty for this application. And I'm pretty sure PRAM is different than EEPROM in a few ways. Depending on which kind of PRAM you're talking about, it's either phase-change memory which uses chalcogenide glass to store data or Parameter RAM, which is found only in macs IIRC.
 

Latest posts

Top