# How to make some sound with MicroPython
# Make a single note sound
# Author: Chanita Phayungwong
# Date: Feb 14, 2023
# Import the machine module for GPIO and PWM
from machine import Pin, PWM
# Import the time module to add some delays
import time
# Create a regular GPIO object from pin 23
p32 = Pin(32, Pin.OUT)
# Create a new object and attach the pwm driver
buzzer = PWM(p32)
# Set a pwm frequency
C1 = 256
D1 = 288
E1 = 320
F1 = 341
G1 = 384
A1 = 427
B1 = 480
C2 = 512
def play_music() :
buzzer.freq(C1)
time.sleep(1)
buzzer.freq(D1)
time.sleep(1)
buzzer.freq(E1)
time.sleep(1)
buzzer.freq(F1)
time.sleep(1)
buzzer.freq(G1)
time.sleep(1)
buzzer.freq(A1)
time.sleep(1)
buzzer.freq(B1)
time.sleep(1)
buzzer.freq(C2)
time.sleep(1)
# Set the pwm duty value
# this serves as volume control
# Max volume is a duty value of 512
buzzer.duty(50)
# Let the sound ring for a certain duration
#time.sleep(1)
# Turn off the pulse by setting the duty to 0
#buzzer.duty(0)
# And disconnect the pwm driver to the GPIO pin
while True :
play_music()
buzzer.deinit()
"""
buzzer.freq(512)
time.sleep(1)
buzzer.freq(480)
time.sleep(1)
buzzer.freq(427)
time.sleep(1)
buzzer.freq(384)
time.sleep(1)
buzzer.freq(341)
time.sleep(1)
buzzer.freq(320)
time.sleep(1)
buzzer.freq(288)
time.sleep(1)
buzzer.freq(256)
time.sleep(1)
"""