Some drivers have an Atmel microprocessor on board to control different modes. This microprocessor controls the number of modes, the brightness of the modes, the different flashing modes (strobe, SOS, etc.), mode memory, and low battery warnings. Atmel processors are programmable therefore the same driver can be loaded with different settings to customize the light with whatever features you want. This page will summarize some of the things you will need if you want to program your own drivers. This is based on a Budget Light Forum discussion thread where you can go for help or to get answers to your questions. This whole thing was started by a BLF member, Tido.

SOIC 8 clip attached to flashlight driver board for AVR ISP programming

All of this is based on AVR tools developed for Atmel chips, which is mostly open source (free). Wikipedia article. ISP means "In System Programming," in other words, the chip can be reprogrammed while still soldered in place.

Things You Will Need

Drivers

Of course you will need a flashlight driver circuit board first. The stock driver in some flashlights already has an Atmel driver on board. There are also several NANJG drivers available that include an Atmel TINY13A chip that can be programmed. These chips can also be locked down so they can't be changed, but so far none seem to be. However, some variants of NANJG drivers use a PIC chip that can not be programmed with AVR tools. See Popular drivers to see some of the drivers people have been using. Most of these use current regulators to limit the current delivered to the LED while the Atmel chip itself turns the power on and off very quickly, leaving it on for full power, or flashing on and off quickly to simulate a lower mode. As the percentage of off time increases, the light gets dimmer. By setting the on-off cycles to a high frequency, the eye just sees this as a lower mode even when the LED is barely lit. This is called Pulse Width Modulation (PWM). By slowing the flashing more, you get a visible strobe mode or SOS modes. So essentially the Atmel chip is just a computer-controlled on-off switch.

The Atmel ATTiny13A has an 8-bit processor with 1 kb of flash memory, 64 bits of RAM, and 64 bits of EEPROM that can be written several thousand times. There are also settings called "fuses" which can be set or cleared.

Hardware

You will need two pieces of hardware: an AVR ISP programmer that attaches to your computer's USB port and a clip to attach to the Atmel chip which is already soldered in place.

 
USB ISP programmer

There are a number of ISP programmers available, including one called USBTinyISP that you can build yourself from a kit. Ironically, it uses an Atmel chip as well which must be programmed before it can be used as a programmer. There are also a number of cheap drivers available on eBay if you search for USBASP. These all look similar with an exposed blue circuit board and cost about $12, usually including a ribbon cable, which you probably won't need.


 
SOIC 8 clip with wires rearranged

The other piece of hardware is a SOIC 8 clip to attach to the pins of the Atmel chip on the flashlight driver. There are cheap ones on eBay that have been problematic, but will generally work. They cost about $12 as well. It is best to get one with a ribbon cable that attaches to pins coming out of the clip. The individual wires of the ribbon cable can be moved to the correct pins for the ISP programmer. See below for the proper configuration.

Software

You need some kind of software to interface with the USB programmer and read or load programs into the Atmel chip.

You will at least need AVRDUDE (AVR Downloader UploaDEr) to upload and download files to the Atmel chip. It is a command line interface (DOS). If you use Tido's programs then you don't need to compile anything: those programs are ready to be loaded.

If you want to customize the programs or write your own, you will need a compiler. The programs can be written in assembly language or C. Assembly can give more compact programs, but C is easier to write and understand, so that is what is being used so far. AVR Studio is available free from Atmel to allow you to write and compile programs and load them. WinAVR is a free collection of all the software you need as well as a graphical user interface (GUI) to edit, compile, and load programs. Eclipse is another free GUI that can be used to develop programming and is what Tido uses.

BLF-VLD

Tido created the original files that can be used, called the BLF-VLD (Budget Light Forum Versatile LED Driver). If you go to the first entry of the thread How To Build a Flashlight With Perfect Modes, you will find a link to the latest zip archive of the files you need. This zip archive includes a helpful readme file.

Without writing any code, you can choose from three setups that Tido has already written:

Simple is a 3-mode setup with Low, Medium, and High. It includes mode memory (if light is on for 2 seconds or more, the mode is memorized when the light is turned off) and a low battery warning.

Extended has hidden modes that can be accessed by running through all of the modes quickly. By default you run 3 modes, but can reach other modes if you want.

Programmable allows you to assign any of the extended modes to your 3 simple modes.

Getting Ready

Installing software

Installing software on your PC and setting it up to get the USB programmer working. Put files in the correct locations so you can access them.

Follow instructions here for installing WinAVR and Eclipse. Eclipse is optional. Then configure Eclipse for AVR. There is also a Wiki about Eclipse AVR.

Set up your clip

Match the pins of the Atmel chip with the pins of the USB programmer. Only pins 1, 4, 5, 6, and 7 of the Atmel are used by the ISP programmer.

The pins of the ATMEL chip are

 
Pin numbers of Atmel ATTiny13. Pin 1 has an indented circle and is marked with a triangle.


ATMEL ATTiny13A pins
8 VCC 7 SCK 6 MISO 5 MOSI
1 RST 2 INP 3 INP 4 GND

The programmer is 10 pins however which are set up this way:

AVR Programmer ribbon wires
Pin Function Function Pin
1 MOSI VCC 2
3 NC GND 4
5 RST GND 6
7 SCK GND 8
9 MISO GND 10


 
The ribbon port at the end is disassembled and wire 8 is moved over to the 9th position

So Wire 1 (MOSI) of the ribbon cable from the programmer must attach to Pin 5 of the Atmel. Make sure you mark which side of the clip attaches to the top of the Atmel or everything will be misaligned next time you use it. Line up RST and SCK with their appropriate wires. But notice that MISO on the programmer is Wire number 9 and the ribbon only has 8 wires. You will need to disassemble the end of the ribbon cable at the controller end and move wire 8 over to the 9 position so that MISO lines up correctly.

Communication with the Atmel chip is via Serial Peripheral Interface (SPI, see Wikipedia article) with the ISP programmer a Master and the Atmel on the driver board a Slave. SCK is Serial Clock so they are in sync. The ISP programmer talks on the MOSI line (Master Ouput, Slave Input) while the Atmel talks back on the MISO (Master Input, Slave Output) line. RST is reset.

Test the connection

Now everything is installed and set up. You should be able to plug in your USB programmer with the cable leading to the clip. Attach the clip to the Atmel chip right-side up with at least the 4 critical pins wired correctly.

Now open a command window in Windows (or whatever you are in) and typing this command at the C: prompt:

 avrdude -p t13 -c usbasp -n


The command above calls up avrdude. "-p t13" means you are talking to an Atmel ATTiny13A chip. "-c usbasp" says you are using the USBASP controller. "-n" means no writing, since we're not actually doing anything yet. With USB, you shouldn't have to enter the port number, but some people have to go find out what port the USB is assigned to and if it is Port 2, they add "-P com2" after usbasp. This should generate a message that the AVR device is initialized and ready to accept instructions. If the connection is not working, you will get something like


 avrdude: warning: cannot set sck period. please check for usbasp firmware update
 .
 avrdude: error: programm enable: target doesn't answer. 1
 avrdude: initialization failed, rc=-1
        Double check connections and try again, or use -F to override
        this check.
 avrdude done.  Thank you.


So if you get that, check your clip wiring and make sure your clip is getting a good connection. The clips are very finicky and sometimes needs some repairs. You can check continuity with a DMM from the tooth back to the pad on the USB board. Once you get the message that everything is working, you can move on to the next section.

Download settings

Use the software and hardware to download the existing program from the Atmel chip on the flashlight driver. You will need this if your programming doesn't work and you want to reset the driver to how it was originally. Get fuse settings too.

avrdude -pt13 -c usbasp -u -Uflash:r:flash-dump.hex:i -Ueeprom:r:eeprom-dump.hex:i -Ulfuse:r:lfuse-dump.hex:i -Uhfuse:r:hfuse-dump.hex:i

The command above will create 4 files on your hard drive for the flash memory contents, eeprom, high fuses, and low fuses.

Upload new settings

Open a command window in one of the BLF-VLD subdirectories: "Fixed Modes", "Programmable", or "Simple". With everything hooked up, enter this command:

avrdude -p t13 -c usbasp -u -Uflash:w:BLF-VLD.hex:a -Ueeprom:w:BLF-VLD.eep:a -Ulfuse:w:0x79:m -Uhfuse:w:0xef:m

Compile your own code

If you use the standard BLF-VLD files, you don't need to compile anything. But if you write your own code or modify the BLF-VLD files, you will need to compile your own files.

Other Issues

Mode Memory Some flashlights will remember the last mode they were in. Often the mode will be remembered after the light is off for a couple of seconds (if it was too short an interval you couldn't change modes by half-pressing the tail clicky to off briefly). But NANJG drivers usually have memory that sets after the light is on for a couple of seconds. When you turn the light off, it will come back on in the last mode, but if the light was on for less than 2 seconds, it will come back on in the next mode. This works pretty well except that if the light is on for a couple of seconds and you want to change modes, you will have to half-press the clicky once and end up in the same memorized mode, and then again to change the mode. The reason for memory to be this way is that when power is cut to the driver, it can't write to its memory. But it is easy for the memory to be written while the light is on and a timer can be used to write to the memory after the light is on for a couple of seconds. In order to get conventional memory, a capacitor would have to be added to hold on to power long enough after the power is turned off for the driver to write to its memory.

Low Battery The Atmel chip can monitor voltage through its VCC pin, however, the voltage can't be more than 1V. Therefore the battery voltage being directed to the VCC pin must go through a couple of resistors to come down to a readable level. Then the software can have some calibrated values so that the user is warned of a low battery by the brightness dropping or the light flashing periodically.