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

863 posts / 0 new
Last post
Tido
Offline
Last seen: 3 years 2 months ago
Joined: 05/28/2010 - 15:28
Posts: 189
Location: Berlin, Germany

sixty545 wrote:

Now to the big question: are drivers with ATtiny about to die out?

That would be a real bummer, the Atmels are the easiest to re-program. OTOH, I've got a PIC-Kit2 programmer on the way, so this is a strong incentive to port the BLF-VLD to the PIC platform Wink

DrJones
DrJones's picture
Offline
Last seen: 5 years 4 months ago
Joined: 01/05/2011 - 13:30
Posts: 1044
Location: Frankfurt, Germany

Hi, I followed this thread with great interest, and I already programmed some kind of flashlight driver (a somewhat special thing for geocaching though, not a general flashlight; wanted to do that next).

I too got the new 105A from KD (unfortunately a 5-pack), while I earlier got two 105 ones. And fortunately I have still some AK101 and AK47 around.

I just got into programming AVRs (WinAVR/AVR Studio), I don't know if I really want to adapt to PICs, too.

I wonder about code efficiency... The 12F629 has space for 1024 instructions (of 14bit each...), which is more than the ATtiny13 (1024 Bytes for 512 two-byte-instructions), but tiny13 has a bigger (more powerful?) instruction set (120) than the PIC (35). The available free compiler (Hi-tech PICC lite) probably will optimize less than the avr-gcc  -  So I'm looking forward to your porting reports.

Furthermore the 12F629 doesn't have ADC nor PWM (as far as I know).  -- I prefer AVR I guess Smile

Did you order a 'real' PICkit2 (~33 EUR, ebay) or a cheaper clone (~17 EUR, ebay)?

SPAMBOT
SPAMBOT's picture
Offline
Last seen: 3 years 9 months ago
Joined: 07/16/2010 - 18:40
Posts: 484
Location: Old World

Hi and welcome to the BLF DrJones!

Hope you will enjoy your stay : )

Now with 100% all natural asbestos!

fran82
Offline
Last seen: 9 months 3 weeks ago
Joined: 07/31/2010 - 10:15
Posts: 2588

Hey DrJones, Glad to have you here!

I knew about your site many many months ago! Thanks for your help!

This post/thread "may" contain referrals, a little contribution I "earn" in form of points ONLY if you buy the item. The purpose is to redeem items using the points and then making reviews of them in the forums to shar

Match
Match's picture
Offline
Last seen: 5 years 5 months ago
Joined: 11/14/2010 - 11:57
Posts: 1488
Location: South Carolina, USA

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


sixty545
Offline
Last seen: 1 year 7 months ago
Joined: 10/25/2010 - 14:15
Posts: 702
Location: Denmark (GMT + 1)

Hi and welcome DrJones.

Good to have another active programmer here.

linterno
Offline
Last seen: 7 years 10 months ago
Joined: 01/05/2011 - 18:55
Posts: 80
Location: El Salvador, Central America

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.

Match
Match's picture
Offline
Last seen: 5 years 5 months ago
Joined: 11/14/2010 - 11:57
Posts: 1488
Location: South Carolina, USA

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

fishinfool
fishinfool's picture
Offline
Last seen: 2 years 1 month ago
Joined: 03/09/2010 - 00:30
Posts: 4342
Location: Hilo, Hawaii

Aloha and welcome to BLF DrJones and linterno!

Don wrote:

"But as I said long ago, you are more likely to be killed by a dead fish dropped by a seagull in the Sahara Desert than by a lithium ion

DrJones
DrJones's picture
Offline
Last seen: 5 years 4 months ago
Joined: 01/05/2011 - 13:30
Posts: 1044
Location: Frankfurt, Germany

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 Smile Hello all Smile

fran82
Offline
Last seen: 9 months 3 weeks ago
Joined: 07/31/2010 - 10:15
Posts: 2588

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

This post/thread "may" contain referrals, a little contribution I "earn" in form of points ONLY if you buy the item. The purpose is to redeem items using the points and then making reviews of them in the forums to shar

sixty545
Offline
Last seen: 1 year 7 months ago
Joined: 10/25/2010 - 14:15
Posts: 702
Location: Denmark (GMT + 1)

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.

linterno
Offline
Last seen: 7 years 10 months ago
Joined: 01/05/2011 - 18:55
Posts: 80
Location: El Salvador, Central America

Thank you all for giving me a welcome.

sixty545 wrote:

... (I guess 'linterno' is spanish for 'lantern').

In fact in spanish is "Linterna" but that is female. Since I am a man I prefer to use "Linterno"
DrJones
DrJones's picture
Offline
Last seen: 5 years 4 months ago
Joined: 01/05/2011 - 13:30
Posts: 1044
Location: Frankfurt, Germany

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!" Smile )

Tido
Offline
Last seen: 3 years 2 months ago
Joined: 05/28/2010 - 15:28
Posts: 189
Location: Berlin, Germany

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 Wink

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.

Boaz
Boaz's picture
Offline
Last seen: 1 day 7 hours ago
Joined: 11/07/2010 - 09:31
Posts: 7659
Location: Birthplace of Aviation

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 ...

       καὶ τὸ φῶς ἐν τῇ σκοτίᾳ φαίνει καὶ ἡ σκοτία αὐτὸ οὐ κατέλαβεν

                            

       Dc-fix diffuser film  >…  http://budgetlightforum.com/node/42208

Tido
Offline
Last seen: 3 years 2 months ago
Joined: 05/28/2010 - 15:28
Posts: 189
Location: Berlin, Germany

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

sixty545
Offline
Last seen: 1 year 7 months ago
Joined: 10/25/2010 - 14:15
Posts: 702
Location: Denmark (GMT + 1)

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.

DrJones
DrJones's picture
Offline
Last seen: 5 years 4 months ago
Joined: 01/05/2011 - 13:30
Posts: 1044
Location: Frankfurt, Germany

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

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

sixty545
Offline
Last seen: 1 year 7 months ago
Joined: 10/25/2010 - 14:15
Posts: 702
Location: Denmark (GMT + 1)

DrJones wrote:

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

Can you tell the sku no. please.

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

pipopopo
Offline
Last seen: 7 months 1 day ago
Joined: 12/23/2010 - 15:07
Posts: 189
Location: EU

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

following this thread with interest  but way over my head

JonnyC
JonnyC's picture
Offline
Last seen: 5 months 3 days ago
Joined: 01/14/2011 - 19:12
Posts: 1148
Location: Green Bay, WI - USA

Excellent thread!  In fact, I registered on this forum just so I could post here Wink  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-... (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 Smile

- Jon

sixty545
Offline
Last seen: 1 year 7 months ago
Joined: 10/25/2010 - 14:15
Posts: 702
Location: Denmark (GMT + 1)

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).

JonnyC
JonnyC's picture
Offline
Last seen: 5 months 3 days ago
Joined: 01/14/2011 - 19:12
Posts: 1148
Location: Green Bay, WI - USA

sixty545 wrote:

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.

Don
Don's picture
Offline
Last seen: 11 months 5 days ago
Joined: 01/12/2010 - 16:32
Posts: 6617
Location: Scotland

Welcome JonnyC. Good to have you here, I hope you enjoy your stay.

 

The numbers from my light tests are always to be found here.

https://spreadsheets.google.com/ccc?key=0ApkFM37n_QnRdDU5MDNzOURjYllmZHI...

fishinfool
fishinfool's picture
Offline
Last seen: 2 years 1 month ago
Joined: 03/09/2010 - 00:30
Posts: 4342
Location: Hilo, Hawaii

Aloha and welcome to BLF JonnyC!

Don wrote:

"But as I said long ago, you are more likely to be killed by a dead fish dropped by a seagull in the Sahara Desert than by a lithium ion

Tido
Offline
Last seen: 3 years 2 months ago
Joined: 05/28/2010 - 15:28
Posts: 189
Location: Berlin, Germany

I received a bunch of 101-AKs last Tuesday, all were equipped with ATtinys. Ordered on December 26.12., received on 10.1. KD really seem to have streamlined their delivery process.

If anybody's interested in news from the PIC port, I have given up on the open source tool-chain. SDCC just isn't anywhere near GCC when it comes to code quality or features. I switched to Microchip's Hi-Tech compiler, which also sucks hard enough to pull golf balls through a garden hose, but at least it produces working machine code.

So far I managed to implement timer-interrupt based PWM and something analog to the watchdog-timer ISR I used in the AVR version. It looks like I can use the same basic architecture as in the original BLF-VLD. Today I wanted to test the PWM, so I copied some mode functions over from the AVR version and they just worked without any changes. I'm almost tempted to try keeping a common code base between AVR and PIC...

sixty545
Offline
Last seen: 1 year 7 months ago
Joined: 10/25/2010 - 14:15
Posts: 702
Location: Denmark (GMT + 1)

Tido wrote:

If anybody's interested in news from the PIC port, I have given up on the open source tool-chain. SDCC just isn't anywhere near GCC when it comes to code quality or features. I switched to Microchip's Hi-Tech compiler, which also sucks hard enough to pull golf balls through a garden hose, but at least it produces working machine code.

So far I managed to implement timer-interrupt based PWM and something analog to the watchdog-timer ISR I used in the AVR version. It looks like I can use the same basic architecture as in the original BLF-VLD. Today I wanted to test the PWM, so I copied some mode functions over from the AVR version and they just worked without any changes. I'm almost tempted to try keeping a common code base between AVR and PIC...

That sounds very good (given my growing collection of PIC based drivers).

But Hi-Tech compilers did not used to come cheap! (I have used Hi-Tech C compiler in the past so I know that Aussee compagny, there were also some bugs....., but the firm was always responsive and fresh to do corrections).

How much would the new expences be to cover the PIC development (lightest set-up)?

Happy to see that you keep up your enthusiasm despite job-hunting and perhaps little interrest from the forum - not so many with MCU programming experience, I think.

Tido
Offline
Last seen: 3 years 2 months ago
Joined: 05/28/2010 - 15:28
Posts: 189
Location: Berlin, Germany

Hi-Tech's "Light" version is free, but the produced code size is atrocious. I couldn't get the driver to fit into the program area, even with just a single mode function compiled in. Using the "Demo" version, I still got 15% free space, and that is with all available mode functions added. So it seems like I won't be allowed to distribute compiled hex-files. Maybe we can find somebody with a Pro license to build these for us, so they can be distributed legally.

Some good news. Today I felt a bit daring and just copied the missing parts of code over from the AVR source. After adding some functions for compatibility (mostly EEPROM stuff) and changing register names, I managed to compile the driver and it just worked. So far, all the mode functions and switching between standard and extended mode groups are working. Only mode programming is broken, but that shouldn't be too hard to figure out. I'm really surprised at how easy it was to port this thing.

Now I have to find a way to flash the program into a real driver PCB. So far, I only tested it on my breadboard model. None of my PIC-based driver PCBs will allow me to use the debugging clip, as the MCU is always obstructed by other components. I think I'll have to glue something together from two connecting plugs with 1.27mm pitch.

DrJones
DrJones's picture
Offline
Last seen: 5 years 4 months ago
Joined: 01/05/2011 - 13:30
Posts: 1044
Location: Frankfurt, Germany

Tido wrote:
Hi-Tech's "Light" version is free, but the produced code size is atrocious.

Thanks for that report!

That confirms my worries; I guess I'll not go that way and stick to AVR instead. Fortunately AVR drivers are still available.

sixty545: I edited my post; It now has a working link.

Pages