# Driver microcontroller hacking



## Fallingwater (Jan 4, 2009)

Browsing DX I found this fully regulated driver. Only problem it has are the modes; five are too many for me, especially with strobe and sos.
Looking at the pictures, I see that the board has a microcontroller on the underside that probably takes care of the modes via PWM.
The pictures aren't detailed enough to make out all the lettering, but from what I've been able to read and from the general shape of the writings it seems that it's more or less the same thing as the microcontroller in this buck-only two-mode driver.

This got me wondering... I don't have nearly enough knowledge in the field of microcontrollers to modify the programming myself, and even if I did I don't have the tools to program whatever kind of uC that is.
But what if I unsoldered the uC from the fully regulated board and swapped it with the one from the buck-only one? Since the uC only does PWM dimming, would this not give me a fully regulated two-mode board? (and a five-mode buck-only board if I bothered doing the swap on the other one as well)


----------



## linterno (Jan 5, 2009)

You could do the IC swap if the PIC12F629 output PWM pin in both drivers is the same. You should do a little bit more research on this.

In KD's driver it seem to me that the pin 7 is the output. but, in DX cannot see clearly, nothing that with wires and lifted pins cannot be solved.


----------



## Fallingwater (Jan 5, 2009)

So even if the output PWM pin isn't the same, bending up the pin and connecting it properly would work? In other words, in one way or the other this can be made to work?


----------



## linterno (Jan 5, 2009)

I couldn't absolutely say yes, but it is very likely.

I don't know what PWM frequency and duty is being used in each driver. But, if both do PWM I would give it a try.


----------



## Fallingwater (Jan 5, 2009)

Ok then, I'm placing an order for a KD five-pack of the five-mode regulated drivers (I have five buck-only ones incoming already). Will report on my findings when they eventually get here.

Edit: done. Apparently the ones in the KD 5-pack are 20-mode, which is better - if I complete the swap with the other board I'll have 20-mode AMC boards to use in less important projects; I dislike 20-mode drivers less than I dislike 5-mode ones, because almost all 20-mode ones have a group with just three steady levels and no strobe. It's too easy to change group by mistake though.


----------



## Tohuwabohu (Jan 5, 2009)

Swapping the microcontroller could be more difficult than you think.
I got a 5-pack of the KD linear buck drivers in december - they had a completely different layout and a different microcontroller.

Photo of one of the drivers I got from KD:





The microcontroller is an Atmel Tiny13V

Photo of one of the DX drivers




The microcontroller is a PIC12F629.

The PWM output pin and even the supply voltage pins are different.

It could even be that they use a oppisite voltage levels to switch the output on and off. Instead of 100% and 15% you could get 0% and 85%. But I'm not sure about that.


----------



## Fallingwater (Jan 5, 2009)

Are you absolutely sure you placed your order for the same item I did?

Well, it's too late to cancel the linear board order - they shipped it a few days ago; all I can do now is hope that I get the ones pictured in the KD page.
If I get the kind you did I'll try to get KD to send me the right ones, though I have little hope that they'd listen to me (since they've never replied to *anything* I've asked them). 

If I'm stuck with these I'll still try to figure out what pin does what and rewire the regulated board so as to accept the Atmel ATtiny. It shouldn't be too hard, considering only three pins seem to be connected to anything in both cases. Hopefully I won't let any magic smoke out...


----------



## Tohuwabohu (Jan 5, 2009)

Fallingwater said:


> Are you absolutely sure you placed your order for the same item I did?


Yes, I think so. I ordered these drivers from KD.
I'm hoping that they have a higher PWM frequency than the older version.
But I did not yet use them in any light.


----------



## linterno (Jan 5, 2009)

Sorry. I thought both drivers were using the same microcontroller. They usually put pic16f629 in theirs drivers.


----------



## ifor powell (Jan 5, 2009)

Here is a link to the info on the tiny. vcc pin 8 top left in the pic. gnd pin 4 bottom left the pwm will be from 6. If you realy want to play you can get somthing like a dragon programer for the avr's for not much ($40?) and the free software tools are very good although you would probobly have to work in asembler rather than C for the tiny13 it's about the chepest one Amtel do.

Never done any pic programing so no quick help there...

Ifor


----------



## Fallingwater (Jan 5, 2009)

ifor powell said:


> Here is a link to the info on the tiny. vcc pin 8 top left in the pic. gnd pin 4 bottom left the pwm will be from 6.


Thanks, this will be very useful. 
Do you know how the 3 pins would be connected to the PIC as well?



> Never done any pic programing so no quick help there...



I've done some, but my current knowledge is too low to write programs from scratch. Can one extract the program from a microcontroller, tweak it in C and then put it back in? That I could probably do...


----------



## Fallingwater (Jan 6, 2009)

Fallingwater said:


> Can one extract the program from a microcontroller, tweak it in C and then put it back in? That I could probably do...


Well, turns out that's impossible.

So the next question is: has anyone written flashlight code for PIC12F629 or ATtiny13 MCUs that they're willing to share/opensource?


----------



## linterno (Jan 6, 2009)

Fallingwater said:


> Do you know how the 3 pins would be connected to the PIC as well?


In PIC12F629 VCC is Pin 1, GND is Pin 8 and PWD could be 7 ( DX SKU 6190 uses pin 7; see *HOWTO: Making your own digital driver*.) However, could be other. You will need to find it out.



Fallingwater said:


> Can one extract the program from a microcontroller, tweak it in C and then put it back in? That I could probably do...


I don't think so. I guess the developer/programmer could have protected the flash memory from reading. Even if you could read it you won't be able to convert it to C language.

I still believe you can replace the PIC12F629 microcontroller with the ATTiny13V. PWM is PWM and, even if ATTiny13V can do inverted PWM, I don't thing they are doing it. I am almost sure the driver's uc does non-inverting PWM . The LED needs VCC not GND.


----------



## ifor powell (Jan 6, 2009)

The programming for these types of driver is interesting where they have a single switch that controls the power. I have never done it but I read somewhere about it. You basicaly have a capacitor to give you slow decay on the power that enables you to destinguish between a brown-out reset for just a quick click as opposed to a full power on reset for comming on from off for a long time. It's tricky and you may well find that just swapping the controller from your two drivers will not work because of it, they may need differny capacitor values and be set up for different brown out voltage levels for example.

My only coding experiance with lights is with re-working a bflex that I fried the AVR on and subsiquently swapped. I span my own software for this. It is a lot easier though as the bflex has a proper switch and has a more beafy Tiny85 which has 8k of flash and more ram and eprom so C coding is ok for the limited interface I have put on it. I am tempted though to get hold of the avr based driver you have linked to and have a play...

Ifor


----------



## Fallingwater (Jan 6, 2009)

I'll try a MCU swap first. If I get the PICwhatever boards it'll probably be a simple unsolder-solder job; if I get the others I'll try adapting the Atmels, if that doesn't work I'll try swapping the capacitors as well.

I'm seriously tempted to invest some time in learning more programming, getting a smaller programmable MCU (the ATmega168 I have now is unnecessarily complex for the task), design my interface the way I want it to be and then swap the programmed MCU in one of these drivers.

Don't hold your breath though  I'll have to learn a lot before I can do this... if I can even muster the willpower.


----------



## linterno (Jan 6, 2009)

You could learn PIC Programing easily if you do it in Basic language. Not the best but the easiest.

There is a free demo of MikroBasic which is a good compiler for Microchip microcontrollers. Mikroelectronika also has MikroC compiler which is a C language one. These compilers already have a lot of fuunctionality available including PWM. The demo version is limited to 2K words of code, more than enough if you want to program PIC12F629, PIC12F675 and PIC12F683 microcontrollers, which are good 8 pins ones.

Currently the best and less expensive PIC Programmer (to burn the program into the microcontroller) is PICkit 2 Microcontroller Programmer directly from Microchip ($ 35).

You can buy inexpensive PIC Microcontrollers delivered to Italy with cheap delivery cost from Futurlec. I have bought from them a couple of times. I would recommend you to buy PIC12F683 since it has A/D functionality which could be a good option if you want to implement Low Battery Disconnect in your driver.

I guess your ATmega168 is an Italian Arduino. Isn't it?


----------



## Fallingwater (Jan 6, 2009)

linterno said:


> In PIC12F629 VCC is Pin 1, GND is Pin 8 and PWD could be 7 ( DX SKU 6190 uses pin 7; see *HOWTO: Making your own digital driver*.) However, could be other. You will need to find it out.


Whoops - I missed this post. I'll adapt the pins, thanks.



linterno said:


> You could learn PIC Programing easily if you do it in Basic language. Not the best but the easiest.


I didn't know you could program MCUs in Basic. I remember some Basic from when I dabbled in it a bit ages ago; it is indeed easier than C. I'll investigate this, thanks.



> Currently the best and less expensive PIC Programmer (to burn the program into the microcontroller) is PICkit 2 Microcontroller Programmer directly from Microchip ($ 35).


I thought programmers were a lot more expensive. I'll order this soon, I think.



> You can buy inexpensive PIC Microcontrollers delivered to Italy with cheap delivery cost from Futurlec. I have bought from them a couple of times. I would recommend you to buy PIC12F683 since it has A/D functionality which could be a good option if you want to implement Low Battery Disconnect in your driver.


Noted. Will order one or three soon.



> I guess your ATmega168 is an Italian Arduino. Isn't it?


It doesn't have anything to do with Arduinos, at least as far as I'm aware. Or with Italy for that matter. It's just one of the more complex MCUs from Atmel. It came in a NerdKit which I received for review.


----------



## Tohuwabohu (Jan 6, 2009)

I removed the microcontrollers from two of my drivers.

DX SKU 15880 without the PIC12F629




Only 3 pins are connected.
VCC is pin 1, GND is pin 8 and PWM is pin 7.

The 2-mode linear buck converter I got from KD without the Atmel Tiny13V. 




VCC is pin 8, GND is pin 4 and PWM is pin 6.
Pin 2 is connected to the two resistors R1 and R2.


----------



## Fallingwater (Jan 6, 2009)

Hmm. Perhaps the Atmel can work without the two resistors? Otherwise I'll have to somehow put the resistors on the other board along with the MCU, if that is even possible.

What does the fourth pin do, anyway? It seems unnecessary.


----------



## Tohuwabohu (Jan 6, 2009)

Pin 2 of the Atmel can have different functions depending on the programming.
Perhaps I can try what happens when I remove both resistors or change the value of one of them tomorrow.


----------



## Fallingwater (Jan 6, 2009)

It might be related to battery status or regulation. We'll see.

Thanks for this... with our resources combined we might just hack together the best cheap driver out there


----------



## ifor powell (Jan 6, 2009)

Looking at the picture I would say that the two resistors are a voltage divider and pin 2 is bing used to do an analog to digital conversion to mesure the voltage. This is probobly to do with working out the switch stuff as I aluded to before. Having the switch on the power makes things tricky but with a sutible capacitor you could see the voltage start droping for just a small click and still stay active. That's just a guess but I expect you will need it for the code to work properly.

Ifor


----------



## mpf (Jan 7, 2009)

Fallingwater said:


> So the next question is: has anyone written flashlight code for PIC12F629 or ATtiny13 MCUs that they're willing to share/opensource?



I put up this thread,*Build a Basic uC 3 Level Led Driver - A Tutorial * 
https://www.candlepowerforums.com/threads/201383
on how to make a Attiny Led driver from scratch, no smds required.

it is listed in the *Flashlight Electronics - Batteries Included - Threads of Interest
*
Let me know if you need more help,
matthew


----------



## linterno (Jan 7, 2009)

A have tinkering with some PICAXEs which are PIC microcontrollers. There is no need for a programmer to load the program to it. Simply connect the PICAXE to your computer's serial port an you can download the program to PICAXE.

These are programmable in Basic and there is an 8 pin SMD model (PICAXE-08M Surface Mount AXE007MSM) which is fully compatible with PIC12F629.

Currently I own some PICAXE-08M (non SMD) and PICAXE-18X. 

I am going to work on a program to replace PIC12F629 in these drivers. Just give some time to do it since I am some busy these days.


----------



## Tohuwabohu (Jan 7, 2009)

Fallingwater said:


> It might be related to battery status or regulation. We'll see.


I think you are right.
Pin 2 of the Atmel seems to be used for low battery detection.
When the supply voltage of the board drops below 3V the driver switches to low mode and starts to blink.
When I pull pin 2 to GND (by shorting R2) the low battery warning is activated even when the supply voltage is above 4V.
When pin 2 is pulled up (by shorting R1) there is no low battery warning.
The voltage divider R1/R2 sets the level of the low battery detection.
I did not yet test what happens when both resistors are removed.

The PWM frequency of the board I tested is 580Hz.
In low mode the ON-time is 0.22ms, OFF-time is 1.5ms.

A member of the german Messerforum.net reported a PWM-frequency of 21kHz in low mode for some drivers he got from KD in october.
But he didn't buy the 5-pack, he bought some single drivers.
Some board he got earlier had a PWM frequency in the range of 500Hz to 540Hz.
But nobody ever mentioned a low battery warning for any of these boards.


----------



## linterno (Jan 7, 2009)

Tohuwabohu said:


> When the supply voltage of the board drops below 3V the driver switches to low mode and starts to blink.


This is excellent. 3 volts could be the ideal low voltage disconnect for Li-Ion batteries.

I have a question here: Would a simple voltage divider be enough to detect low voltage? I guess this ATtiny13V is doing some ADC in that pin.

EDITED: ATtiny13V has an internal voltage reference of 1.1V independent of the power supply. That is enough to detect low voltage.


----------



## Fallingwater (Jan 7, 2009)

linterno said:


> This is excellent. 3 volts could be the ideal low voltage disconnect for Li-Ion batteries.


Low voltage prevention is theoretically a good thing, but in my experience (with the NDI) the drivers aren't smart enough to differentiate inbetween the battery actually going low and temporary voltage fluctuations given by a slightly flaky switch.

Tohu, could you do a check and see if there's a time limit? In other words, does pin2 need to be grounded for a certain amount of time before the driver starts blinking (thus insuring temporary fluctuations don't activate the protection) or is it instant?

If it's instant I'll most likely permanently keep it up and remove the protection altogether. In that case I'll just use protected cells.



> I did not yet test what happens when both resistors are removed.


If you happen to test that please let me know 
How are the connections inbetween the two resistors, and the board?

Thanks for this research you're doing at the risk of your own driver board. 

KD haven't yet sent the regulated drivers. Argh, I'll have to wait a while before I can experiment myself.


----------



## Tohuwabohu (Jan 7, 2009)

linterno said:


> I have a question here: Would a simple voltage divider be enough to detect low voltage? I guess this ATtiny13V is doing some ADC in that pin.


Pin 2 of the Tiny13 can be configured as ADC input.
I'm not sure but I think it can also be used as a simple anlog comparator
With the voltage divider the low voltage detection level can be set to the desired value.



Fallingwater said:


> Tohu, could you do a check and see if there's a time limit? In other words, does pin2 need to be grounded for a certain amount of time before the driver starts blinking (thus insuring temporary fluctuations don't activate the protection) or is it instant?


I'm not yet sure how but I'll try to do it tomorrow.



> How are the connections inbetween the two resistors, and the board?


You can see it on the photo.
The upper end of R2 is connectet to GND, the upper end of R1 to VCC of the Atmel.
Pin 2 of the Atmel is connected to the connection of R1 and R2 (the lower end of the 2 resistors).


----------



## linterno (Jan 7, 2009)

Fallingwater:

If you are planning to buy or already bought this driver "from DX", not from KD, and you want to replace the microcontroller with the one in these drivers from KD then you can simply swap them since both drivers are using PIC12F629.

If you bought or are planning to buy these drivers from KD and want to replace the microcontroller with the one in these drivers from KD then that is a different story.


----------



## Fallingwater (Jan 7, 2009)

Wait, I'm confused.

Aren't this and this exactly the same from a hardware point of view? The pictures certainly seem of the same thing.
Compare and contrast:

DX:
http://www.dealextreme.com/productimages/sku_15880_1.jpg
http://www1.dealextreme.com/productimages/sku_15880_3.jpg

KD:
http://www.kaidomain.com/UploadFiles/633570483074060000.jpg
http://www.kaidomain.com/UploadFiles/633570483072653750.jpg

The components are all in the same places, have all the same connections and those whose readings I can decipher are the same.

The MCU programming is probably different, since the KD one seems to be 20-grouped-modes and the DX one only has five, but everything else seems the same.

I ordered these, because they are just like the above one from KD but in a five-pack and thus cheaper per-unit.

I was under the impression that the potential problem here lies in the fact that I ordered these buck-only circuits with the same PIC MCU as the regulated driver, but that I'll probably receive the revised version with the Atmel microcontroller instead, which will need its pins adapted to the new board.


----------



## Tohuwabohu (Jan 8, 2009)

Fallingwater, I think we have to wait until we see what version of the KD linear buck driver you get.
Sometimes I hate this sort of lottery when I order from KD or DX.
I really would prefer to know exactly what I will get when I order something.

I removed the two resistors from one of the KD linear drivers with the Atmel controller today: It is always blinking now, regardless of the supply voltage.
Pin 2 of the controller has to be pulled up to Vcc to prevent this.

With both resistors installed a loose contact of the power supply does not activate the low battery warning.
I tried this by touching the contact pads on the driver only very slighlightly with the leads from my power supply.
The light was flickering when I moved the leads across the contact pads but the low battery warning was not activated.

Pulling pin 2 down to GND seems to instantly activate the low battery warning. But what could pull down only pin 2 during normal operation?


----------



## Fallingwater (Jan 8, 2009)

Huh. In that case I might actually keep the safety circuit. If I get the driver with Atmel MCU, that is.

I agree though, I have to wait. *sigh*


----------



## linterno (Jan 13, 2009)

*PICAXE-08M to replace this driver's microcontroller*

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.


```
'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%

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

I will be working in a MikroBasic code that can be used in the stock microcontroller, but a PIC programmer will be necessary.

Juan C.


----------



## Fallingwater (Jan 14, 2009)

I love you 

I'll be ordering some PICAXE MCUs very soon


----------



## Fallingwater (Jan 23, 2009)

I just received the buck drivers, and they are the revised version with the Atmel MCU. I'll use one straight away (after unsoldering two of the AMC chips) on a 5mm honeycomb light, and I'll probably put one in the L-Mini as well, at least until I get the fully regulated ones (I want the L-Mini fully regulated, but I'm dying to use it, so this will do while I wait).

I've had a look at the Atmel MCU, and I'm wondering how I'm going to fit the resistors and connection for the low-voltage alarm to the regulated board. Oh well, we'll see.

Linterno: I'm considering ordering a few AXE007MSM. Do you know of places other than the RevEd site where I can get them?

As for your program: can I consider it open source and/or free to all intents and purposes? I'm thinking of modifying it so you change mode by softpressing three times instead of just one (if, that is, I can actually do it). This way I could use the light momentarily without it always changing mode.


----------



## linterno (Jan 23, 2009)

Fallingwater said:


> Linterno: I'm considering ordering a few AXE007MSM. Do you know of places other than the RevEd site where I can get them?


No. I have not seen any other place selling SMT ones.



Fallingwater said:


> As for your program: can I consider it open source and/or free to all intents and purposes?


Yes you can. If you use it for commercial purposes you could give me a free unit of the product  .



Fallingwater said:


> I'm thinking of modifying it so you change mode by softpressing three times instead of just one (if, that is, I can actually do it).This way I could use the light momentarily without it always changing mode.


No, you cannot because the program is not using the microcontroller's Brown-out detection capability. I tried to use this capability but it didn't work with PICAXE (Probably I did something wrong). What you could do is to change in the program the mode switching time to make it smaller (half a second or a second). With a modification to the program we could use a switch to increase and other tr decrease intensity in any percentage step. 

Juan C.


----------



## Fallingwater (Jan 23, 2009)

I'm not currently planning on any commercial endeavour with the modded driver, but who knows 

Too bad I can't mod the program to do that... as far as I know no existent driver does it in that way, and it seems a whole lot more practical to me than the usual click-switch.
Oh well, I'll just shorten the mode switching delay to something like, oh, a third of a second. That should make the light usable for pretty much any purpose while still leaving it capable of switching mode if need be.

Does something like the axe007msm in a non-smt version exist elsewhere? I keep seeing a lot of unused space in many pills, having some taken by the MCU shouldn't be much of a problem.


----------



## linterno (Jan 23, 2009)

Fallingwater said:


> Does something like the axe007msm in a non-smt version exist elsewhere? I keep seeing a lot of unused space in many pills, having some taken by the MCU shouldn't be much of a problem.


Yes, You can buy not SMT PICAXE at Professor Peter Anderson's site. He sells items really low price and low shipping cost too. He ships to Italy. There is where I have bought mines.

There is a Distributor in Italy but their prices are much higher.

Just remember that with PICAXE you need a 10K + 22K pull down resistor. (see picaxe_manual1.pdf).

Juan C.


----------



## Fallingwater (Jan 24, 2009)

Peter Anderson has a $25 minimum order for international orders, and similar prices to RevEd... I'm just going to order the SMT MCUs at RevEd. I guess £1.73 each + shipping is a small price to pay for a fully configurable interface. 

Edit: ordered five  cost me about €13 shipped.


----------



## linterno (Jan 26, 2009)

Fallingwater:

This is the code to implement mode change by soft pressing three times instead of just one.


```
'LED PWM Driver software.
'for PICAXE-08M. Could work with other PICAXEs but should be revised.
'Written by Juan C Galvez

' the following EEPROM entry contains the desired levels percentages
' The first entry must be 1. this byte contains the level position to use the next time the driver is powered.
' Next are the desired levels 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% and 100%
' 1, 10, 100, 0               means 10% and 100%
' 1, 10, 30, 50, 100, 0       means 10%, 30%, 50% and 100%

' Each level must be between 1 and 100 or unexpected results might occur.

' No SOS. No STROBE. Just plain brightness levels.

'EEPROM 0,(1, 1, 10, 30, 50, 100, 0)
EEPROM 0,(1, 5, 50, 100, 0)

' If the driver it turned on and off TIMES_TO_SWITCH_MODE times before LEVEL_SHIFT_PERIOD has elapsed then there will be a mode switch.

symbol LEVEL_SHIFT_PERIOD = 500
symbol TIMES_TO_SWITCH_MODE = 3

' PICAXE-08M uses output 2 (which is pin 5 in the microcontroller) as the PWM pin for PWMOUT command.
' Please read PICAXE manuals for minimum setup which requires at least 2 resistors for programming and at least 1 resistor for proper operation.
' Sorry about my not good English. Probably some comments are not clear enough.

' b0     keeps the current level position  b0 = b0 & $0F
' b1     Current level value (%)
' b4     Number of times the flashligth has been turned on. Uses to switch to the next mode.

read 0,b0           ' Read next level position

b4 = b0 & $F0       ' Take high nibble
b4 = b4 / 16        ' shift 4 positions to the right (PICAXE-08M) doesn't support >> operand
b4 = b4 + 1            ' Turned on one more time, so count it.

b0 = b0 & $0F       ' take low nibble which is the current level position

if b4 >= TIMES_TO_SWITCH_MODE then
  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
  b4 = 0
end if

b4 = b4 * 16        ' shift 4 positions to the left (PICAXE-08M) doesn't support >> operand
b0 = b0 | b4        ' High nibble contains time on. Low nibble contains current level position.
write 0, b0         ' Write the value to EEPROM position 0

b0 = b0 & $0F       ' Take low nible only.
read b0, b1         ' Read current level value

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.

write 0, b0         ' Write current level position to EEPROM. high nibble is zero here.

b0 = 1              ' Enter an infinite loop
do
  pause  2000       ' Used to allow firmware upgrade while running.
loop while b0 = 1
```
Those three times if defined in symbol TIMES_TO_SWITCH_MODE.

The maximum time between soft presses is (in the published source code) half a second. You can change it by modifying the line containing:

symbol LEVEL_SHIFT_PERIOD = 500

500 is the number of milliseconds.

If you don't do the soft pressing three time (with half a second maximum wait between one an the other) then the current mode is kept.

My English is not that good and some times it is difficult for me to use the correct words to explain the idea. I hope you get it.

I tested this code with a regular 5mm red LED connected to PICAXE-08M.

Juan C.


----------



## Fallingwater (Jan 26, 2009)

Did I already mention that I love you? 

I don't really care about strobe mode at all, but just as a curiosity: say I set one of the modes to 0.01 - wouldn't this cause the PWM to become so slow as to effectively become a strobe?


----------



## linterno (Jan 26, 2009)

Fallingwater said:


> but just as a curiosity: say I set one of the modes to 0.01 - wouldn't this cause the PWM to become so slow as to effectively become a strobe?


The percentage must be an integer number between 1% and 100%.

Since the frequency is 10Khz (PICAXE implementation), you will never get a strobe. Strobe should be programmed different.

Most people don't like strobe. Do you?

Juan C.


----------



## linterno (Jan 26, 2009)

How useful would be a UI like this?: Once on, a fast off-on cycle will go to first mode (lowest/dimmest), two fast off-on cycles will go to second level, three fast off-on cycles will go to third level and so on until the last defined level. If the flashlight is just turned on then the last level will be kept.


----------



## Fallingwater (Jan 26, 2009)

linterno said:


> Most people don't like strobe. Do you?


Very no. As I said, I was asking out of curiosity.



linterno said:


> How useful would be a UI like this?: Once on, a fast off-on cycle will go to first mode (lowest/dimmest), two fast off-on cycles will go to second level, three fast off-on cycles will go to third level and so on until the last defined level. If the flashlight is just turned on then the last level will be kept.


Hmm. This would give the ability to jump levels at will without having to go through the other ones (assuming more than two are programmed). Nice idea  only I'd reverse the order, i.e. one cycle gets you to full power. In an emergency you'd want to have full power as close as possible, after all.


----------



## Fallingwater (Jan 29, 2009)

I just received the regulated drivers. I want to wait for the Picaxes, but I might just get too impatient and try the MCU swap I had originally in mind.

Edit: just tested the regulated drivers: they have no low-voltage protection whatsoever, and will in fact keep drawing about an amp at 2V. This is not good at all. I might be able to transplant the MCU of the AMC circuit along with its resistors, and thus give the board low-voltage protection, but the custom programmable interface with the Picaxe would still be protection-free.

Linterno: is there any way of getting the Picaxes to do low-voltage checking?


----------



## linterno (Jan 29, 2009)

Fallingwater said:


> Linterno: is there any way of getting the Picaxes to do low-voltage checking?


Yes, with a voltage reference IC (some one tolerant to temperature changes, low voltage operation 2.8v+) and a resistor, the PICAXE analog port could be used to check for low voltage and disconnect the LED.

Juan C.


----------



## Fallingwater (Jan 29, 2009)

What sort of IC and what resistor value would I need?

Dammit, I want my dream interface and I'm gonna get it


----------



## linterno (Jan 29, 2009)

If your flashlight won't get warmer than 70ºc then LM385-1.2 will work, if lower than 85ºC then use LM285-1.2 and if lower than 125ºC then use LM185-1.2.

Here you can get more information about the IC. 

Since the minimum operating current is 20 micro Amps then I think a ~30KOhms would work (100 micro Amps at 3 volts, which is just the disconnect voltage I would like to use).

Please read the datasheet. I am an electronics "aficionado" not an expert. Mr Happy could tell you more here. He seems to be an "electronics expert".

Basically reading this voltage reference and the battery voltage you can determine the disconnect voltage. I did some tests using a 5 mm LED as the voltage reference.

Juan C.


----------



## Fallingwater (Feb 4, 2009)

Quoting from your program:


> ' Please read PICAXE manuals for minimum setup which requires at least 2 resistors for programming and at least 1 resistor for proper operation.


I have read the manual (well, I skimmed through it and then searched for "resistor"), and while I can see how to use the two resistors for programming I see no mention of a resistor that is permanently required for operation of the chip. :thinking:


----------



## MatajumotorS (Feb 4, 2009)

Fallingwater said:


> Quoting from your program:
> 
> I have read the manual (well, I skimmed through it and then searched for "resistor"), and while I can see how to use the two resistors for programming I see no mention of a resistor that is permanently required for operation of the chip. :thinking:


 No resistors needed to program PIC or AVR chip.


----------



## Fallingwater (Feb 4, 2009)

I was talking about the Picaxe for which linterno wrote the programs.


----------



## linterno (Feb 4, 2009)

Fallingwater said:


> Quoting from your program:
> 
> I have read the manual (well, I skimmed through it and then searched for "resistor"), and while I can see how to use the two resistors for programming I see no mention of a resistor that is permanently required for operation of the chip. :thinking:


In PICAXE manual 1 page 24 you can find the "The minimum operating circuit for the 8 pin devices". There are two resistors: a 10K from GND to middle point and a 22K from middle point to pin 2 (a voltage divider). Those two resistors are required for programming. In your driver (unless you want to be able to program in circuit) a 22K resistor alone would be enough. I guess this is a pull down resistor to avoid the input pin to be floating and generating instabilities to the PICAXE.



MatajumotorS said:


> No resistors needed to program PIC or AVR chip.


You are not "programming a PIC microcontroller" but a PICAXE. The latest, even if it is a PIC, uses it own serial communication to auto-program the device, and Yes, you need two resistor to program the device (See PICAXE Manual 1 page 24).


----------



## Fallingwater (Feb 9, 2009)

I just received the picaxes. I'm going to start experimenting soon.


----------



## linterno (Feb 10, 2009)

Let me know when you get the voltage reference IC and when you want to test LVD (Low Voltage Disconnect).

Once you do you tests let me know how everything went. Then, we could try to lower the PICAXE (PIC) clock to reduce power consumption (to ~60[FONT=Arial, Helvetica, sans-serif]µA)[/FONT]. Additionally, if the PWM frequency it to high for the driver's coil then we could reduce it too.

Juan C.


----------



## Fallingwater (Feb 10, 2009)

Soldered the picaxe to the circuit, now building the cable. 
How will I know if the frequency is too high for the coil?

The low voltage thing will wait until I get the rest working.

Thanks for the help


----------



## linterno (Feb 10, 2009)

Fallingwater said:


> How will I know if the frequency is too high for the coil?


If the LED is not driven as expected..

I am no an electronics expert but I think that 10Khz is not to high frequency for the drivers coil.


----------



## PEU (Mar 7, 2009)

Buen trabajo Juan!!


Pablo


----------



## linterno (Mar 7, 2009)

Hola Pablo. Muchas gracias. Es un honor para mi que un experto como usted lo escriba.


----------



## gillestugan (Mar 10, 2009)

En ingles, por favor...

This is great fun. Im working on a program for a picaxe 14M in a headlamp. (project is currently on hold as I need an oscilloscope to measure ripple. ) It will be controlling 3 separate AX2002 based drivers by analogue voltage to the feedback pin. No PWM as the ax2002 itself is using PWM internally.
It will also have temperature protection and battery level monitor. 

And thanks linterno for the code. Especially the extraordinary well written notes!


----------



## linterno (Mar 10, 2009)

Would be good to see you final project when done.

I was thinking to include low battery protection using a LM185-1.2 be safe using Li-Ions.

What do you want to protect and what device are you going to use to monitor temperature? a DS18B20?


----------



## BillyNoMates (Mar 11, 2009)

This is an interesting thread. I used a PIC 16F506 in the bike light / headtorches I've been building. These little beasts are a great way of customisng the operation of any home brew light.

I wrote the program in assembler and here is a list of the functionality I included in my first application:

Four brightness settings (spaced apart by a factor of x3 in power) and two strobe settings (for urban riding).

Single button operation; long press to wake-up / sleep, double click to increase brightness, single click to decrease brightness (Note this control drives enable pins on the driver ICs rather than PWM).

Battery voltage monitor used to drive tri-colour LED and force minimum brightness mode when battery reaches 80% depleted.

Memory function that remembers the last brightness setting used when powered down. This resets to the initial state if the battery is disconnected.

Here's a pic of the PIC on my custom driver board (5x rebel headtorch / bike tail light design).


----------



## VidPro (Mar 11, 2009)

BillyNoMates said:


> This is an interesting thread. I used a PIC 16F506 in the bike light / headtorches I've been building. . . .


 wow!


----------



## ifor powell (Mar 11, 2009)

Very nice looking boards. Whats the size? 20-25mm? What is the cooling like on the rebels? I guess you don't push them flat out?

Ifor


----------



## BillyNoMates (Mar 11, 2009)

ifor powell said:


> Very nice looking boards. Whats the size? 20-25mm? What is the cooling like on the rebels? I guess you don't push them flat out?
> 
> Ifor



Hi Ifor

The boards are 30mm in diameter. The 5x rebel optic from Ledil has a 35mm diameter, so that sort of defined the board size. It could go a bit smaller but that needs more effort on the layout !!).

The rebels run at 750mA each (the drivers will supply upto 1000mA per LED), so there is a reasonable amount of heat to get rid of. I rely on using the vias under and around the thermal PAD of the rebels to conduct the heat to the back of the board (plain copper) from where the heat is extracted and conducted to the main case/heatsink.

The microcontroller can be programmed in place so it is easy to change the function of the light if I so desire - I have different loads for front and rear for example.


----------



## gillestugan (Mar 11, 2009)

I will be using two resistors to get voltage division and measure the voltage with the ReadADC command. 
Same for temperature monitoring except I will use a thermistor in combination with a resistor.
Then simple "If" to make it lower output when temperature goes to high.

I will have one MC-E and two XP-Es in a 5*4*3cm housing, so it will easily get way too hot if I leave it on maximum for too long. I want the protection so I don't have to worry.

I have an exam later this week, but will continue work on it next week. Will post code, schematics and drawings when done. (may take some time...)


----------



## gillestugan (Mar 11, 2009)

BillyNoMates said:


> Hi Ifor
> The microcontroller can be programmed in place so it is easy to change the function of the light if I so desire - I have different loads for front and rear for example.



That is great! very nice looking boards! Are the vias on the LED boards enough to keep the leds cool? Why did you choose to use normal PCB material instead of copper or aluminium? cost?

Are you using PWM regulation? If so, what is the drive level when led is on? 1000mA?

Are you selling the boards/lamps? They look very professional.


----------



## linterno (Mar 11, 2009)

gillestugan said:


> I will be using two resistors to get voltage division and measure the voltage with the ReadADC command.


You should better use ReadADC10, but anyways the resistor voltage divider won't work.

The ReadADC returns a value between 0 and 255. ReadADC10 returns a value between 0 and 1024. Using a resistor voltage divider you will always get the same value returned from ReadADC/ReadADC10 no matter what the battery voltage is. This is because the value returned by ReadADC10 is based on the VDD voltage divided by 1024 possible values (10 bit resolution). i.e: Assuming that your battery voltage is (x) and you use a 5k and 5k resistor voltage divider will give you (x)/2 or half the input voltage which is the battery voltage. If the battery voltage is 4.2 or 3.8 or 3 volts ReadADC10 will always return 512 and ReadADC will always return 128 for the previous resistor combination.

You need a voltage reference to determine the battery voltage.


----------



## gillestugan (Mar 12, 2009)

Yes, but I will already use a 3,3V voltage regulator for power supply of the picaxe as I will use batteries with higher voltage than 5.5V, so Input voltage will never be the same as battery voltage. 
I also think 8-bit resolution is enough as I then will get 0,013V steps, which should be enough. But then of course it wont hurt to have the higher resolution of the ReadADC10 .


----------



## BillyNoMates (Mar 12, 2009)

The ADCs on the PICs all read relative to the supply: Full scale corresponds to an input = to the PIC supply, 0 corresponds to GND.

To get around this in my app, I configured the ADC input to the internal 0.6V reference (this is possible in the PIC I used). This way the ADC read tells me what fraction of the supply voltage the 0.6V reference corresponds to. Below is an extract of the assembler for this function showing the numbers I used:

; This function uses the ADC to read the internal reference *
; voltage. This is 0.6V, but the value returned by the ADC is *
; reference to the supply voltage. The higher the supply voltage, *
; the lower the ADC value. An example Table is given below: *
; ADC VCC *
; 57.0000 2.7000 *
; 53.0000 2.9000 *
; 49.0000 3.1000 *
; 46.0000 3.3000 *
; 44.0000 3.5000 *
; 41.0000 3.7000 *
; 39.0000 3.9000 *
; 37.0000 4.1000 *
; 36.0000 4.3000 *
; 34.0000 4.5000 *
; 33.0000 4.7000 *
; 31.0000 4.9000 *

Of course, using a regulated voltage for the PIC works just as well...


----------



## BillyNoMates (Mar 12, 2009)

gillestugan said:


> That is great! very nice looking boards! Are the vias on the LED boards enough to keep the leds cool? Why did you choose to use normal PCB material instead of copper or aluminium? cost?
> 
> Are you using PWM regulation? If so, what is the drive level when led is on? 1000mA?
> 
> Are you selling the boards/lamps? They look very professional.



I only have a couple of boards at the moment so I don't have any for sale. If there is enough interest in this particular implmentation I could consider doing another run. I'm planning on waiting until I have finished doing a nice case before I intend posting full details on this build.

I used a standard 1.6mm PCB simply because the on-line PCB supplier I used only offered this material on their prototype (low cost) runs.

The vias should be enough to provide a thermal resistance of approx 10k/W per LED with this board - more vias can reduce this a bit. I'm running the LEDs at about 2.5W (750mA drive) so the thermal pad will be about 25K above the baseplate temp....

I don't use PWM, instead I use different drive levels + different number of LEDs.

1xLED at 125mA
1xLED at 450mA
5xLED at 250mA
5xLED at 750mA

I'll post more info when I get this finished - shame the light nights are approaching, perhaps I'll have to take up caving


----------



## gillestugan (Mar 12, 2009)

Yes its getting lighter very fast. I probably will have my lamps finished too late, but I also love those summer nights when it never gets dark.

I think the guys over at MTBR would be very happy to see your boards if they not already have.


----------



## bikerjay (Aug 31, 2009)

I have a non micro controller related question about this board I can not seem to find an answer for. I would to be able to identify and change the sense resistor, which is presumably on the side of the board with the inductor. What I am going for is lowering the current from 1000ma to 700ma. I am also curious what people think about using this board with a single red orange luxeon III, given its very low forward voltage.


----------



## gillestugan (Aug 31, 2009)

I have been using AX2002 based boards with luxeonIII reds for garden illumination. As long as you are using current regulated drivers you will have no problem with the lower Vf.


----------



## astanapane (May 19, 2010)

Hello all,

i just show your posts. I'm new to this hobby according to programming MCUs and i would like to ask you if there is any HEX file for pic12f629 in order to program it and use it for personal only use. 

I would like specifically to make some outdoor light for the house and also for internal use also.

Thank you very much for your help in advance.


----------



## manne (May 19, 2010)

If someone interested to reprogram the DX linear drivers (AMC7135 with ATTINY13V), here i wrote a german tutorial (maybe google translate helps) includes a BASCOM program: http://www.messerforum.net/showpost.php?p=664718&postcount=52 

It is without memory function and can be customize between 2 or 3 mode and the (256) values of the dimm mode(s) using the plated-through stars on the backside of the board. 

Cause i have some older versions with the 12f629 PIC i compiled the program (without ADC and customize parts) in Mikrobasic, the EEPROM part was working fine, but got some problems to make an acceptable fast software PWM and gave up as the PIC seems to gave up too.


----------

