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 ULN2003

    • breadboard

Software

  • Keil uVision, CMSIS, EC_HAL library

Problem 1: Stepper Motor

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. Use Moore FSM for this case. If you want, you may use Mealy FSM.

See ‘Programming FSM’ for hints

  • Full-Stepping Sequence

  • Half-Stepping Sequence

Problem 2: Firmware Programming

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(GPIO_TypeDef* port1, int pin1, GPIO_TypeDef* port2, int pin2, GPIO_TypeDef* port3, int pin3, GPIO_TypeDef* port4, int pin4);

//or   using ecPinNames.h 
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

Requirement

You have to program the stepping sequence using the state table. You can define the states using structures. Refer to ‘Programming FSM’ for hints.

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(GPIOC, BUTTON_PIN, FALL,0);           // External Interrupt Setting
	GPIO_init(GPIOC, BUTTON_PIN, EC_DIN);           // GPIOC pin13 initialization

	Stepper_init(GPIOB,10,GPIOB,4,GPIOB,5,GPIOB,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