Docs

Micro-controllers, wireless transmission and database

Head

Servo Motor with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

STM32CubeIDE Settings

Click Timer → Click TIM2 →

Clock Source set to Internal Clock

Channel3 set to PWM Generation CH3

Channel4 set to PWM Generation CH4

Configuration → Parameter Settings →

Prescaler set to 15

Counter Period 9999

Click Timer → Click TIM3 →

Clock Source set to Internal Clock

Channel1 set to PWM Generation CH1

Channel2 set to PWM Generation CH2

Channel3 set to PWM Generation CH3

Channel4 set to PWM Generation CH4

Configuration → Parameter Settings →

Prescaler set to 15

Counter Period 9999

Additional code on top of STM32CubeIDE generated code

  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 250);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4, 450);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1, 650);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2, 850);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_3, 1050);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4, 1250);
      HAL_Delay(1000);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 450);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4, 650);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1, 850);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2, 1050);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_3, 1250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4, 250);
      HAL_Delay(1000);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 650);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4, 850);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1, 1050);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2, 1250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_3, 250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4, 450);
      HAL_Delay(1000);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 850);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4, 1050);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1, 1250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2, 250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_3, 450);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4, 650);
      HAL_Delay(1000);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1050);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4, 1250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1, 250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2, 450);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_3, 650);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4, 850);
      HAL_Delay(1000);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1250);
      __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4, 250);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1, 450);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2, 650);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_3, 850);
      __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4, 1050);
      HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */