LVP issues in biscotti and babka

Hi all,

I am new here.

Recently I bought some Convoy S2+ flashlights and of course couldn’t resist trying to upgrade firmware on the NANJG drivers. Many thanks to ToyKeeper and gchart for their work on the excellent biscotti and babka firmware. The code is easy to read, does a lot for its size, and works great.

One issue that I noticed with the new firmware is that LVP kicks in too late — around 2.85V or so (as measured with a bench power supply). No problem I thought, I would just calibrate my ADC_XX values using ToyKeeper/battcheck. I did that, but LVP still kicked in at around 2.85V vs the expected 3.0V. So I went ahead and changed ADC_LOW value from ADC_30 to ADC_32 (3.2V). This made a very small difference — LVP now activated at around 2.90V or so.

It appears that there is a flaw in how LVP is implemented. The threshold for triggering LVP does not correlate well with values produced by battcheck. I think this happens for two reasons:

1) battcheck measures the average of 8 values, while LVP relies on the minimum of 8 consecutive values (a single value above ADC_LOW aborts LVP activation)
2) More importantly, battcheck runs with the LED off, while LVP triggers with the light running, possibly with PWM.

PWM wreaks havoc on voltage measurements. Because of parasitic induction in the flashlight (e.g. in the springs, but also in traces on the board, wires, vias, etc), when current is pulsed through the LED, the voltage on the board rises and falls below the average. My measurement with an oscilloscope shows a 2.5V - 3.5V range for a 3.0V input. Because LVP effectively measures the maximum of 8 readings, it captures the PWM-induced voltage peaks. Moreover, since the amount of LED current depends heavily on voltage, especially below 3V, peak voltage increases non-linearly with input voltage. This makes LVP difficult to tweak.

I think there’s got to be a better way to implement LVP, and I would like to get ToyKeeper’s and gchart’s thoughts on this. One proposal would be to measure voltage multiple times in a row and then discard the outliers, averaging the remaining values. Another would be to compute the minimum and maximum values of a series of measurements and take the average of the two.

One issue is that biscotti in its current configuration already takes up 98% of space on the attiny13. babka on the other hand still has lots of space available.

I will try to make changes to babka in the next few days and report the results. As is, sub-3V LVP activation is detrimental to the longevity of Li-Ion batteries.

Apologies if this topic has already been discussed here.

Here is the oscilloscope trace of voltage jumping from 2.5V to 3.5V for a 3V input + PWM. Current here is around 100 mA.

Thank you for the heads-up! In my own firmware I don’t have any filtering or averaging which worked fine in my test setup (using a bench power supply) and I didn’t think it might fail so hard. No solution for your issue, of course. But your post contains very helpful information.