How to convert Nanjg 105c to "off-time" memory?

So, judging from what i read here, most of us like “off-time” memory better than “on-time” memory
(the latter meaning that a mode is memorized after it has been on for at least a specified amount of time, while the former means there is a mode change when the flashlight has been off shorter than a certain threshold time).

The Nanjg 105c implements the unloved on-time memory, and driver programmers (as in, programming people, not the devices) mention that there need to be hardware changes made to implement off-time memory. Those changes are usually very unspecific like “you need to add a diode, capacitor and resistor”. Now, i think i now what to do with those items. The capacitor will power the microcontroller while the battery is disconnected, the diode prevents the led from being powered by the capacitor (which would drain it way too fast), and the resistor either limits the current through the capacitor at turn-on, or acts as a pull-down for the input pin that will detect the switch-off state while powered from the capacitor.

But then i looked at the Nanjg 105c pcb, and it already features a capacitor and a diode that seem to be connected just as needed for off-time memory. Maybe the capacitor value is too low for that purpose, i guess it is there just as an input filter. But it seems to me that it would suffice to change that capacitor to a higher value.

The current draw of the driver, when not powering the led, seems to be 0.3mA or less. The Atmel is specified to work down to 1.8V, and i measured the capacitor at 11uF. So from, lets say 4V down to 1.8V at 0.3mA it will last for
11uF*(4V-1.8V)/0.3mA = 0.08s. So why not just exchange that one for a 100uF one?

Ok, the Atmel still has to know when it is capacitor-powered. But it has low-voltage detection, which could be used for that.

So, to those who have actually done it, how did you change your Nanjg to off-time memory? And do you have a software driver for that?

I’m no expert either, but I’m pretty sure the added capacitor won’t need to power the whole microcontroller - just one pin. Then when turned off, that cap then will bleed its charge through a resistor.

Then the software can be programmed to “look” at that pin when turned on and decide whether the mode need to be moved or maintained. Cap still charged = short click = change mode, no charge = long off = same mode.

This is a good thread BTW, hopefully we can get specifics on doing exactly this. I know sixty545 has already explained it, and Tido’s code can be used, but that “perfect mode” thread is already too long IMO and information is too far spread.

Interesting… i did not think of that way of knowing the off-time. That explains why there is a need for the extra resistor.
Still no explanation for the diode :slight_smile: (i guess it was the mentioned diode that led me to think about powering the atmel while the switch is off).
So when the microcontroller is switched on, it first uses that pin for input, measures the voltage to know if it has been off for long, then changes it to output to keep the capacitor charged for the next off-cycle.
Looks quite elegant to me.

Both versions do have one “problem”: the threshold time will change with varying battery voltage. But with your version, you could make up for that in software, by comparing the capacitor voltage not to an internal reference, but to the battery voltage.
But i guess the time difference will hardly be noticeable.

I didn’t know that Tido’s code implements the software side of off-time memory. Those driver-software threads really are long…
I just got into re-flashing the Nanjg 105c driver, so far i only tried DrJones’ luxdrv 0.3, which in a source code comment also mentiones the “adding R,C,Diode” idea as a “TODO”. So no usable code there.

I was just looking if there were some instructions on how to add off time memory. Has anybody out there done it and how?

The diode that is already present on the NANJG is simply to prevent reverse voltage from destroying the microcontroller. The cap is a standard decoupling cap…

To add Off-Time mode memory, you’d have to do…

So out of an unused micro pin and into a forward conducting diode AND a 1 meg resistor, and then both of those connected to a 1uF cap which is connected to ground.

The software from there is easy…

On power up, set that pin to input and see if it’s high. If high, it’s a mode change, if not, it’s been a long power off… After checking, set pin to output high to charge the cap back up for next time.

Changing the value of the resistor will change the delay (how long it has to be off to not be a mode change).


The diode allows the cap to be charged up very quickly (current flows out of the micro pin into the cap through the diode)
but then bleed off very slowly (current flows out of the cap into the micro through the very high value resistor)

You don’t actually ‘measure the voltage’, you use the input as a digital input (either high or low). The ATTINY13A high/low states are PERCENTAGES of VDD, so the difference is extremely small in the timing with reference to different battery voltages.

The ATTINY13A senses a high at anything above about 50% of VDD and a low at anything below about 50% - so it’s all relative. The cap will be charged to (Battery Voltage - Diode Drop) Extremely quickly, it will be further charged to Battery voltage more slowly (through the resistor). When power is removed, it will start bleeding off through the resistor. No matter where it starts, 50% of that will come in roughly the same amount of time…


How does an atmel pin behave when the atmel is off? The capacitor loses its charge no matter what, but the way you describe it, it seems it discharges through the atmel (using R for setting the time constant).
I had a circuit in mind where R is parallel to C for discharging (and no diode), but your circuit reminded me that maybe the atmel draws too much current when the pin is input, so that the time window to actually measure the capacitor as LOW or HIGH might be too small.

Generally, if you have a voltage on an input of a CMOS chip and the chip has no power applied, the chip winds up trying to power itself (rather poorly) from the input pins via the protection diodes on those pins. Many a lucky engineer has spent some quality time trying to figure out why their circuit is behaving rather strangely…

Right at each output pin are two diodes for protection. When VDD falls to zero (battery disconnected), there is then a current path into the pin, through the protection diode to VDD. This allows the cap to pass current and supply power for the ATTINY to continue running. This bleeds the cap dry in no time flat.


Edit: Texas beat me to the answer - but my picture is way cooler than his answer :slight_smile: lol

I read this somewhere and this is how i implemented the memory and mode change:

From power on:

- read memorized mode from flash

- store next mode to flash

- if within two secs you click the switch you will be in the next mode

  • if after two secs you do not click you store the previous memory back to flash.

Diode and cap are not related for this to work.

Works on my zilog nangj style ptototype posted here.

Yes, that’s on-time memory. This thread is about off-time memory.

Ok. I thought that is what he meant by off time memory. To change mode when off shofter than a certain time.

Going back here, anybody can explain the behavior of an Off Time memory?


Standard Mode Memory

Power light on. Mode x.

Power off.

Power on. If OFF for less than y seconds (y depends on RC constant), Mode x+1. If OFF for more than y seconds, Mode x.

Start At Mode 1 Memory

Power light on. Mode x.

Power off.

Power on. If OFF for less than y seconds, Mode x+1. If OFF for more than y seconds, Mode 1.

**Notice that in both cases, ON time has no bearing.


Thanks PPtk!
Got an idea on how to implement this on my zilog prototype.

Power On:

  • if short tap ==0x45 mode
    else mode (or mode 1)

Short Tap/LVD interrupt:

- short tap = 0x45

  • after 500mS short tap = 0xBA

I’ll check this out on Monday.

Are there any linear drivers like* Nanjg 105c that have off-time memory?

*can be reprogramed, and can keep constant current just as good

There might be others.. But there will be this one soon..

Yes a very promising driver indeed.