0:09
hello and welcome to controllers Tech in
0:12
today's video we will see how to
0:15
interface a passive buzzer module with
0:18
32 we will write functions to set the
0:21
tone of the desired frequency and in the
0:23
end we will play A Melody there are two
0:26
types of buzzer modules available for
0:30
an active buzzer is only capable of
0:32
producing a single tone so to interface
0:35
an active buzzer you just need to turn
0:37
the signal on or off just how you would
0:40
blinker LED on the other hand a passive
0:43
buzzer needs a triggered wave to produce
0:46
Sound by controlling the frequency and
0:49
duty of the wave we can control the
0:51
sound output in today's tutorial we will
0:55
cover how to do this using the SDM 32
0:58
MCU here here I have two different types
1:01
of passive buzzer modules with me both
1:04
of these modules have three pins
1:06
available but the middle pin in this
1:09
module is not connected so we only have
1:12
a signal pin and a ground pin it does
1:15
not need a separate VCC Supply we can
1:18
connect the pwm output to this signal
1:21
pin and then vary the frequency and duty
1:23
to control the output sound the other
1:26
module has all three pins connected you
1:30
can connect the VCC to 5 Vols ground to
1:33
ground and pwm output to the io pin both
1:37
of these modules are available on the
1:39
controller's tech store so you can
1:42
purchase them from there I am going to
1:45
use the black one in this tutorial as it
1:47
needs only two pins for the connection
1:50
this is how the buzzer is connected to
1:53
32 the pin pa8 of the MCU is the timer 1
1:58
pwm output pin and it is connected to
2:01
the signal pin let's start the QBE ID
2:05
and create a new project I am using SDM
2:11
C8 give some name to the project and
2:15
click finish let's start with the clock
2:18
setup first I am selecting the external
2:21
Crystal to provide the clock the blue
2:24
pill board has 8 MHz Crystal on it and
2:27
we will run the system at 60 MZ
2:31
I am choosing 60 because most of the SDM
2:33
32 MCU can run at this clock frequency
2:37
so if you have some confusion you can
2:39
set the clock as for this
2:41
video note that both apb1 and two timer
2:44
clocks are running at 60
2:47
mahz the timer will use this clock to
2:50
generate the output signal each timer is
2:53
either connected to apb1 or apb2 bus you
2:57
can check the data sheet of your MCU to
2:59
confirm which bus it is connected to I
3:02
have already made a video explaining the
3:04
pwm generation in SDM 32 you can check
3:08
it out in case of any doubt since both
3:11
the APB timer clocks are at the same
3:14
frequency I don't need to worry about
3:17
Source now go to CIS debug and enable
3:21
serial wire let's configure the timer
3:25
now I am using the timer one to generate
3:28
the pwm signal set the clock source to
3:32
internal clock now enable the pwm
3:35
generation for channel 1 you can see the
3:38
pin pa8 got configured as the pwm output
3:42
we connected this pin to the buzzer in
3:46
diagram let's configure the parameters
3:48
now in order to generate the sound of
3:51
desired frequency we need to change the
3:54
prescaler on the fly but keep the a
3:57
constant the timer clock is is at 60
4:01
mahz let's say if I keep the auto reload
4:06
1,000 now if I want to generate a
4:08
frequency of 15 khz the prescaler should
4:12
be set to four similarly if we want to
4:15
generate the frequency of 250 htz the
4:18
prescaler should be set to
4:20
240 we will write a formula in the main
4:23
code to calculate the prescaler for
4:26
frequency so let's leave the prescaler
4:29
to Zer for now and set the auto reload
4:33
1,000 we will not modify the duty cycle
4:36
throughout this video rather we will
4:40
50% so set the pulse value to half of
4:43
the auto reload value so the duty can be
4:46
50% that is all the configuration we
4:49
need click save to generate the project
4:53
let's start with a simple test first in
4:56
the main function we will start the
4:59
timer in the pwm mode the parameters of
5:02
this function are the timer Handler and
5:05
the timer Channel which you have
5:07
selected in the cube MX in the cube MX
5:10
configuration we set the prescaler to
5:13
zero so the timer will run at 60
5:16
khz so before starting the timer we will
5:21
240 this will generate a frequency of
5:24
250 HZ actually this value should be 2
5:30
39 whenever you are setting the
5:32
prescaler the value should be one lower
5:35
than the desired value this is because
5:37
the code at the register level adds a
5:40
value I will fix this in the final code
5:44
and also mention it in the
5:46
article all right the code has been
5:48
flashed successfully and now you can
5:50
hear the sound produced by the
5:58
buzzer now if I set the prescaler to 24
6:01
the frequency will be
6:16
frequency you can clearly hear the sound
6:18
with a higher pitch than
6:20
before let's change these frequencies in
6:23
the while loop so that we can hear the
6:25
differences between them
6:35
you can hear the tone changing every 500
6:39
milliseconds let's also see the output
6:50
buzzer we pretty much got the same tones
6:54
also a note is produced by managing
6:57
these frequencies and their timing
7:00
we will also do the same and try to
7:03
produce a random Melody we need a
7:06
function to calculate the prescaler
7:08
value for the desired
7:10
frequency let's first Define the timer
7:13
frequency you can change it as per your
7:16
setup the argument of the function
7:19
prescaler for frequency is the desired
7:21
frequency which you want to
7:23
Output inside this function we will use
7:26
the same formula which we have used so
7:28
far to C calculate the
7:30
prescaler the return the calculated
7:33
value in case the frequency is zero we
7:37
don't want to pass the value to the
7:38
formula so we will simply return zero
7:42
now inside the while loop I am
7:44
generating the output at different
7:47
frequencies there is a delay of 250
7:50
milliseconds between each
7:56
tone let's build and Flash the code to
8:09
you can hear the tones of five different
8:11
frequencies and the pitch is gradually
8:14
increasing and in a way we just produced
8:19
tones basically you can produce any
8:21
Melody by mixing different frequencies
8:23
and their duration for which they will
8:25
play while I was looking for some
8:28
frequency data for a Melody I found this
8:30
GitHub repository this is actually
8:33
written for the Arduino but we are only
8:36
interested in the frequency and timing
8:38
data here this guy has also explained
8:41
how to generate the data for any desired
8:43
music but it is a paid service so I am
8:46
not covering that inside the Arduino
8:50
code I found the data for The Stranger
8:52
Things Melody any Melody will contain
8:55
these two data at minimum the frequency
9:00
let's copy them in our code these here
9:03
are the frequency values for different
9:05
tones and below are their duration in
9:07
milliseconds for which these tones will
9:10
play there is one more parameter which
9:13
is used in the Arduino while playing
9:15
these Melodies and that is the delay
9:18
after the tone for which the tone is
9:21
paused we will also consider this delay
9:24
in our code let's write a function which
9:27
will play the melody for us the
9:29
parameters of this function are the
9:31
pointer to the frequency array the
9:33
pointer to the timing array pointer to
9:36
the pause timing array and the size of
9:39
arrays here for each array element we
9:42
will calculate the prescaler value for
9:46
frequency then get the duration for
9:48
which the tone will play now Define a
9:52
variable to store the pause data if the
9:55
pause arrays passed to the parameter we
9:59
timing as for the Arduino source code
10:03
this pause timing also considers the
10:05
duration time so we will need to
10:07
subtract it now let's set the prescaler
10:11
value as calculated above and give a
10:14
delay of the duration time but if we do
10:17
want to pause the tone after this
10:19
duration we will not play any tone after
10:22
this time and it will continue to pause
10:24
for the pause time calculated above we
10:28
will Define this note tone
10:30
function inside this function we will
10:32
set the prescaler to
10:34
zero this will actually generate the
10:37
output frequency of 60 khz which we
10:40
can't hear so it will be treated as the
10:43
pause let's call this function in the
10:45
main function and pass the
10:48
parameters there is no pause data here
10:51
so pass the null here also we need to
10:54
calculate the size of the array
11:02
after the melody is over we will call
11:04
the no tone function to stop the tone
11:07
all right let's Flash the code to the
11:22
board you can hear the stranger things
11:25
Melody being played by the buzzer to
11:28
demonstrate the use of the pause let's
11:31
set the pause time of 30 milliseconds
11:48
tone you can clearly hear the pause
11:51
between the tones here this is it for
11:54
the video so we were able to interface
11:57
the passive buzzer module with the stdm
12:00
32 we produced the sound of desired
12:03
frequencies and also played a Melody
12:06
successfully you can try some other
12:08
Melodies and let me know the outcome the
12:11
link to download the code is in the
12:13
description below leave comments in case
12:16
of any doubt keep watching and have a