# PWM bike lamp regulator

#### corblimey

##### New Member
Can anyone help me with some coding?

I want to power a 12v halogen bulb from a 14.8v li-ion battery pack and regulate the supply voltage using pwm. Amongst other things my aim is to flatten out the battery discharge curve to provide a longer run time.

For the sake of simplicity assume that the required regulated output voltage is going to be set at 12v. I may change this value at some point e.g. 6v for dipped beam.

My circuit is pretty straight forward. So far I have a high effiecency mosfet driven by the picaxe pwm output to switch the power to the lamp. This part is working and I can light my bulb and start it by ramping the pwm from 0v to provide a soft start feature.

I am currently missing the feedback loop both in terms of the physical ciruit design and the code. My intention is to measure the voltage at the lamp using the ADC input of my picaxe via a potential divider to ensure the input voltage remains within the range the ADC function can handle. There are a couple things I need help with.

1) Since I need to measure the average voltage seen at the bulb and not the pulse voltage I need to smooth it out before it's fed into the ADC input. I assume I do this with a capacitor in parallel to the resistor in the potential divider I'll be taking my measurement from. Is this correct and if so how do I choose the correct value for the capacitor?

An now the tricky bit...

2) Any pointers on the code I should use to calculate the pwm duty cycle and period on the fly from the measured feedback voltage.

BTW I'm currently using a 08M.

Any help, much appreciated.

Thanks

#### BeanieBots

##### Moderator
The rate at which you will need to change the output will be very slow so I'd go for significant overkill on the PWM-&gt;ADC filtering so that you don't need to worry about ripple voltages and what to do about it in code.
Your idea about adding a cap in parallel with the lower part of the potential divider is OK but because of the resistor it will need to be a fairly large cap. Fit one anyway, about 1uF but instead of feeding that point direct to the PICAXE input, feed it via a 10k resistor and another 1uF cap on the ADC input. That will offer significantly better filtering than just one bigger cap.

In your code, simply check the voltage, if its too low, increase the duty, if it's too high, decrease the duty. That's all you need to do. It's called &quot;integral only&quot; control and will be fine for controlling a fixed load such as a lamp.

Hmm.. not so clear now I've read it back.
Make a loop.
in the loop check the voltage.
If too low then
increment duty by fixed amount
else
decrement duty by fixed amount
end if

you might want to add some extra bits and peices to prevent the duty from over or under flowing but the overall idea is simple enough.

Edited by - beaniebots on 03/09/2007 19:25:29

#### boriz

##### Senior Member
The way you describe it, the capacitor will be charged by the lamp and discharged by the MOSFET. Not good.

Try something like this:<A href='http://www.zen86415.zen.co.uk/pwm.gif' Target=_Blank>External Web Link</a>

If you&#8217;re getting too much ripple, just make C1 bigger. R1 and R2 together form a potential divider that will limit the maximum voltage on C1 to 6v. Note: The voltage on C1 varies inversely to the lamp brightness. IE. When the lamp is fully on, V(C1) will be zero, and when the lamp is fully off, V(C1) will be 6v.

#### Dippy

##### Moderator
I assume in Boriz's schematic that V1 should read &quot;V1 (Battery Pack)&quot;.

I'm a lazy so&amp;so, so I was wondering the need for feedback? A 12V halogen bulb is usually aimed at being used on '12V' lead acid i.e. &gt; 12V.
Even so, you can do some easy calcs to see what PWM is required to 'reduce' 14.8 to 12V.

But (I hear you say) the battery droops and PWM needs changing..
Aha, I say, why not monitor the battery instead and just to be a smart you could have a low battery warning too. And it would be a smoother thing to ADC.

#### BeanieBots

##### Moderator
boriz, &quot;The way you describe it, the capacitor will be charged by the lamp and discharged by the MOSFET. Not good.&quot;
Thats's EXACTLY what your circuit does so I don't understand your comment.
The circuit I described is the same as yours but with another cap on the ADC input.

Dippy, I agree that having feedback is questionable but the more I think about it the more I think it is the easier option from a coding perspective if 'constant' voltage is the desired objective.
Monitoring battery voltage is an essential and needs to be done anyway so you could well have a point.

#### inglewoodpete

##### Senior Member
Dippy's point, a predictive system, is OK once you've got a working system.

However, you will still need a development stage where PWM is controlled by feedback from the average output voltage. Data from the feedback, PWM values and battery voltage can then be used to develop a predictive system.

In the meantime, you lose interest and remain with the closed-loop development system ;o) After all, it works!

#### Dippy

##### Moderator
Ooops, only just noticed about 'dipped beam'.
Back to Plan A.