How To Build a Flashlight With Perfect Modes (picture heavy)

Welcome to BLF DrJones. I wonder, is this the same DrJones of DX forum fame? :)


Hi and welcome DrJones.

Good to have another active programmer here.

I have been reading this forum since almost two months ago and I like the fact it is for BUDGET lights. I like budget flashlights because I like to play with them replacing leds and drivers, something I won't do with expensive flashlights. The expensive flashlights I have are collecting dust just because I almost don't use them.

Considering some people here is wanting to use PIC microcontrollers I am posting this information I posted some time ago in another flashlight forum.

I have written and tested this PICAXE source code to implement multi-level functionality in DX SKU 15880 or any driver using Microchip PIC12F629 microcontroller.

The test was done using a regular LED instead in the actual driver. It must work in the actual driver.

You need to replace the stock microcontroller with a PICAXE-08M Surface Mount AXE007MSM device. You will also need to do some pin swapping and lifting since PICAXE-08M uses pin 5 to do PWM and DX SKU 15880 uses pin 7.

I used PICAXE mainly because there is no necesity for a programmer to burn the program into the device. A computer with a serial port (or USB to RS232 converter) and 2 resistors is all needed to program the device.

For more PICAXE information visit PICAXE's site.

This is the program's source code:

'LED PWM Driver software.
'Writen by Juan C Galvez

' the following EEPROM entry contains the desired level percentages
' The first entry contains the level position to use the next time the driver is powered.
' Next are the desired level percentages.
' The last position next to the last desired level must be zero. It is used as end indicator
' Example EEPROM entries:

' 1, 1, 50, 100, 0 means 1%, 50%, 100%
' 1, 10, 100, 0 means 10%, 100%
' 1, 10, 30, 50, 100, 0 means 10%, 30%, 50%, 100%

' No SOS nor STROBE implemented.

EEPROM 0,(1, 5, 50, 100, 0 )

symbol LEVEL_SHIFT_PERIOD = 3000

' b0 keeps the current level positi b0 = b0 & $0F
' bit7 If 0 then keep the current level. If 1 then go to next level. Note: bit7 if the MSB in field b0
' b1 Current level value (%)

read 0,b0 ' Read next level position

if bit7 = 0 then ' If bit 7 not set the keep the current level.
read b0, b1 ' Read current level value
else
b0 = b0 & $0F ' Turn bit 7 off before reading position value
do
b0 = b0 + 1 ' Go to next level
read b0, b1 ' Read next level value
if b1 = 0 then ' If next level value is zero then start over.
b0 = 0 ' Reset position
end if
loop while b1 = 0 ' Keep until a valid position has been
end if

bit7 = 1 ' Turn bit 7 on. This means that the next time the driver must go to the next level
write 0, b0 ' Write the value to EEPROM position 0

w1 = b1 * 4 ' Multiply level value times 4
pwmout 2 , 99, w1 ' Turn the LED using the current/calculated level

pause LEVEL_SHIFT_PERIOD ' Wait n seconds

bit7 = 0 ' Turn bit7 off so the next time the driver is powered it will keep the current level
write 0, b0 ' Write current level position to EEPROM

b0 = 1 ' Enter an infinite loop
do
pause 2000 'Used to allow firmware upgrade while running.
loop while b0 = 1


As written, the code implements 3 brightness levels 5%, 50% and 100% but that can be easely changed. Also, the level change occurs if the driver is powered for 3 seconds; I mean, if you turn the driver on and keep is for at least 3 seconds then the next time you turn the driver again it will start in the last used time. I you turn the driver on and turn it off before 3 seconds then the driver will use the next level the next time it is turned on. To change this 3 seconds just modify the line containing:

symbol LEVEL_SHIFT_PERIOD = 3000

with the milliseconds you want to use.

I hope this code is useful to some one.

Juan C.

Fantastic info, Linterno...and welcome to BLF!

Aloha and welcome to BLF DrJones and linterno!

PICAXE is interesting and somewhat strange... From the specs it seems that the flash memory only contains the PICAXE interpreter while the user program is loaded and stored in the EEPROM (probably tokenized) and interpreted by the (flashed) interpreter. I have seen something like that in an ATtiny2313-based educational set.

Edit: Yes, I'm the DrJones at DX :) Hello all :)

Glad to have you here DrJones... The info in your site has been very usefull for me

Welcome linterno to BLF! (I guess 'linterno' is spanish for 'lantern').

Very interesting with the picaxe, just a pity one have to unsolder and solder SMD multilegged chips.

Thank you all for giving me a welcome.

In fact in spanish is “Linterna” but that is female. Since I am a man I prefer to use “Linterno”

My plan for building that driver is to actually keep the µC running for a while after the user has switched the light off. I'm going to use a bigger capacitor parallel to the µC, so when the battery power is off, it runs from that. Some battery sense pin is required to check if the flashlight is on or off, and sleep with watchdog is engaged as soon as power goes down (via interrupt). This allows timing the 'off' taps for mode changes etc. Since programm logic will be simpler, it might even use up less memory and leave more space for fun ("Hey look, that light says my name in Morse code!" :) )

When I first thought about the the BLF-VLD, I wanted to keep the MCU running too, but I soon realised that this just wouldn't work with the standard PCBs available. For one, almost all ATtiny based PCBs have voltage dividers for battery monitoring, that will suck a buffer cap empty in a matter of milliseconds. Second, to actually detect the power-off, one would have to add a wire directly from the battery's positive end to an I/O pin, circumventing the protective diode. As a major design goal was to have a driver program that could be flashed into a stock PCB, that was the end of that idea ;)

AFAIK, most PIC based PCBs don't have the voltage divider problem, as the 12F629 doesn't have an ADC and therefore can't monitor the battery's voltage. But you'd still need to add the wire for detecting power loss and would probably have to upgrade the buffer cap.

Have noticed your posts on DX forums , they stand out and have been helpful just knowing that your advice is a step above the norm..

I also have to admit to having a thing for Mrs. jones ..

We gotta thing goin' on ...

BLF-VLD version 0.4 is available for download here. Special thanks to sixty545 for giving valuable feedback and spotting a few bugs. This release is not as thoroughly tested as I would like, but I do have a lot on my plate right now. Bug reports and suggestions are still welcome, but it might take some time before I get around to fixing anything.

Here is the section from the ChangeLog:

2011-01-10 Tido Klaassen <tido@4gh.eu>

* BLF-VLD: release v0.4

* driver.c: fixed initialisation of ADC

* driver.c: added support for mode switching based on an I/O pin's
state

* driver.c: fixed driver getting possibly stuck in extended mode if
compiled without extended mode support and EEPROM gets corrupted.

* driver.c: added compile time option for gradually lowering light
level in low battery situation

For those of you who are worried about the availability of drivers with ATtiny13 I am happy to say that I just got 4 pc. of the old AK-47 with ATtiny13 delivered from KD in just 9 days.

Tested one: It was AVR programable as expected.

And I ordered a single AMC7135*8+MCU from KD on 01/05 and got a Nanjg 105 (with ATtiny13A) 01/14 :)

Edit: Doh, wrong item number, sorry, edited.

Can you tell the sku no. please.

I got 105A (with PIC) in an order of SKU: S009742 dec.9 - dec.27

I got a 105A SKU: S009742 1/3/2011

following this thread with interest but way over my head

Excellent thread! In fact, I registered on this forum just so I could post here ;) I want to thank all of you for the information so far.

I am planning on buying the equpment needed in order to program ATtiny13 chips to use in a driver for a D cell Maglite runing (for now) an XP-G. Here is what I plan to order or have ordered:

Drivers:
DX 7135 - http://www.dealextreme.com/details.dx/sku.6190 (probably a crapshoot to see if I'll get a good one with the correct MCU)
SB 7135 - http://www.shiningbeam.com/servlet/the-133/3-dsh-Mode-Regulated-Circuit-Board/Detail (not sure about the solder bridge I see between pins, but I'll try it)

Programmer:
USBtinyISP - https://www.adafruit.com/index.php?main_page=product_info&products_id=46

Cable:
SOIC 8 pin - http://cgi.ebay.com/ebaymotors/ws/eBayISAPI.dll?ViewItem&item=400171159037 (as suggested by a member here - thanks!)

The purpose of doing this is mainly to have some fun, but it seems the majority of the 7135 constant current multi-mode drivers out there have a UI that I really dislike. Because they only have an ON and an OFF state, controlled by switching the power from the battery, they have that funky "3 second memory" function (because there is no way to know how long a light has been off). Now, I would like to have the benefits of brown-out detection with a capacitor (I believe I'm saying that right) so that the controller knows when a user clicks the light off and then back on in a split second. However, I can't seem to find a good driver that has that ability, at least without other drawbacks. Currently, the Solarforce low-voltage drop-in I have in front of me has a great UI. You can turn it on for a split second, and as long as you wait a second or two before turning it on again, it will not switch to the next mode. Also, if the light has been on for a few minutes, you can switch to the next mode just by tapping the reverse-clicky once, instead of twice like the drivers I listed above. However, the PWM is really bad on it.

So, my goal is to hopefully be able to program whatever UI I or a friend might like to have, at least within the limits of the hardware setup. I will need to be able to do this with pre-mounted controllers, such as on the drivers I purchased. In the future however, I might try to build a driver board myself, but I doubt I'll get there. I've never programmed in C before, and I was initially turned on to the PICAXE chip as you can program in BASIC. However, being that the Atmel tiny chips can run at lower voltage and seem to be more widely available on pre-built drivers, this seems the way to go.

Anyways, I just wanted to say thanks and warn you that there will probably be a lot of questions coming your way. I've hardly soldered much in my life, let alone try to work with a microprocessor :)

- Jon

Hi JonnyC, and welcome to this thread. Hope you'll still like it after striking some time with the challenges in this project.

The 8 pin clip can really give you some grey hair. It needs some fine fiddling to have full contact with the MCU and at the same time hang on to it. Also you will do some microscopic soldering, especially as you (like me) prefer the memory_after_OFF (pin-switching) which needs 3 (SMD) components to be mounted. Use a pair of +3 spectacles.

But it is a great reward in itself when you have made your first own driver. I like letting my light run down to watch the stepwise dimming functioning (there is appr. 3 hours of light left after the battery reach 3V on max).

Thanks sixty. Maybe you already posted it, but I'll be interested to see how you accomplished this. I really don't know much when it comes to electronic components, but I'm trying to learn. I'll just stick with the basics first (not physically altering the drivers that arrive), and then maybe move on to adding capacitors and such.