LAB: Stepper Motor

Date: 2023-09-26

Author/Partner:

Github: repository link

Demo Video: Youtube link

PDF version:

Introduction

In this lab, we will learn how to drive a stepper motor with digital output of GPIOs of MCU. You will use a FSM to design the algorithm for stepper motor control.

You must submit

  • LAB Report (*.pdf & *.md)

  • Zip source files(main*.c, ecRCC.h, ecGPIO.h, ecSysTick.c etc...).

    • Only the source files. Do not submit project files

Requirement

Hardware

  • MCU

    • NUCLEO-F411RE

  • Actuator/Sensor/Others:

    • 3Stepper Motor 28BYJ-48

    • Motor Driver A4988 (tutorial)

    • Motor Driver ULN2003 (lab)

    • breadboard

Software

  • Keil uVision, CMSIS, EC_HAL library


Tutorial: STM-Arduino

We are going to create a simple program that runs a stepper motor using A4988 motor driver See here for A4988 spec sheet

Here, you only need to give pulses to the driver. You can also change the microstepping setup.

Procedure

  1. Connect the motor driver and the stepper motor as follows.

Supply 5~8 [V] to the driver using a power supply.

  1. Create a new project under the directory \EC\LAB\STEPPER

  2. Open Arduino IDE and Create a new program named as ‘TU_arduino_Stepper.ino’.

  3. Write the following code.

  4. upload and run.

Press the reset button(black) and verify the operation.

// Run Stepper Motor with A4998  

int x; 
#define BAUD (9600)


void setup() 
{
  Serial.begin(BAUD);
  pinMode(6,OUTPUT); // Enable
  pinMode(5,OUTPUT); // Step
  pinMode(4,OUTPUT); // Dir
  digitalWrite(6,LOW); // Set Enable low
}

void loop() 
{
  digitalWrite(6,LOW); // Set Enable low
  digitalWrite(4,HIGH); // Set Dir high
  Serial.println("Loop 200 steps (1 rev)");
  for(x = 0; x < 200; x++) // Loop 200 times
  {
    digitalWrite(5,HIGH); // Output high
    delay(10); // Wait
    digitalWrite(5,LOW); // Output low
    delay(100); // Wait
  }
  Serial.println("Pause");
  delay(1000); // pause one second
}

Tutorial: FSM

You have to program the stepping sequence using the state table. You can define the states using structures.

Read Tutorial: FSM programming for hints

// State number
typedef enum StateNum {
	S0. S1, S2, S3
} StateNum;

typedef struct State {
	uint8_t out;
	StateNum next[2];
} State_t;

State_t FSM[4] = {
	{0x9 , {S1, S3}},
	{0xA , {S2, S0}},
	{0x6 , {S3, S1}},
	{0x5 , {S0, S2}}
};

Problem : Stepper Motor with 4-input sequence

For the lab, we are going to use another stepper motor driver of ULN2003 motor driver See here for ULN2003 spec sheet)

Here, you have to give 4-input pulses in sequence.

Hardware Connection

Read specification sheet of the motor and the motor driver for wiring and min/max input voltage/current.

Stepper Motor Sequence

We will use unipolar stepper motor for this lab

Fill in the blanks of each output data depending on the below sequence.

Full-stepping sequence

Half-stepping sequence

Finite State Machine

Draw a State Table for Full-Step Sequence. You can choose either Moore FSM or Mealy.

  • Full-Stepping Sequence

  • Half-Stepping Sequence

You have to program the stepping sequence using the state table. You can define the states using structures.

Read Tutorial: FSM programming for hints

// State number
typedef enum StateNum {
	S0. S1, S2, S3
} StateNum;

typedef struct State {
	uint8_t out;
	StateNum next[2];
} State_t;

State_t FSM[4] = {
	{0x9 , {S1, S3}},
	{0xA , {S2, S0}},
	{0x6 , {S3, S1}},
	{0x5 , {S0, S2}}
};

Create HAL library

Download files:

Then, change the library files as ecStepper.h, ecStepper.c

Declare and define the following functions in your library.

You must update your header files located in the directory EC \lib\.

ecStepper.h

// Initialize with 4 pins
// ( A, B,  AN,  BN)
void Stepper_init(PinName_t A, PinName_t B,  PinName_t AN, PinName_t BN);

// whatSpeed [rev/min]
void Stepper_setSpeed(long whatSpeed);

// Run for n Steps
void Stepper_step(uint32_t steps, uint32_t direction, uint32_t mode); 

// Immediate Stop.
void Stepper_stop(void);

Note that these are blocking stepper controllers. While the stepper is running, the MCU cannot process other polling commands. If you can, modify it to be the non-blocking controller.

You can also create your own functions different from the given instructions.

Procedure

  1. Create a new project under the directory \repos\EC\LAB\LAB_Stepper_Motor

    • The project name is “LAB_Stepper_Motor”.

    • Create a new source file named as “LAB_Stepper_Motor.c”

      You MUST write your name on the source file inside the comment section.

  2. Include your updated library in \repos\EC\lib\ to your project.

    • ecGPIO.h, ecGPIO.c

    • ecRCC.h, ecRCC.c

    • ecEXTI.h, ecEXTI.c

    • ecSysTick.h, ecSysTick.c

    • ecStepper.h ecStepper.h

  3. Connect the MCU to the motor driver and the stepper motor.

  4. Find out the number of steps required to rotate 1 revolution using Full-steppping.

  5. Then, rotate the stepper motor 10 revolutions with 2 rpm. Measure if the motor rotates one revolution per second.

  6. Repeat the above process in the opposite direction.

  7. Increase and decrease the speed of the motor as fast as it can rotate to find the maximum and minimum speed of the motor.

  8. Apply the half-stepping and repeat the above.

Configuration

Discussion

  1. Find out the trapezoid-shape velocity profile for a stepper motor. When is this profile necessary?

    Answer discussion questions

  2. How would you change the code more efficiently for micro-stepping control? You don’t have to code this but need to explain your strategy.

    Answer discussion questions

Code

Your code goes here: ADD Code LINK such as github

Explain your source code with necessary comments.

// YOUR MAIN CODE ONLY
// YOUR CODE

Sample Code : Stepper Motor

#include "stm32f411xe.h"
#include "ecGPIO.h"
#include "ecRCC.h"
#include "ecEXTI.h"
#include "ecSysTick.h"
#include "ecStepper.h"

void setup(void);
	
int main(void) { 
	// Initialiization --------------------------------------------------------
	setup();
	
	Stepper_step(2048, 1, FULL);  // (Step : 2048, Direction : 0 or 1, Mode : FULL or HALF)
	
	// Inifinite Loop ----------------------------------------------------------
	while(1){;}
}

// Initialiization 
void setup(void){
	
	RCC_PLL_init();                                 // System Clock = 84MHz
	SysTick_init();                                 // Systick init
	
	EXTI_init(BUTTON_PIN, FALL,0);           // External Interrupt Setting
	GPIO_init(BUTTON_PIN, EC_DIN);           // GPIOC pin13 initialization

	Stepper_init(PB_10,PB_4,PB_5,PB_3); // Stepper GPIO pin initialization
	Stepper_setSpeed(2);                          	//  set stepper motor speed
}

void EXTI15_10_IRQHandler(void) {  
	if (is_pending_EXTI(BUTTON_PIN)) {
		Stepper_stop();
		clear_pending_EXTI(BUTTON_PIN); // cleared by writing '1'
	}
}

Results

Experiment images and results

Show experiment images /results

Add demo video link

Reference

Complete list of all references used (github, blog, paper, etc)

Troubleshooting

(Option) You can write Troubleshooting section

Last updated