Questions And Answers

More Tutorials

Raspberry Playing Basic Notes

First, let's try to control the buzzer to play seven basic notes: do, re, mi, fa, sol, la, si.

Hardware Connection

In this project, the electronic hardware we need to use is as follows:

• Raspberry Pi Pico

• Shield for Pi Pico

• Grove - Passive Buzzer

Attach the Pico and the Shield, and use a Grove data cable to connect the buzzer to A1.

Write a Program

If we want to use the buzzer to play music, we need to learn to control the two dimensions of the sound emitted by the buzzer: volume and tone.

For the volume, like adjusting the brightness of the LED, we can control it by changing the duty cycle of the PWM signal as well. On the other hand, since the tone of a sound is determined by its frequency, we can control it by just modifying the frequency of the PWM signal.

Let's write a program to let the buzzer send out seven notes: do, re, mi, fa, sol, la, si.

In this program, we need to define pins and use the “sleep()" function to control the duration of each note. As usual, we need to declare the function library at the beginning of the program:

from machine import Pin, PWM
from time import sleep

Next, define the pin:

buzzer = PWM(Pin(27))

Next, we need to use “freq()" and “duty_u16()" to define the tone and volume:

1 buzzer.freq(1046) #DO 
2 buzzer.duty_u16(1000)
4 buzzer.freq(1175) #RE 
5 buzzer.duty_u16(1000)
7 buzzer.freq(1318) #MI 
8 buzzer.duty_u16(1000)
10 buzzer.freq(1397) #FA
11 buzzer.duty_u16(1000) 
13 buzzer.freq(1568) #SO 
14 buzzer.duty_u16(1000) 
16 buzzer.freq(1760) #LA 
17 buzzer.duty_u16(1000)
19 buzzer.freq(1967) #SI 
20 buzzer.duty_u16(1000)

Finally, use “sleep()" to specify the duration of each note, and complete the program. The complete program is as follows:

1 from machine import Pin, PWM 
2 from time import sleep
4 buzzer = PWM(Pin(27)) 
5 while True:
7 buzzer.freq(1046) #DO buzzer.duty_u16(1000) 
8 sleep(1) buzzer.freq(1175) #RE buzzer.duty_u16(1000) 
9 sleep(1) buzzer.freq(1318) #MI buzzer.duty_u16(1000)

Use a USB cable to connect Pico to the computer, click the “run" button to save the program to any location. Now, you can hear the buzzer play seven basic notes.


In this page (written and validated by ) you learned about Raspberry Playing Basic Notes . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Two Tigers.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.