{"id":3658,"date":"2025-07-31T09:05:46","date_gmt":"2025-07-31T09:05:46","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3658"},"modified":"2025-07-31T09:05:48","modified_gmt":"2025-07-31T09:05:48","slug":"stm32-timers-applications-servo-motor-control","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3658","title":{"rendered":"STM32 Timers Applications: Servo Motor Control"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-1024x683.png\" alt=\"\" class=\"wp-image-3659\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-1024x683.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-300x200.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-768x512.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-1150x767.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-750x500.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-400x267.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM-250x167.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/ChatGPT-Image-Jul-31-2025-at-11_43_02-AM.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Controlling a servo motor is a fundamental task in embedded systems, often used in robotics, automation, and mechatronics to achieve precise angular positioning. In this section, we will explore how to use an STM32 timer to generate the PWM signal required to drive a standard hobby servo motor.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Introduction.<\/li>\n\n\n\n<li>Connection.<\/li>\n\n\n\n<li>STM32CubeIDE configuration.<\/li>\n\n\n\n<li>Firmware development.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction:<\/h2>\n\n\n\n<p>A&nbsp;<strong>servomotor<\/strong>&nbsp;is a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Rotary_actuator\">rotary actuator<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Linear_actuator\">linear actuator<\/a>&nbsp;that allows for precise control of angular or linear position, velocity and acceleration.<sup><a href=\"https:\/\/en.wikipedia.org\/wiki\/Servomotor#cite_note-1\">[1]<\/a><\/sup>&nbsp;It consists of a suitable motor coupled to a sensor for position feedback. It also requires a relatively sophisticated controller, often a dedicated module designed specifically for use with servomotors. (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Servomotor\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia link<\/a>).<\/p>\n\n\n\n<p>The servo motor used in our guide has three wires: red, brown and orange.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Wire color<\/td><td>Function<\/td><\/tr><tr><td>Red<\/td><td>Vcc (4~6V)<\/td><\/tr><tr><td>Brown<\/td><td>GND<\/td><\/tr><tr><td>Orange<\/td><td>Signal<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"284\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-1024x284.png\" alt=\"\" class=\"wp-image-393\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-1024x284.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-300x83.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-768x213.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-1150x319.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-750x208.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-400x111.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM-250x69.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.35.58-AM.png 1370w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">1.2. Construction of Servo Motor:<\/h2>\n\n\n\n<p>The construction of the servo motor is shown in the figure below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"500\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Servo-motor-constructons.png\" alt=\"\" class=\"wp-image-2365\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Servo-motor-constructons.png 900w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Servo-motor-constructons-300x167.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Servo-motor-constructons-768x427.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Servo-motor-constructons-750x417.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Servo-motor-constructons-400x222.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Servo-motor-constructons-250x139.png 250w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p>Typically, the servo motor is consist of the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Drive great to decrease the rotation speed and increase the torque output of the motor.<\/li>\n\n\n\n<li>Feedback in form of potentiometer or encoder to get the current position of the putout shaft.<\/li>\n\n\n\n<li>Integrated circuit which compares the desired angle with the current angle and adjust the position accordingly.<\/li>\n\n\n\n<li>Motor which is responsible for the movement of the servo motor and it could be either DC or AC.<\/li>\n<\/ul>\n\n\n\n<p>In order to control the angle of the servo motor, a PWM signal of frequency 50Hz (20mS period) needed to be applied to the signal pin on the servo motor. to determine the angle of the servo as following:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Angle&nbsp;<\/td><td>Duty Cycle (Period)<\/td><\/tr><tr><td>0<\/td><td>5% (1mS)<\/td><\/tr><tr><td>90<\/td><td>7.5% (1.5mS)<\/td><\/tr><tr><td>180<\/td><td>10%(2mS)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"520\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-1024x520.png\" alt=\"\" class=\"wp-image-394\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-1024x520.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-300x152.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-768x390.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-1536x780.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-1150x584.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-750x381.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-400x203.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM-250x127.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.44.51-AM.png 1560w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>However, in reality, the servo requires signal duration of 0.65ms for 0 position and 2.4ms for 180 degree position with frequency of 50Hz.<\/p>\n\n\n\n<p>In this guide, we shall use DC version commonly used by RC vehicles and drones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Connection:<\/h2>\n\n\n\n<p>In this guide, we need the following<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>STM32F411 Nucleo<\/li>\n\n\n\n<li>SG90 microservo<\/li>\n\n\n\n<li>Hock-up wires<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-900x1024.png\" alt=\"\" class=\"wp-image-395\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-900x1024.png 900w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-264x300.png 264w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-768x873.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-1351x1536.png 1351w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-1150x1308.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-750x853.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-400x455.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM-250x284.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-09-at-7.41.22-AM.png 1370w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Servo Motor<\/td><td>STM32F411 Nucleo-64<\/td><\/tr><tr><td>Red<\/td><td>5V<\/td><\/tr><tr><td>Brown<\/td><td>GND<\/td><\/tr><tr><td>Orange<\/td><td>PA0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3. STM32CubeIDE Configuration:<\/h2>\n\n\n\n<p>We shall setup timer in PWM. For how to configure timer in PWM mode, please refer to this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=3363\" data-type=\"link\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=3363\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>This guide shall continue from there.<\/p>\n\n\n\n<p>From the PWM guide, open .ioc file as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-650x1024.jpg\" alt=\"\" class=\"wp-image-3660\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-650x1024.jpg 650w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-190x300.jpg 190w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-768x1210.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-975x1536.jpg 975w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-750x1181.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-400x630.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44-250x394.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-36-44.jpg 1054w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, configure TIM2 as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prescaler to 16-1.<\/li>\n\n\n\n<li>Counter Period to 20000-1.<\/li>\n\n\n\n<li>Pulse to 1000-1.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"906\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-1024x906.jpg\" alt=\"\" class=\"wp-image-3661\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-1024x906.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-300x265.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-768x679.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-1150x1017.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-750x664.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-400x354.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15-250x221.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/07\/2025-07-31_11-39-15.jpg 1180w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>By this configuration, the timer shall generate 50Hz PWM signal which needed to control the servo. With Pulse width of 1000-1, the initial position will be 0.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the STM32CubeIDE configuration. Save the project and the project shall be generated.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Firmware Development:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Once the project has been generated, the main.c file shall be open.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c in user code begin 0, declare the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int32_t map(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max)\n{\n    return (x - in_min) * (out_max - out_min) \/ (in_max - in_min) + out_min;\n}<\/pre><\/div>\n\n\n\n<p>This function will take a value of x, with minimum and maximum input output values and scale it according to the desired value.<\/p>\n\n\n\n<p>Next, a function to control the servo angle as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void servoAngle(uint16_t angle)\n{\n\tif(angle&gt;180)\n\t{\n\t\tangle=180;\n\t}\n\n\t\/\/ Map angle [0-180] to pulse [1000-2000]\n\tuint16_t pulse_length = map(angle, 0, 180, 900, 2500);\n\n\t__HAL_TIM_SET_COMPARE(&amp;htim2, TIM_CHANNEL_1, pulse_length);\n}<\/pre><\/div>\n\n\n\n<p>First, the function shall check if the angle is beyond 180 and set it to 180.<\/p>\n\n\n\n<p>After that, convert the angle into duty cycle using the map function which declared previously and update the duty cycle of the PWM using __HAL_TIM_SET_COMPARE macro.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 2 in main function, start the timer in PWM mode as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_TIM_PWM_Start(&amp;htim2, TIM_CHANNEL_1);<\/pre><\/div>\n\n\n\n<p>In user code begin 3 in while loop, loop through all the angles as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t  for (int i=0;i&lt;180;i++)\n\t  {\n\t\t  servoAngle(i);\n\t\t  HAL_Delay(50);\n\t  }\n\t  for (int i=0;i&lt;180;i++)\n\t  {\n\t\t  servoAngle(180-i);\n\t\t  HAL_Delay(50);\n\t  }<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the firmware, save the project, build and run it on your board as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"28\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-1024x28.jpg\" alt=\"\" class=\"wp-image-3329\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-1024x28.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-300x8.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-768x21.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-1536x43.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-1150x32.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-750x21.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-400x11.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4-250x7.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2024-07-12_15-03-3-2-4.jpg 1734w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Once you run you code and attach servo motor according to the connection, you should get the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Servo Control using STM32\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/C01XrTcBo3I?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note: if you have bigger servo motor, you need to use external 5V power supply to provide power to the motor and connect a common ground between your MCU and power supply. (mine is small and not loaded and charger able to provide up to 3A)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Controlling a servo motor is a fundamental task in embedded systems, often used in robotics, automation, and mechatronics to achieve precise angular positioning. In this section, we will explore how to use an STM32 timer to generate the PWM signal required to drive a standard hobby servo motor. In this guide, we shall cover the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-3658","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3658"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3658"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3658\/revisions"}],"predecessor-version":[{"id":3662,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3658\/revisions\/3662"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}