#### Follow our illustrated blog on Embedded Software Architecture

How to build a hybrid solar/wind energy harvester?

# Current limiting with PWM

VN:F [1.9.22_1171]

For particular applications, such as our energy harvester or at least the battery charger part of it, the ability to control the current is an essential property. We must emphasize that the presented method will limit the average current; the instantaneous current might be higher. So, although it works well for our particular application, it might be ill suited for your application. We use PWM on mosfets to allow current to flow or not.

The duty cycle controller – as we call it – has a target duty cycle one can set, but also the currently applied duty cycle. The currently applied duty cycle might differ from the target duty cycle because

1. we just changed the target duty cycle and the applied duty cycle has not reached it yet, or
2. the applied duty cycle is limited because a current threshold has been exceeded, in which case the applied duty cycle will no longer increase, or will even decrease until current drops below the set limit.

The duty cycle controller is thus also further defined by the allowed duty cycle change rate and the (current) limit value.
Applied duty cycle is only allowed to evolve to the target duty cycle at a particular speed and is defined as a maximum change in % of the duty cycle per second. Hence, a value of 100 for the duty cycle change rate will allow the applied duty cycle to linearly change from 0% to 100% in 1 second. A value of 200, will allow this to happen in 0.5 seconds. And with a value of 50, it will take 2 seconds to go from 0% to 100% or from 100% to 0%.

The information is processed every tick by the algorithm and then the applied duty cycle is calculated: Inputs are the current time (tick) and the measured (amperage) value. Internally, the data is processed with regard to the set target duty cycle, the change rate, the set limit and the previous time the algorithm was processed:

1. calculate the time difference since the last algorithm function call
2. if that time is too small, then return without acting further.
3. Check the current limit against the measured value, decrease applied duty cycle if needed. In that case, return without acting further.
4. If applied duty cycle is different from target duty cycle, move to target duty cycle in accordance with specified duty cycle change rate.

In step 3, we also try not only to react but also to anticipate to changes. A measured current (amperage) change rate is calculated, and with that the next amperage value is estimated. If this estimated value also exceeds the limit, we already start to decrease the applied duty cycle. This type of control algorithm could be compared with a PI controller but it is somewhat simpler.

VN:F [1.9.22_1171]