Skip to content

Open Loop PWM

The goal of this basic example is to understand what is a duty cycle and how it is related to power flow. We will use our TWIST as a buck converter in an open-loop to divide our voltage by 2.

Are you ready to start ?

Before you can run this example, you must have :

  • successfully gone through our getting started tutorial to set up your work environment
  • successfully gone through our first example tutorial and download the "Open Loop PWM" example

Background


What is a PWM ?

PWM or Pulse-Width-Modulation is based on the idea that a periodic (repetitive) logic signal (ON/OFF or 0/1) can have its width changed over time. This is shown on the three images below, where you can see the width of the \(T_{ON}\) changing from 25% to 75% of the total period.

As shown in Figure 1, the duty cycle is 25% of the total time (T), is called period.

Representation of a duty cycle of 25% Representation of a duty cycle of 25%
Figure 1: Representation of a duty cycle of 25%

In figures Figure 2 and Figure 3 you can see the duty cycle again, but for a different width. Thus, "modulating the width" in practice means changing the duty cycle.

Representation of a duty cycle of 50% Representation of a duty cycle of 50%
Figure 2: Representation of a duty cycle of 50%

Representation of a duty cycle of 75% Representation of a duty cycle of 75%
Figure 3: Representation of a duty cycle of 75%

What is a duty cycle?

The duty time is the ratio between the ON state and the period T of the logic signal. You can see it in the image above. It is defined by the Equation 1 shown below.

\[ D = \dfrac{T_{ON}}{T} \]
Equation 1: Definition of the duty cycle

What about power?

In many fields, like electrical engineering, duty cycles are important to provide power efficiently and effectively for a specific situation. Many applications of motors, physical electronics, and even music use something called pulse width modulation or PWM to control the duty cycle and the necessary applied power (which can be referred to as input voltage or current). The duty factor plays a key role in representing the ratio of on time and off time for a given power supply.

A buck power converter will output a voltage that is the duty cycle times its input voltage. This is expressed by the equation:

\[ V_{OUT} = D \cdot V_{IN}\]
Equation 2: Buck converter voltage output as a function of the voltage input and duty cycle

This gives the following output for different input voltage values.

Plot of the buck converter output (click to zoom) Plot of the buck converter output (click to zoom)
Figure 4: Plot of the buck converter output (click to zoom)

What is Deadtime?

In power electronics, switching between transistors is NOT instantaneous. To avoid short circuits, we provide a time between opening one switch and closing the other. This time is called dead time.

The deadtime can be of three formats: zero, positive or negative.

In zero deadtime, both PWM signal switches simultaneouly as shown in []

No deadtime of a PWM signal No deadtime of a PWM signal
Figure 5: No deadtime of a PWM signal

Positive deadtime of a PWM signal Positive deadtime of a PWM signal
Figure 6: Positive deadtime of a PWM signal

Negative deadtime of a PWM signal Negative deadtime of a PWM signal
Figure 7: Negative deadtime of a PWM signal

The length of the deadtime in either the rising or falling edges depends on the application.

Hardware setup and requirement


Circuit diagram

The circuit diagram of the Twist board is shown in Figure 8. The Currents are represented by arrows and both legs are active. The power flows from VHigh to VLow.

The circuit diagram of the Twist board power block (click to zoom) The circuit diagram of the Twist board power block (click to zoom)
Figure 8: The circuit diagram of the Twist board power block (click to zoom)

Wiring diagram

The wiring diagram is shown in Figure 9.

Hardware pre-requisites

You will need :

  • 1 TWIST board
  • A dc power supply (20-60V)
  • A resistor (or a dc electronic load)
  • A USB-C cable

The wiring diagram of this example setup (click to zoom) The wiring diagram of this example setup (click to zoom)
Figure 9: The wiring diagram of this example setup (click to zoom)

Once you have wired the cables are connected your TWIST board to your computer, you can upload the example code.

Is you software ready?

Before you can run this example, you must have :

  • successfully gone through our getting started tutorial to set up your work environment
  • successfully gone through our first example tutorial and download the "Open Loop PWM" example

Main code structure


Code structure

Let's briefly explain the conde structure of the main.cpp file of this example.

Figure 10 shows a general representation of how the code is structured.

Oveview of the code structure (click to zoom) Oveview of the code structure (click to zoom)
Figure 10: Oveview of the code structure (click to zoom)

The code structure is as follows:

  • On the top of the code some initialization functions take place.
  • Setup Routine - initializes both legs in Buck mode, creates and starts the tasks of the example
  • Communication Task - Handles the keyboard communication and decides which MODE is activated and if the duty cycle is incremented or decremented by 0.01
  • Application Task - Handles the MODE, activates the LED and prints the duty cycle value in the serial port
  • Critical Task - Handles the MODE, sets power ON/OFF and writes the duty cycle value to the PWM output

Task timing diagram

The firmware of the TWIST board is executed according to the diagram in Figure 11.

  • Communication Task - Is awaken regularly to verify any keyboard activity
  • Application Task - This task is woken once its suspend is finished. By default its period if of 100 miliseconds.
  • Critical Task - This task is driven by the HRTIM count interrupt, where it counts a number of HRTIM switching frequency periods. In this case 100us, or 20 periods of the TWIST board 200kHz switching frequency set by default.

Timing diagram of the tasks Timing diagram of the tasks
Figure 11: Timing diagram of the tasks

Control scheme

There is no control scheme in this example. By default it is in Open-loop.

Expected result


Build and Upload

  • Build and Upload (+ ).

  • In the bottom toolbar, click on the Serial Monitor icon . Then click on the serial monitor screen.

This code will control duty_cycle so that the voltage output will vary. You can control the duty cycle through platformio serial monitor.

When opening it for the first time, the serial monitor will give you an initialization message regarding the parameters of the ADCs as shown below.

serial monitor initialization
Figure 12: serial monitor initialization

Commands keys

Here are the command keys programmed by default in this example:

  • press p to go into POWERMODE and turn on the output
  • press i to go into IDLEMODE and stop the power flow
  • press u to increase the duty cycle by 0.05
  • press d to decrease the duty cycle by 0.05
  • press h to show the help menu

Here's sequence when the help menu is activated with h, the power mode is then activated with p, duty cycle is raised up to 0.5 and finally the Twist converter is put in idle with the i.

Serial monitor output of the sequency above
Figure 13: Serial monitor output of the sequency above

You can measure the DC output voltage thanks to an oscilloscope or a multimeter.

That’s it!

Contributors
  • 2021.11.04: Romain Delpoux, Loïc Quéval, Adrien Prévost
  • 2021.11.07: Luiz Villa, Antoine Boche
  • 2022.01.24: Luiz Villa, Loïc Quéval
  • 2022.02.01: Luiz Villa
  • 2022.03.13: Luiz Villa
  • 2022.06.23: Loïc Quéval
  • 2023.07.10: Luiz Villa
  • 2023.10.09: Mathilde Longuet
  • 2024.02.25: Luiz Villa, Ayoub Farah
  • 2025.04.24: Luiz Villa
  • 2025.05.15: Luiz Villa