Follow our illustrated blog on Embedded Software Architecture

How to build a hybrid solar/wind energy harvester?

Pulse Width Modulation

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

A short definition

Pulse-width modulation is commonly used in power-control or motor-control applications. Time is divided into periods. As always, the smaller the time periods, the higher the frequency. Within this time block a pulse signal is applied. The digital signal is logically active (“1”or ON) during a specified percentage of the time period, the other time it is logically inactive (“0” or OFF). The active or high period is the so-called duty-cycle.


So, let’s say we divide time in blocks of 1ms. The frequency is thus 1/0.001s=1000Hz=1KHz.

If, within this block of 1ms, the pulse is 500µs active (high) and then 500µs inactive, we establish that the duty-cycle of the PWM signal is 50%.

If for the same time block of 1ms, the pulse is 300µs active (high) and then 700µs inactive, the duty-cycle is 30%.

Duty cycle

The term duty cycle describes the proportion of ‘on’ time to the regular interval or ‘period’ of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.”1

A 1KHz frequency is usually not enough for power control and it must be higher, maybe 20, 50 or even above 100KHz but it depends also on the used electronic switch components, possible additional electronic filters and capacitors.


PWM in micro-controllers can be further defined by the way where the pulse start, end or center is located in the time block:

  • the pulse start is at the begin edge of the time block.
  • the pulse end is at the end of the time block.
  • the pulse center is in the center of the time block and changing the duty cycle implies an (equal) change in pulse edge at the start and at the end.

PWM driver for the energy harvester

Pulse Width Modulation will be used in the Energy Harvester Project to control (relatively) high power “current flows”. Our MCU has a couple of timers with PWM-capable output. The driver design is relatively concise and simple. And it does not use all the whistles and bells the timers are capable off. But we want to keep it generic to be easily portable to other MCUs or platforms.


/*! PWM output polarity as defined as either active high (default)
or active low (if this is supported by the specific driver). */

enum pwm_output_polarity {
    PWM_OUT_HI = 0,

/*! PWM output defined by MCU pin and its output polarity */
typedef struct pwm_output {
    PIN pin; /* you can use pin e.g. PC6 or output e.g. PWM3_OUT1 */
    enum pwm_output_polarity active;
} pwm_output_t;


Initialization of PWM outputs

/*! Initialize (a) pwm(s) with a mode and a period,
 * pwm_output_t* output array ends with a last element
 * containing PWM_OUTPUT_INVALID,
 * if the period_in_KHz is not supported,
 * the function will return FALSE */

BOOLEAN pwm_init(uint16_t period_in_KHz,
    const pwm_output_t* output);

Setting the duty cycle on a single PWM output

/*! Set duty cycle on a single PWM output. */
BOOLEAN pwm_setdutycycle(const pwm_output_t* output,
    uint8_t dutycycle);

Setting the duty cycle on several PWM outputs in one go

/*! Set duty cycles, pwm_output_t* output array
 * ends with a last element
 * containing PWM_OUTPUT_INVALID,
 * there must be at least the same amount of
 * dutycycles elements
 * as there are pwm_output_t* output elements */

BOOLEAN pwm_setdutycycles(const pwm_output_t* output,
    const uint8_t* dutycycles);

A short demo

Nothing says more than a short demonstration. We are using a STM32 value line discovery board (at the bottom) that it is powered by the hidden-from-the-video PC usb port on the right. The PWM output is connected to a mosfet driver board (at the top). This mosfet regulates the 12V (provided by a lab power supply somewhere on the left) towards an eye-blinding 50W halogen lamp. The PWM duty cycle continuously changes quickly  from 0 to 100% and back.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
About is a joined initiative from Gert Boddaert and Pieter Beyens.
More info: about us, our mission, contact us.

Speak Your Mind