0:07
hello everyone welcome to controllers
0:11
tech I have already made a video on
0:14
stepper motor but that one only shows
0:17
how to rotate the motor regardless of
0:19
the speed angle or the direction today
0:23
in this video I am going to make it a
0:26
bit more interesting this is another
0:29
video on stepper motor and this time we
0:31
will vary the RPM change the direction
0:34
and also step the motor through some
0:36
angles let's start by creating project
0:49
I am using f103 c8 controller let's give
0:58
a name to this project let's set up the
1:11
cube MX part first of all I am enabling
1:15
the external crystal for the clock next
1:19
select the serial wire debug now I am
1:23
setting PA 1 to PA 4 as output these
1:27
pins are connected to the stepper I will
1:31
show you the connection in a while let's
1:34
set the clock now I want the controller
1:37
to run at maximum clock possible here in
1:41
this case 72 megahertz
1:44
note that a PB 2 timer clock is also at
1:47
72 megahertz setting the timer for
1:56
microseconds delay if you don't know
2:00
check the video on the top right
2:08
setup is complete click Save to generate
2:21
this is the connection stepper motor is
2:25
connected via uln 2003 motor driver to
2:28
the f103 controller pin a1 goes to in-1
2:33
a2 2i n 2 and so on motor driver is
2:41
needed because the current consumption
2:44
is stepper is high let's write the code
3:07
first of all I am writing a function to
3:10
create delay in microseconds set the
3:13
counter to zero wait for the counter to
3:21
reach the entered value
3:28
now I am going to write a function for
3:31
the half drive for the motor as shown in
3:38
my website the motor takes 8 steps in
3:41
the half drive first only a is high then
3:45
a and B both are high then only B is
3:48
high then B and C are high and so on
3:51
here a B C and D represents the pins
3:56
connected to the motor
4:14
here only first is hi here first and
4:17
second both are high than only second
4:19
than second and third and so on
4:43
now the motor takes 4,096 steps in half
4:48
drive to complete one revolution eight
4:51
steps in half drive is called a sequence
4:55
this way the motor completes 512
4:58
sequences for one revolution I am
5:06
defining here the number of steps this
5:08
motor takes for one revolution if you
5:11
are using wave drive or full drive then
5:14
steps per revolution would be 2048
5:17
but the sequences would still be 512 as
5:20
the motor now takes 4 steps to complete
5:23
a sequence this here is the function to
5:43
set the RPM for the motor I have tested
5:47
it and found out that maximum rpm for
5:50
this stepper is around 14 although it
5:53
misbehaves at 14 so I wrote here the max
6:07
you can use this function as a delay
6:31
now inside the main function first we
6:34
have to start the timer
7:00
let's see the calculation if I want to
7:04
run the motor at rpm of 14 the delay
7:07
would be 1046 microseconds
7:18
to run at one revolution per minute the
7:22
delay would be fourteen thousand six
7:24
hundred and forty eight microseconds
7:35
as I mentioned 512 sequences are needed
7:39
for one complete revolution
7:51
I am going to write a for loop for these
7:53
512 sequences for each sequence the
7:58
motor is going to take 8 steps making it
8:01
4096 steps let's set the RPM of 5 at
8:24
so there is no error in the code let's
8:27
debug it then select stm32 application
8:36
leave everything default and click OK
8:54
as you can see the motor is rotating as
8:57
expected you can time this video and
9:01
count the RPM this part is exact one
9:04
minute long so it is easier to count it
9:51
the stepper made 5 revolutions as we
9:54
asked it to do let's increase the RPM
9:58
now I am making it 13 flash the code
10:03
again note that the speed has increased
10:13
I am only running this part for 30
10:16
seconds if you want to count
10:42
the motor completed around six and a
10:44
half revolutions in 30 seconds exactly
10:47
what we needed now it's time to write
10:57
the function for the angle the motor
11:03
will rotate by the angle in the
11:05
parameter we can also control the
11:08
direction and rpm like I mentioned there
11:16
are 512 sequences for one revolution
11:19
which means zero point seven zero three
11:21
one two five degrees per sequence we can
11:24
find the angle the number of sequences
11:26
motor needs to complete
11:40
we can also control the direction by
11:43
reversing sequence of the steps in the
11:45
half drive let's test it for the
12:04
Here I am writing 45 degrees first and
12:07
clockwise direction with an RPM of 10
12:13
and it will keep repeating every one
12:26
let's build it and test
12:39
I am steeping through this code to show
12:42
you the angle calculation now when we
12:46
enter the function the angle is 45
12:57
number of sequences come out to be 64
13:00
this is 1/8 of the 512 as 45 is of 360
13:06
let's run it now you can see the result
13:18
yourself the motor rotates for 45
13:22
degrees every 1 second
13:43
let's change the angle to twenty two
13:45
point five degrees and flash the code
13:48
again I will change the RPM to twelve
14:04
as expected the motor now takes 22.5
14:10
degrees in each second this is it guys I
14:24
hope you will make some better use of it
14:28
leave comments in case of any doubt you
14:32
can download the code from the link in
14:34
the description have a nice day