0:09
hello and welcome to controllers Tech
0:12
this is part two of the video
0:14
interfacing The Continuous Servo motor
0:17
32 in the previous video we saw how to
0:20
connect the motor and how to use the pwm
0:23
to vary the RPM of the servo motor today
0:27
we will add a potentiometer and a button
0:29
to our previous project the
0:32
potentiometer will be connected via the
0:34
ADC and we will use it to vary the RPM
0:37
of the motor the button will be used to
0:40
change the direction of rotation of the
0:42
motor I have already covered the basics
0:45
of the continuous Servo in the previous
0:47
tutorial so I will not go over them
0:50
again since we will be using the same
0:52
project that we created in the previous
0:55
video let's first import the project to
0:58
ID here we have have the main file from
1:01
the previous project we will now add the
1:04
ADC and button to this project so let's
1:08
MX here is the connection diagram for
1:11
this project on the left side we have
1:14
the servo motor and the connection is
1:17
the same as the previous video the servo
1:20
is powered from a 5vs battery and the
1:23
signal pin is connected to the pin pa8
1:26
timer 1 channel 1 I have added a
1:29
potentiometer to this project which will
1:31
be used to vary the RPM of the
1:33
continuous Servo motor the potentiometer
1:37
is connected to the 3.3 volts of the MCU
1:40
and the output pin is connected to the
1:43
pa1 this is the adc1 channel one pin
1:47
there is a button connected to the pin
1:49
pa2 which we will use as the external
1:52
interrupt pin the button will be used to
1:55
change the direction of rotation of the
1:58
motor let's enable the adc1 Channel 1
2:02
you can see the pin pa1 got configured
2:05
as the ADC pin this is where we have
2:07
connected the potentiometer to I am
2:10
leaving the ADC configuration to default
2:14
so there is only one conversion The
2:16
Continuous conversion mode is disabled
2:18
and the sampling time is the least 1.5
2:22
Cycles potentiometer does not need any
2:25
specific configuration so you can set
2:27
this however you wish now now configure
2:30
the pin pa2 as the external interrupt
2:33
pin this is where the button is
2:36
connected the other pin of the button is
2:38
connected to 3.3 Vols so we will pull
2:41
this pin to Low by default when the
2:44
button is pressed this pin will be
2:46
pulled High to 3.3 Vols and the
2:49
interrupt will trigger make sure to
2:52
enable the external interrupt in the
2:54
nvic tab there is some issue with the
2:57
clock configuration due to the ADC C we
3:01
need to reduce the ADC clock to the
3:03
permitted value all right 10 mahz works
3:07
fine here that is all the configuration
3:10
we need click save to generate the
3:13
project we will read the ADC in the
3:15
Blocking Mode let's write a function to
3:18
read the ADC value here first start the
3:22
ADC then pull for the conversion to
3:25
finish then read the ADC value and store
3:28
it in the variable a ADC Val finally
3:31
stop the ADC the stdm 32 f103 C8 has
3:37
12-bit adcs so the values will range
3:42
4,095 we need to map these values
3:45
according to our pulse width the map
3:48
function here does exactly this I got
3:51
this function from the Arduino source
3:54
code the parameters of the function are
3:57
the variable we want to map the minimum
4:00
and maximum values of the variable and
4:03
the minimum and maximum values that we
4:06
output in the previous video we
4:09
calculated the minimum and maximum
4:11
values for the compare register for
4:15
responding therefore we are mapping the
4:18
ADC value between the limit of 25 and
4:23
125 basically if the ADC returns zero
4:27
the output of this function will be 25
4:29
and if the ADC Returns
4:32
4,095 the output of the function will be
4:36
125 now in the main function we have
4:39
already started the timer in the pwm
4:42
mode inside the while loop we will fetch
4:45
the mapped ADC value and then pass it to
4:49
register that is all I have not added
4:52
the button yet but I will add it later
4:58
first right now the potentiometer is in
5:01
the middle therefore the motor is
5:03
stationary when the slider is pulled
5:06
backwards the ADC value increases
5:11
4,095 so the pulse value is also
5:14
increasing towards 125 and the RPM of
5:18
the motor is increasing in the
5:21
Direction at the extreme end the ADC
5:24
value is maximum so the pulse value is
5:30
5 the motor is rotating at maximum speed
5:34
in the counterclockwise
5:36
Direction now when the slider moves back
5:38
towards the center the ADC value starts
5:41
decreasing towards 2048 and the motor
5:44
also slows down in the counterclockwise
5:48
Direction at this point the pulse value
5:50
is 75 and hence the motor is
5:54
stationary when moving the slider
5:56
towards the other end the ADC value
6:00
decreases the pulse value also decreases
6:03
from 75 and RPM of the motor starts
6:06
increasing in the clockwise
6:09
Direction when the slider is at another
6:11
extreme the ADC value is near zero and
6:15
the pulse value is at
6:17
25 the motor is rotating at the maximum
6:22
Direction so we are able to control the
6:27
potentiometer the ADC value ranging from
6:31
4,095 are maed in the range of 25 to
6:35
125 and we can control the RPM as well
6:38
as the direction of the motor now let's
6:41
assume that instead of splitting the
6:43
slider into two halves I want to control
6:46
the RPM of the motor for the fall slider
6:49
movement basically I want to vary the
6:51
pulse from 25 to 75 but for the entire
6:55
range of the ADC values from 0 to 4,095
6:59
five and to control the direction of the
7:02
rotation I can add a button let's define
7:06
a few variables first the pulse fix
7:09
variable will remain constant and it is
7:12
set at the middle value of the pulse
7:14
limit the variable pulse will be updated
7:17
in the code the variable button will
7:20
keep track of the button pressed count
7:22
the pin pa2 where the button is
7:25
connected is pulled Low by default when
7:28
the button is pressed pressed the pin
7:30
will be pulled High to 3.3 Vols this
7:34
will trigger an external interrupt and
7:36
the GPI oxd callback will be called
7:40
inside this call back we will first
7:42
check if it is called by the pin 2 then
7:45
increment the button variable when the
7:48
button variable is higher than two reset
7:51
it back to one this way the button
7:54
variable will be either one or two we
7:57
can use these values of the variable to
8:00
set the direction of the rotation in
8:02
either clockwise or counter we also want
8:05
the motor to rotate in only one
8:07
direction for the entire range of the
8:10
potentiometer so the mapping needs to be
8:12
done differently instead of mapping the
8:15
ADC value for the entire range of the
8:18
pulse value we will only map it to 0 to
8:21
50 let's understand this in detail when
8:25
the potentiometer will vary from 0 to
8:29
the function will output the value from
8:31
0 to 50 now our fixed pulse variable
8:35
contains a constant value of
8:37
75 so to rotate the motor in clockwise
8:40
direction we can subtract the ADC output
8:43
from this constant value this will
8:46
result in the final pulse value ranging
8:50
75 similarly to rotate the motor in the
8:53
counterclockwise direction we can add
8:55
the ADC value to the fixed pulse value
8:59
and that will result in the final pulse
9:01
value ranging from 75 to
9:05
125 inside the while loop we will check
9:08
the value of the button variable if it
9:10
is one then add the ADC map value to the
9:13
pulse fixed value so as to rotate the
9:16
motor in the counterclockwise Direction
9:19
otherwise if it is two then subtract the
9:21
ADC map value from the pulse fixed value
9:24
so as to rotate the motor in the
9:26
clockwise direction that is all we need
9:29
here let's build and Flash the projects
9:34
board right now the button value is zero
9:38
and if I vary the slider there is no
9:41
rotation now let's press the button you
9:44
can see the motor is rotating in the
9:46
clockwise Direction and if I vary the
9:49
slider position the RPM of the motor
9:52
also varies accordingly
10:06
when the button is pressed again the
10:08
direction of rotation has been
10:10
changed and varying the slider now
10:12
increases the RPM in the
10:26
Direction when the button is pressed
10:28
again the direction is switched back
10:31
clockwise so we can control the RPM of
10:36
potentiometer I have demonstrated two
10:38
different ways of doing it and you can
10:40
use whichever suits you better this is
10:43
it for the video I hope everything was
10:46
clear you can download the code from the
10:49
link in the description leave comments
10:52
in case of any doubt keep watching and
10:56
have a nice day ahead