Interfacing Servo with the STM32-F103RB Nucleo Board
Introduction
Problem Statement
Requirements
To successfully complete this project, you will need the following components:
- STM32-F103RB Nucleo Board
- Servo Motor
- Breadboard
- Jumper wires
Circuit Diagram
Follow these steps to configure the .IOC file and set up GPIOs and Clock :
Open the STM32 Cube IDE and start by creating a new STM32 project. Click on the board selector tab in the target selection window.
Enter "NUCLEO-F103RB" as the commercial part number and click on "Next" after selecting the board.

- Enter the Project Name "servo_pwm" and click on "Finish". After that, click on "Yes" to initialize all the peripherals in default mode.

Pinout & Configuration: In the Pinout & Configuration menu, select "TIM2" from the Timers section. This will configure Timer 2 for generating PWM signals.
Clock Source and PWM Generation: Choose "Internal Clock" as the Clock Source to utilize the internal clock of the STM32-F103RB Nucleo Board for PWM generation. Additionally, select "PWM Generation CH1" in Channel 1. This setting enables PWM generation on channel 1, which corresponds to PA0 on the board.
Timer Configuration: In the Configuration part of the Timers section, set the Prescaler value as "960-1". The prescaler determines the frequency of the timer clock. Here, we divide the timer clock frequency by 959.
Next, specify the Counter Period as "1000-1". The counter period defines the overall period of the PWM signal. With a value of 100-1, the PWM signal will complete one cycle every 999 timer clock cycles.

- Disable the Low Speed Clock in the RCC menu, and set the High Speed Clock to Crystal/Ceramic Resonator. In the NVIC setting of RCC configuration, enable the RCC global interrupt.

After finishing the Pinout Configuration, go to the Clock Configuration menu. In the HCLK field, enter "48 MHz" as the desired frequency for the HCLK (system clock). The servo works on 50 Hz frequency, so we will use the closest that we can get. The Integrated Development Environment (IDE) will automatically search for the necessary clock sources and prescalers based on this configuration.
Next, locate the gear-like icon on the toolbar and click on it. This will add the configuration settings we did in the GUI to the code.

Code
Now, let's proceed to understand and write the C code to control the servo motor. Follow the steps below:
The Device Configuration Tool will automatically open the main.c file for you. In the project, you will find all the necessary pin and port names defined in the Core ⇾ Inc ⇾ main.h file. These definitions will help us easily reference the pins and ports in our code.
Navigate to the main function in the main.c file and paste the following code:
Note: You can download the full project code from the provided link.
int main(void)
{ /* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
htim2.Instance->CCR1 = 25; // duty cycle is .5 ms
HAL_Delay(1000);
htim2.Instance->CCR1 = 75; // duty cycle is 1.5 ms
HAL_Delay(1000);
htim2.Instance->CCR1 = 125; // duty cycle is 2.5 ms
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
After pasting the above code, ensure that you have connected your STM32-F103RB Nucleo Board to your PC/Laptop using a USB cable.
Next, click on the "Run and Debug" icon in the toolbar menu of the IDE (Refer to image below ). This will initiate the build process for your project. The IDE will compile the code. Once the build process is successful, the IDE will start flashing the compiled project to the STM32-F103RB Nucleo Board.

Output
Upon successfully running the project, the servo motor will rotate from 0 to 90 and finally to 180 degrees.
Conclusion
In this guide, we have successfully interfaced a servo motor with the STM32-F103RB Nucleo Board. By controlling the pulse width of the PWM signal. By adjusting the duty cycle values, you can program the servo motor to rotate to specific angles and perform various tasks. This opens up possibilities for applications such as robotics, automation, and remote control systems.