SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
(continued from previous page)
tone(buzzer,
494
,
250
)
utime
.
sleep_ms(
500
)
tone(buzzer,
523
,
250
)
How it works?
If the passive buzzer given a digital signal, it can only keep pushing the diaphragm without producing sound.
Therefore, we use the
tone()
function to generate the PWM signal to make the passive buzzer sound.
This function has three parameters:
•
pin
, the GPIO pin that controls the buzzer.
•
frequency
, the pitch of the buzzer is determined by the frequency, the higher the frequency, the higher the pitch.
•
Duration
, the duration of the tone.
We use the
duty_u16()
function to set the duty cycle to 30000(about 50%). It can be other numbers, and it only
needs to generate a discontinuous electrical signal to oscillate.
What more
We can simulate the specific tone according to the fundamental frequency of the piano, so as to play a complete piece
of music.
•
Piano key frequencies - Wikipedia
import
machine
import
utime
NOTE_C4
=
262
NOTE_G3
=
196
NOTE_A3
=
220
NOTE_B3
=
247
melody
=
[NOTE_C4,NOTE_G3,NOTE_G3,NOTE_A3,NOTE_G3,NOTE_B3,NOTE_C4]
buzzer
=
machine
.
PWM(machine
.
Pin(
15
))
def
tone
(pin,frequency,duration):
pin
.
freq(frequency)
pin
.
duty_u16(
30000
)
utime
.
sleep_ms(duration)
pin
.
duty_u16(
0
)
for
note
in
melody:
tone(buzzer,note,
250
)
utime
.
sleep_ms(
150
)
84
Chapter 3. For MicroPython User
Summary of Contents for Thales Kit
Page 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Page 2: ......
Page 4: ...ii ...
Page 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Page 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Page 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...