luxdrv - custom modes driver firmware with ramping

This is about programming a driver yourself with all the modes you want; just like Tido's thread "How To Build a Flashlight With Perfect Modes (picture heavy)". It got quite lengthy, so I started a new one for my driver program.

I finally found the time to program my own driver. After playing with the three pocket XM-L lights in the last week I found it good enough to publish it here.

It has less features than Tido's, so it leaves room for improvements (meaning that there's indeed quite some free space left to do so). It doesn't have any battery monitoring yet.

It implements sixty545's UI(post #254), which after some testing I found very nice. Gives you lots of modes without the need to cycle through all of them when switching between the favourites. Leaving the light on for only a short time (<2s) and the switching it off again will bring you to the next mode when you switch it on again. Leaving it on for longer will memorize this mode for the next time; but if you change modes again it will start from the first mode in the mode order. If you put your favourite modes to the front, this is a quite nice UI I think.

Current modes are moon-medium-high- 6 levels of brightness -strobe-beacon-timer.

As a special mode I added a 5-minute timer - partly for fun, but I do need a 5-minute timer relatively often.

One of the PROs of this driver is the wide EEPROM wear leveling; writes are spread over 32bytes, and only 1-3bytes are written each time the light is switched on. A ramping mode might be feasible that way.

It is supposed for/tested on the NANJG 105C (2.8A) and NANJG 101-AK (1.05A+) drivers using AMC7135s and the ATtiny13A micro-controller.

Tido's thread mentions everything you need, so here's a short version:

  • Driver: NANJG 105C or NANJG 101-AK or similar
  • Programmer for flashing the program to the controller: there seem to be some available for 4$ on ebay (search for "avr programmer").
  • a clip for contacting the controller, search for "soic8 clip" at ebay
  • Software: I use AVRstudio4 with WinAVR

The driver program is just a C file; you could create a new project for the ATtiny13A using the wizard and then just copy&paste the contents into that projects C file.

Since I am very sensitive to PWM (I easily notice 2kHz PWM...), I meanwhile use a CPU frequency of 4.8MHz with a PWM frequency of 9.4kHz, difficult to notice even if I want to That means a low fuse of 0x75 (and I use a high fuse of 0xff) and the lowest usable PWM value is 5. Shorter PWM pulses are too short to activate the AMC7135s.

UPDATE: luxdrv 0.22 with ramping

I implemented ramping, have played with it for a while and found it nice, so here's the new version officially:

Standard modes are now (can be easily changed of course):
moon - medium - high - ramping - use selected level - strobe - beacon - timer
Tapping the switch while ramping will select that level. The ramp is actually 25 levels 'only', so you can see it stepping up&down. Brightness doubles about every 5 steps (the darkest levels are spaced a bit wider).

I also implemented sixty545's idea of skipping the 1st mode when starting over after already being in 1st mode. Also changed the wear leveling method; it writes even a bit less bytes now, but it does write 25 bytes per ramp, so you probably shouldn't use that for hours (~700 writes per EEPROM cell per hour).

UPDATE: luxdrv 0.30 - added battery monitoring

I implemented battery monitoring:

  • When the battery (under load) goes below ~3.0V, the light automatically steps down in brightness (halves the output); the reduced load usually gets the battery voltage above 3V again. When it falls below that threshold agan, output will be halved again - and so on.
  • Beacon mode has a battery indicator: Before actually starting beacon mode, a number of (low intensity) blinks indicates battery level (without load), ~12 blinks is full, ~5 blinks is nearly empty. It's about one blink for every 0.1V over 3.0V (cell voltage *without load*).
  • Note that those values are not accurate due to production variances in the microcontroller's internal voltage reference.

Update: luxdrv 0.30b - improved config, const progmem, no dummy any more, timer removed, 4 main modes as default

Driver download links:
luxdrv 0.1

luxdrv 0.22 with ramping
luxdrv 0.30b, added battery monitoring

Dear DrJones,

Many thanks for your sharing of your codes. I have just downloaded your source code and will try it out later. I am building a head lamp which requires temperature monitoring the LED. Being an uC dummy, I would like to ask you to put this feature in your to do list. I wish the driver will switch to the next lower light level when the temperature reach the threshold. No blinking as the driver which I modified the battery monitor to a temperature monitor.

Thanks in advance.

Microa

Thanks very much for sharing your valuable work. This adds so much more flexibility to the ubiquitous 7135-based drivers.

Schönen Dank!

Microa, please give a bit more info then, i.e. the rule for ramping down, e.g. like "if voltage >0.7v, go down a level", and I could try.

Then again, you could modify Tido's firmware to get rid of the blinking:

Find the lines (line number 555 for me):

while(1){
// give a short blink every five seconds
PWM_LVL = pwm;
_delay_ms(5000);
// PWM_LVL = 0; <---
// _delay_ms(100); <---
#ifdef LOWBAT_RAMPDOWN

and prepend "//" to the marked lines as shown above.

DrJones, The NTC resistor is 10K at 25C and about 3.3K at 70C. A 5V of Vref is applied to the voltage devider. For setting at 130/255 of 1.1V of the uC voltage reference, the light level will go down if voltage of the NTC drops lower than 0.56V.

Thanks DrJones, I will try to modify the code to get rid of the blinking.

It works.Laughing

I modified the code and it works. The driver ramps down the light level without blinking.

Thanks a lot.

Thanks for this post! I wanted to do the same kind of post, but my code (created by using Tido's as a reference) is much simpler and doesn't have the best EEPROM wear leveling (as Tido's seemed to write the same byte on each turn-on anyways, IIRC, unless I read the code wrong). However, looking at yours, I do see you doing true wear leveling, and it is quite ingenious! (well, it took me a while to realize how you did it - had to freshen up on my bitwise understanding)

The best I could come up with was changing a byte from 0 to 1 when doing the short presses while working through the bytes (one-click: 10000, two: 11000, three: 111000, etc.), and then clearing them all back to 0 when the current mode is saved. Not exactly the best, but better than wearing out the same byte on every short press. Yours is much better though, and using the 0x80 bit as the short-press indicator is pretty awesome - one of those "why didn't I think of that?!" moments. I'll have to find a way to incorporate your logic into my UI setups.

Yes, my driver on average does one erase/write cycle per cell and click. Wear levelling is only done on cells that may see more than one write when the light is on (click type detection and mode change sequences in newer versions). I tried using wear levelling the way DrJones does it, but it turned out to eat too much EEPROM and program flash space if I wanted to fit all the functionality I have now into the BLF-VLD. It is the same thing with cramming independent state information into one byte using bit operations. Sure, you have one less variable sitting in the EEPROM, but you pay for it in program flash because you need more instructions to access and store the information.

Anyway, the ATtiny's EEPROM is rated for a guaranteed durability of 100000 erase/write cycles. Even at a hundred clicks per day, it would last almost three years. And this is only the guaranteed minimum, the real durability is probably five to ten times that.

BTW, writing a zero to a cell is not just erasing it, it is a full erase/write cycle. If you just want to erase it, you can set EEPM1=0 and EEPM0=1 in EECR, but the erased cell will then read 0xff. To only write without erasing, you set EEPM1=1 and EEPM0=0. You can write multiple times to a cell without erasing it first, but you can only write zeroes (e.g. you can overwrite 0xff with 0xfe, but not the other way around). This will be less stressful for the cell, as it is the erasing that causes the wear, but you'll have to invert your program logic.

Thanks for sharing your code DrJones and thanks again Tido for planting the seed that made this all possible. Hopefully I will be able to share my dream program with everyone here soon.

Thanks for that info Tido!

DrJones,

Thanks for posting your program. I flashed a generic 3x7135 board last night with your program and it worked like a charm. Using the highest PWM frequency you have listed and a Q5 XR-E I was able to get light using a PWM setting of 1. The next driver I'll flash is an 8x7135 NANJG 105C to be used with an XM-L in a Trustfire T2, assuming everything fits properly.

Thanks again for writing this program,

Kris

Interesting. Maybe those 0.4 µs pulses are not sufficient for 8 AMCs, but for 3...

What fuses did you use?

To be honest, this was my first time doing any Atmel programming so I had just installed a fresh copy of AVR Studio 5 and didn't change any fuse settings. The chip on the board is a low voltage version (1.8V min instead of 2.7), an Attiny13V.

Perhaps with the stock fuse settings it defaults to a slower frequency? I'll gladly check the settings when I get home tonight if you'd let me know where I should check.

Kris

Yes, with stock settings you should get 2.35 kHz PWM frequency.

I think the timing should be off in your setting then; it was compiled for 4.8MHz CPU frequency, but it runs on 1.2MHz. How does strobe look? How long does the beacon take between two flashes? How long does the 5-min timer run?

Edit... Then again, I don't know what stock settings the driver has... the above mentioned are the stock settings for new bare ATtiny13s.

I just looked at the code again and noticed that you had the values for the low fuses listed after the frequencies. I'll try to find where to change the setting and make it 0x75 to run at 4.8 MHz, then try the tests you mentioned.

EDIT: I'm installing AVR Studio 5 on my work computer as I type this. I should be able to find where the fuses are set once it is installed.

Kris

I run mine at 4.8 as well, and set the fuses this way...

avrdude -c usbtiny -p t13 -Ulfuse:w:0x79:m -Uhfuse:w:0xed:m

Not sure if I got all of the fuses right, but it works :)

It turned out that my stock frequency was actually 9.6 MHz. I changed it to 4.8 MHz using the GUI in AVR Studio. This set the fuses to H=0xFB, L=0x71. I'm still able to get light at a PWM of 1, but it is quite dim. I can look directly at the emitter through an 8 degree optic.

I was able to test the other modes as well. The beacon is 10 seconds, the timer works out to a little over 5 minutes, and the PWM frequency seems to be ~8.5 KHz if the PWM is set to 1-31, and ~17 KHz if set to 32-254.

My multimeter doesn't like the strobe, so I can't give an absolute value for that. It did look pretty good though. If the rest of the timings are on then it must be pretty close as well.

If there's anything else you'd like tested I'll give it a shot.

Kris

I am curious if there have been lights whose EEPROMs have worn out, hrm..

C