Interface Stepper motor with STM32

Description

A stepper motor divides full rotation into number of equal steps. It finds great application in the fields of Robotics. Today I will explain you how to interface stepper motor with STM32.

Check out the Video to see the working..

Stepper motors generally comes along with an IC ULN2003. This IC is used to drive motor because microcontroller pins are unable to provide sufficient current to drive these motors. There are three different types of stepping modes used for stepper motors:-

  • Wave Drive
  • Full Drive
  • Half Drive

Wave Drive

In this mode only one stator electromagnet is energized at a time. It has the same number of steps as the full step drive.

Full Drive

In this mode two stator electromagnets are energized at a time and the motor runs at full torque.

Half drive 

In this stepping mode, one and two phases are energized alternatively. This mode is used to increase the angular resolution of the motor but the torque is reduced.

CONNECTIONS

PA0  —->    IN1

PA1  —->    IN2

PA4  —->    IN3

PB0  —->    IN4

WORKING

Wave drive is the simplest way to drive the motor and to explain the working of the stepper motor. So I am going to use wave drive to explain how this motor works and how to program it.

According to the figure above (wave drive), we can see that in order to rotate motor we need to turn one pin HIGH at a time. This is called a step. And the combination of those 4 steps is called a sequence. For the motor to complete a full 360 degree rotation, 2048 steps are required in Wave drive and Full drive. That means we need to give 2048/4 = 512 sequences.

However in Half drive, the motor require 4096 steps. But the sequence have 8 steps now (shown in the fig above) so we need to give 4096/8 = 512 sequences. 

Here is the code for wave drive 

  while (1)
  {

	for (int i=0; i<512; i++)   // for 360 deg rotation
	{
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_SET);   // IN1 HIGH
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (2);
		
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_SET);   // IN2 HIGH
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (2);

		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_SET);   // IN3 HIGH
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (2);
		
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_SET);    // IN4 HIGH
		
		HAL_Delay (2);		
	}
		
		HAL_Delay (1000);
  }

This was for one complete rotation. Now let’s say we want to rotate the motor by a particular angle i.e 10 deg. All you need to do is math so for 10 deg rotation, we need (10/(360/512)) sequences i.e 14 sequences. 

Also if you want to vary the RPM, you need to change the time delay between steps.

The following is the code for HALF DRIVE

int main(void)
{

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

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

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
		for (int i=0; i<512; i++)
		{
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (1);
		
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (1);

		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (1);

		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (1);
		
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
		
		HAL_Delay (1);		
		
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
		
		HAL_Delay (1);			
		
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
		
		HAL_Delay (1);			
		
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin (GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
		
		HAL_Delay (1);			

	}
		
		HAL_Delay (1000);
  }
  /* USER CODE END 3 */

}

 

Video

 

DOWNLOAD

You can buy me a coffee sensor 🙂

download the CODE below

, , , ,

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
adminhhh Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
hhh
Guest
hhh

your mathematic ist wrong

Menu