Skip to content

Tutorial Buck converter


  • Romain Delpoux, 2021.11.04
  • Loic Queval, 2021.11.05
  • Adrien Prévost, 2021.11.05
  • Luiz Villa, 2021.11.07
  • Antoine Boche, 2021.11.08

Tutorial Buck Converter

The goal of this tutorial is to explore how to make a buck with the O2 power converter. You will activate the buck and its associated pid parameters, which will automatically drive the duty cycle of the power converter. For this tutorial we will build on your knowledge of communication, measurement and PWM while adding the buck related elements. We will start from the Hardware Setup 2 Tutorial project.

The blocks that are active in this tutorial are shown in the figure below.


Required hardware

  • O2 v_1_1_2
  • STLinkV3
  • PC (windows or linux)
  • power supply (40 V, 2 A)

Create the project

  1. We will create the project by copying the communication project in a new branch, that will be called buck. In the terminal,
  1. On the bottom menu, check that you are now in the buck branch.


  1. Check the libraries

In src/main.cpp, check that we load the owntech.ini libraries and the OS drivers:

  1. Define the hardware by software !

In src/main.cpp, add the following code to define the variables that will be used to determine if the converter is in buck mode.

In src/main.cpp, you will find the setup_hardware() function add the following code to initialize the buck mode correctly together wit the Analog-to-Digital-Converter with two measurements, V1_low and V_high. They correspond to the low voltage and high voltage sides of the power converter respectively. The number of channels is set to two.

  1. Define the communication task and the communication menu

In src/main.cpp, replace the medu_mode by the menu_mode below to create a new BUCK control mode. This will keep the manual mode available if necessary.

In src/main.cpp, the functions setup_communication_task() remains unchanged.

The loop_communication_task() now has to change the menu to treat the buck mode case. Replace

The menu() function will have to be changed to reflect the new commands now available to the user. Add the code below to do so.

  1. Define the application task

In src/main.cpp, add the following code to define the variables that will be used by the application task to print the Vhigh and V1_low measurement values:

In src/main.cpp, in the function setup_application_task(), do not modify anything. This task will link your application task to a thread which will be executed automatically.

In src/main.cpp, in the function loop_application_task(), add the following code to define the application task. In this example, it will turn the LED on and off depending on the mode chosen by the user. When the led is ON the O2 converter will print the value of Vhigh and V1low on the Serial Monitor.

  1. Define the control task

In src/main.cpp, in the function setup_control_task(), we will add the parameters that were ignored on previous examples. The kp, ki and kd are control parameters used by the PID to track the output voltage. The last line activates the timer, launching the control task.

In src/main.cpp, in the function loop_control_task(), add the following code to define the control task loop. This loop will acquire the data we want to display, convert it from raw bit values into Volts and store it into a variable that can be accessed by the application task.

With the inclusion of the buck mode this function can now call the PID to correct the output voltage as needed. The duty cycle is then sent to the switches.

  1. Connect hardware

Now we will connect OwnTech’s O2 to the power supply and to the PC.

  • Connect the pins Vhigh and GND of the O2 to the 40 V power supply (set the current limitation at 1 A).
  • Connect the micro-JTAG connector of the O2 to the PC thanks to the STLinkV3. The leds PWR and COM should be ON.
  • Switch on the power supply. The led2 of the O2 should be ON.


  1. In the bottom menu, click on the Build icon build_icon. This will launch the compilation of the code. When the compilation is completed, you should see [SUCCESS] in the terminal.

  2. On the bottom menu, click on the Upload icon flash_icon. This will flash the compiled code on the microcontroller of the O2. When the process is completed, you should see [SUCCESS] in the terminal.

  3. On the bottom menu, click on the Serial Monitor icon serial_icon. Should print you a menu for you at first. When you press the b button on your keyboard, the output should converge to 10V.

When you press the p button on your keyboard, half of the high side voltage will appear on the low side. the LED will turn ON and Vhigh and V1low data will show on the terminal and half of Vhigh will appear at Vlow. Hit the "u" button to raise the duty cycle and the "d" button to lower it, you will see the ratio between input and output change following the buck converter behavior. Hit the i button to turn it the LED OFF and stop the data display.

Expected outputs

  • The led1 of the O2 should start as off.
  • The led1 of the O2 should turn on when you press the p button.
  • The led1 of the O2 should turn off when you press the i button.
  • The voltage ratio between Vhigh and V1low changes when you press either u or d.
  • The voltage will converge to 10V if you press b.

That’s it!