Skip to playerSkip to main content
In this tutorial, we go beyond basic stepper motor rotation and explore speed, angle, and direction control with STM32. Using the STM32F103C8 (Blue Pill), ULN2003 driver, and CubeIDE, we’ll configure timers for microsecond delays, set motor RPM, rotate through specific angles, and even reverse direction. Perfect for embedded systems and robotics projects!

🔗 Download code here: [https://controllerstech.com/interface-stepper-motor-with-stm32/]

Related videos:
• STM32 Microseconds Delay : https://youtu.be/SqC0IhLKJ9o
• STM32 Servo Motor Tutorial : https://youtu.be/WMS0t9WGqVw

STM32 Timer Tutorial Series : https://www.youtube.com/playlist?list=PLfIJKC1ud8gjLZBzjE3kKBMDEH_lUc428
STM32 Tutorial Playlist : https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd

#STM32 #StepperMotor #ULN2003 #CubeIDE #EmbeddedSystems #Microcontroller #Motors #Robotics #STM32Tutorial

Category

🤖
Tech
Transcript
00:00Hello everyone. Welcome to Controllers Tech. I have already made a video on stepper motor,
00:16but that one only shows how to rotate the motor, regardless of the speed, angle, or the direction.
00:23Today, in this video, I am going to make it a bit more interesting.
00:28This is another video on stepper motor, and this time we will vary the RPM, change the direction,
00:34and also step the motor through some angles. Let's start by creating project in Cube IDE.
00:41I am using F103C8 controller.
00:53Let's give a name to this project.
01:00Let's set up the Cube MX part.
01:13First of all, I am enabling the external crystal for the clock.
01:18Next, select the serial wire debug.
01:22Now, I am setting PA1 to PA4 as output.
01:26These pins are connected to the stepper.
01:30I will show you the connection in a while.
01:34Let's set the clock now.
01:36I want the controller to run at maximum clock possible.
01:40Here in this case, 72 MHz.
01:45Note that APB2 timer clock is also at 72 MHz.
01:49Set up the timer for microseconds delay.
01:58If you don't know what I am doing, check the video on the top right.
02:09Set up is complete.
02:11Click save to generate the project.
02:13This is the connection.
02:24Stepper motor is connected via ULN 2003 motor driver to the F103 controller.
02:32Pin A1 goes to IN1, A2 to IN2 and so on.
02:36Motor driver is needed because the current consumption is stepper is high.
02:46Let's write the code now.
02:47First of all, I am writing a function to create delay in microseconds.
03:13Set the counter to zero.
03:14Wait for the counter to reach the entered value.
03:29Now, I am going to write a function for the half drive for the motor.
03:38As shown in my website, the motor takes 8 steps in the half drive.
03:42First only A is high, then A and B both are high, then only B is high, then B and C are high, and so on.
03:52Here, A, B, C and D represents the pins connected to the motor.
03:57Here, A, B, C and D.
04:13Here, A, B, C and D.
04:13Here, A, B, C and D.
04:15Here, A, B, C and D is high, here first and second both are high, then only second, then second and third, and so on.
04:22Now, the motor takes 4096 steps in half drive, to complete one revolution.
04:52Eight steps in half drive is called a sequence.
04:56This way the motor completes 512 sequences for one revolution.
05:05I am defining here, the number of steps this motor takes for one revolution.
05:11If you are using wave drive or full drive, then steps per revolution would be 2048, but
05:17the sequences would still be 512.
05:21As the motor now takes 4 steps to complete a sequence.
05:42This here is the function to set the RPM for the motor.
05:46I have tested it, and found out that maximum RPM for this stepper is around 14.
05:53Although it misbehaves at 14, so I wrote here the max can be 13.
06:08You can use this function as a delay function.
06:32Now inside the main function, first we have to start the timer.
06:39Let's see the calculation.
06:40If I want to run the motor at RPM of 14.
06:44the delay would be 1046 microseconds.
06:45If I want to run the motor at RPM of 14, the delay would be 1046 microseconds.
06:49If I want to run the motor at RPM of 14, the delay would be 1046 microseconds.
06:56To run at 1 revolution per minute, the delay would be 14648 microseconds.
07:03If I want to run at 1 revolution per minute, the delay would be 14648 microseconds.
07:10To run at 1 revolution per minute, the delay would be 14648 microseconds.
07:35As I mentioned, 512 sequences are needed for 1 complete revolution.
07:51I am going to write a for loop for these 512 sequences.
07:56For each sequence, the motor is going to take 8 steps, making it 4096 steps.
08:11Let's set the RPM of 5 at first.
08:24So there is no error in the code, let's debug it then.
08:29Select SDM32, application.
08:36Leave everything default and click OK.
08:46Run the code now.
08:55As you can see the motor is rotating as expected.
08:58You can time this video and count the RPM.
09:02This part is exact 1 minute long, so it is easier to count it.
09:07Let's watch.
09:17Let's watch.
09:18It is not easier for us to record.
09:20Let's do this.
09:23Let's get started.
09:26Time to record this video.
09:28It's a Simple Maya Pilum.
09:31The stepper made 5 revolutions, as we asked it to do.
09:57Let's increase the RPM now.
10:00I am making it 13.
10:03Flash the code again.
10:11Note that the speed has increased.
10:14I am only running this part for 30 seconds, if you want to count.
10:21The motor completed around 6.5 revolutions in 30 seconds, exactly what we needed.
10:40Now it's time to write the function for the angle.
10:43The motor will rotate by the angle in the parameter.
11:16We have 512 sequences for 1 revolution, which means 0.703125 degrees per sequence.
11:24We can find the angle the number of sequences, motor needs to complete.
11:41We can also control the direction, by reversing sequence of the steps in the half drive.
11:47Let's test it for the different angles.
12:02Here I am writing 45 degrees first and clockwise direction, with an RPM of 10.
12:16And it will keep repeating every 1 second.
12:27Let's build it and test.
12:40I am steeping through this code, to show you the angle calculation.
12:45Now, when we enter the function, the angle is 45 degrees.
12:57Number of sequences come out to be 64.
13:01This is 1 eighth of the 512, as 45 is of 360.
13:07Let's run it now.
13:17You can see the result yourself.
13:20The motor rotates for 45 degrees, every 1 second.
13:44Let's change the angle to 22.5 degrees, and flash the code again.
13:49I will change the RPM to 12.
14:07As expected, the motor now takes 22.5 degrees in each second.
14:23This is it guys.
14:25I hope you will make some better use of it.
14:29Leave comments in case of any doubt.
14:32You can download the code from the link in the description.
14:36Have a nice day.
Be the first to comment
Add your comment

Recommended