0:09
hello and welcome to controller Tech few
0:12
months ago I covered a video explaining
0:15
how to interface a Servo motor with SDM
0:18
32 the motor I used in that video was a
0:22
180° servo motor which rotates between 0
0:26
180° based on the pulse of the input PW
0:30
M signal today I have with me another
0:33
kind of Servo motor a continuous
0:36
Servo as the name itself suggests This
0:40
Server rotates continuously throughout
0:43
360° and we can control its RPM by
0:46
varying the width of the input pwm
0:49
signal here I have both kinds of Servo
0:52
motors with me you can purchase these
0:55
from the controller's teex
0:57
store both the motors look identical
1:00
with literally no difference from a
1:02
visual point of view you have to rely on
1:06
sellers information about which one is
1:09
180° and which one is
1:12
continuous some of these motors do come
1:14
with the label on them but not all have
1:17
it here I have a Servo tester which will
1:20
be used to test these Servo
1:23
Motors the tester has three different
1:26
modes available to test the servo motor
1:29
this select button can be used to switch
1:32
modes I will use the manual mode as it
1:36
is used to vary the width of the pwm
1:39
signal this Servo tester can be used to
1:42
test up to three Servo Motors
1:46
simultaneously this is the output end of
1:48
the tester and we will connect the motor
1:51
here the motor has three wires red is
1:54
the VCC Brown is the ground and yellow
1:58
is the signal let's connect the wires to
2:03
pins I am connecting the second Servo
2:06
also and we will see how both these
2:08
servos behave with the pwm signal
2:12
towards the input of the tester I have
2:15
connected the VCC pin to the 5vt battery
2:18
and ground to ground of the battery
2:21
let's rotate the knob to vary the width
2:23
of the pwm signal you can clearly see
2:27
the behavior of both servos when they
2:29
are provided with same signal the
2:33
180° Servo changes its position based on
2:36
the width of the pwm signal whereas The
2:39
Continuous rotation Servo changes the
2:42
RPM also note the direction of rotation
2:45
of the continuous rotation
2:47
Servo when the knob is at extreme end
2:50
the continuous Servo is rotating at high
2:55
Direction when the knob is rotated the
2:57
RPM starts decreasing and and the motor
3:00
stops when the knob is at the center at
3:05
180° Servo is also rotated through
3:09
90° and now when the knob continues to
3:12
rotate the RPM of the continuous Servo
3:15
is increasing again also the
3:19
180° Servo rotates through another 90°
3:26
rotation as I mentioned there are other
3:29
modes available on the Tester the
3:31
neutral mode sends a pulse with the
3:33
width of 1 milliseconds whereas the auto
3:36
mode changes the width very fast and we
3:39
see an output like this let's see the
3:44
analyzer here I have a logic analyzer
3:47
and I will connect the signal pin from
3:49
the tester output to the channel one of
3:53
analyzer let's rotate the knob to one
3:56
extreme end and check the pulse on the
4:04
the frequency of the signal is 50 htz
4:07
which is standard for any Servo motor
4:10
note that the pulse width is 880
4:13
micros now I am rotating the knob
4:16
towards the middle till the point where
4:18
the servo stops rotating the pulse width
4:21
is 1.5 milliseconds at this point now I
4:25
am rotating it to the other end
4:31
here we have the width of 2.1
4:34
milliseconds so we saw the RPM and
4:37
direction of the motor varied by varying
4:39
the width of the pwm signal between 0.9
4:45
milliseconds actually this goes even
4:48
more extreme than this and I will show
4:50
it when we write the code so let's start
4:54
the cube ID and create a new
4:57
project I am using STM 32 f13
5:02
C8 give some name to the project and
5:06
finish let's start with the clock setup
5:10
first I am selecting the external
5:12
Crystal to provide the clock the blue
5:15
pill board has 8 MHz Crystal on it and
5:19
we will run the system at 60
5:21
MHz I am choosing 60 because most of the
5:24
SDM 32 MCU can run at this clock
5:27
frequency so if you have some confusion
5:30
you can set the clock as for this
5:32
video note that both apb1 and two timer
5:36
clocks are running at 60
5:38
MHz the timer will use this clock to
5:41
generate the output signal each timer is
5:45
either connected to apb1 or apb2 bus you
5:48
can check the data sheet of your MCU to
5:50
confirm which bus it is connected to I
5:54
have already made a video explaining the
5:56
pwm generation in SDM 32 you can check
6:00
it out in case of any doubt since both
6:03
the APB timer clocks are at the same
6:05
frequency I don't need to worry about
6:09
Source now go to CIS debug and enable
6:13
serial wire let's configure the timer
6:16
now I am using the timer one to generate
6:20
the pwm signal set the clock source to
6:24
internal clock now enable the pwm
6:27
generation for channel 1
6:30
you can see the pin pa8 got configured
6:33
as the pwm output we will connect this
6:36
pin to the signal pin of the
6:38
servo let's configure the parameters now
6:42
we need to generate a signal with a
6:46
HZ here is the formula to calculate the
6:49
frequency of the pwm signal we have the
6:53
timer clock of the same frequency as the
6:58
MHz if if I set the prescaler to
7:01
1,200 and the auto reload value of 1,000
7:05
the output frequency will be 50
7:07
HZ the ARR value plays an important role
7:12
as the width of the signal is calculated
7:14
with respect to this value I will
7:17
explain in a while why I chose to input
7:21
1,000 let's set the prescaler value to
7:26
1,200 also set the a value to
7:30
1,000 whenever you are setting these
7:32
values the value should be one lower
7:34
than the desired value this is because
7:37
the code at the register level adds a
7:39
one to this value click save to generate
7:43
project we know that the servo motor
7:46
uses the signal width from 1 millisecond
7:50
milliseconds this is a standard for any
7:53
Servo but when I was testing it I found
7:56
that the motor was responding from 0.5
7:59
milliseconds to 2.5 milliseconds so I
8:04
values the pulse has a frequency of 50
8:07
Herz which implies that the period is of
8:11
milliseconds a width of 0.5 milliseconds
8:15
out of 20 milliseconds is equivalent to
8:17
2.5% duty cycle and a width of 2.5
8:21
milliseconds is equivalent to 12.5% duty
8:25
cycle now let's assume that we set the
8:30
100 in order to vary the signal between
8:33
two extreme ends we only have a
8:35
difference of 10 steps between them so
8:39
each step would have a major change in
8:41
the pulse width on the other hand if we
8:45
use the auto reload value of 1,000 the
8:48
duty values will change to 25 and
8:52
125 this will result in a total of 100
8:55
steps between the extreme values and so
8:58
are outputs signal will have more
9:01
variations all right let's write our
9:04
code now inside the main function start
9:10
mode I am using the timer one channel
9:15
generation the compare register is where
9:18
we set our pulse value for the duty
9:21
cycle here I am setting the value to 25
9:25
basically one of the extreme
9:27
ends let's build and flat the project to
9:35
board you can see the servo motor is
9:38
rotating with very high
9:42
RPM if we check the output on the logic
9:44
analyzer we can see the pulse has a
9:49
milliseconds the frequency of the output
9:54
HZ now I am setting the pulse value to
9:57
50 which will result in the sign with a
10:01
milliseconds you can hear the sound of
10:03
the motor to note the difference between
10:08
speeds the speed has been reduced
10:11
compared to the previous
10:13
value now we will set it to 75 which
10:16
will result in a pulse of width 1.5
10:20
milliseconds and this will stop the
10:22
motor just like it was stopping when the
10:24
knob was in the middle setting the value
10:27
to 100 will result in a pulse of width 2
10:34
milliseconds the motor is now rotating
10:36
in the counterclockwise
10:38
Direction you can see the pulse width is
10:46
milliseconds and in the end we will set
10:50
125 which will result in a pulse of
10:57
milliseconds note the sound of the motor
10:59
the speed has been increased to maximum
11:02
so we can see the motor is responding
11:04
for the pulse of width between 0.5
11:09
milliseconds we will write our code with
11:12
respect to these values let's define a
11:15
pulse variable to store the pulse value
11:18
which we will set to the compare
11:20
register I am setting the initial value
11:23
of 75 this is where the motor stops now
11:27
I want to gradually increase the RPM of
11:29
the motor in One Direction and then in
11:31
the other direction so our pulse value
11:34
is at 75 right now and we will increase
11:40
125 with each step the value will be
11:43
passed to the compare register let's
11:46
give a delay of 250 milliseconds between
11:49
each step when the pulse reaches 125 the
11:54
motor will be at the maximum RPM in the
11:56
counterclockwise Direction and this one
11:59
while loop will exit then we will start
12:02
reducing the pulse value and we will
12:04
keep doing it until the value reaches to
12:07
25 when the pulse is reducing the motor
12:10
will start slowing down and it will stop
12:13
when the pulse is around
12:15
75 then with the further reduction in
12:17
the pulse value the motor will start
12:20
rotating in the clockwise Direction and
12:24
increasing when the pulse value is near
12:27
25 the motor will rotate at maximum RPM
12:32
Direction at this point this while loop
12:35
will exit and the first one will run the
12:38
pulse will start increasing again and
12:41
the motor will start slowing down the
12:44
motor will stop when the pulse value is
12:48
75 the pulse continues to increase and
12:51
the motor will start rotating in the
12:55
Direction the entire Loop will continue
12:59
let's build and Flash the project to the
13:02
board hear the sound of the motor to get
13:04
the idea about the speed and please
13:07
ignore the background noise
13:55
you saw the motor was responding how we
13:59
we were able to control the RPM of the
14:02
continuous Servo motor using the pwm
14:05
signal in the next video we will see how
14:08
to use the potentiometer and a button to
14:11
control the RPM and direction of this
14:14
motor this is it for the video you can
14:18
download the code from the link in the
14:21
description leave comments in case of
14:23
any doubt keep watching and have a nice