Docs

Micro-controllers, wireless transmission and database

Head

L298N Motor with Nucleo STM32F446RE using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with Nucleo STM32F446RE 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=oAwZ0cjlmN8

Wiring Diagram

Diagram

STM32CubeIDE Settings

Click Clock Configuration tab → HCLK (MHz) to 180

Click Timer → Click TIM2 →

Clock Source set to Internal Clock

Channel2 set to PWM Generation CH3

Configuration → Parameter Settings →

Prescaler set to 90-1 (APB1Timer-1)

Counter Period 2000-1

Additional code on top of STM32CubeIDE generated code

  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3); //PB10 TIM2 CH3
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

    // Set the rotation direction backward
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
    // Set the speed slow
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1000);
    HAL_Delay(2000);
    // Set the speed medium
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1500);
    HAL_Delay(2000);
    // Set the speed high
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 2000);
    HAL_Delay(2000);
    // Stop the motor
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
    HAL_Delay(2000);
    // Set the rotation direction forward
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
    // Set the speed slow
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1000);
    HAL_Delay(2000);
    // Set the speed medium
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1500);
    HAL_Delay(2000);
    // Set the speed high
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 2000);
    HAL_Delay(2000);
    // Stop the motor
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
    HAL_Delay(2000);

    /* USER CODE END WHILE */

Add the second motor

Wiring Diagram

Diagram2

STM32CubeIDE Settings

Click Timer → Click TIM2 →

Channel2 set to PWM Generation CH2

Additional code on top of STM32CubeIDE generated code

  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3); //PB10 TIM2 CH3
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); //PA1 TIM2 CH2
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

    // Set the rotation direction backward
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 1);
    // Set the speed slow
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1000);
    HAL_Delay(2000);
    // Set the speed medium
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1500);
    HAL_Delay(2000);
    // Set the speed high
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 2000);
    HAL_Delay(2000);
    // Stop the motor
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
    HAL_Delay(2000);
    // Set the rotation direction forward
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
    // Set the speed slow
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1000);
    HAL_Delay(2000);
    // Set the speed medium
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 1500);
    HAL_Delay(2000);
    // Set the speed high
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3, 2000);
    HAL_Delay(2000);
    // Stop the motor
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, 0);
    HAL_Delay(2000);

    // Set the rotation direction backward
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 1);
    // Set the speed slow
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2, 1000);
    HAL_Delay(2000);
    // Set the speed medium
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2, 1500);
    HAL_Delay(2000);
    // Set the speed high
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2, 2000);
    HAL_Delay(2000);
    // Stop the motor
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 0);
    HAL_Delay(2000);
    // Set the rotation direction forward
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 0);
    // Set the speed slow
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2, 1000);
    HAL_Delay(2000);
    // Set the speed medium
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2, 1500);
    HAL_Delay(2000);
    // Set the speed high
    __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2, 2000);
    HAL_Delay(2000);
    // Stop the motor
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 0);
    HAL_Delay(2000);
    
    /* USER CODE END WHILE */