781 views|1 replies

188

Posts

0

Resources
The OP
 

[Digi-Key Follow me Issue 1] Task 2: Buzzer Driver [Copy link]

 

First, let's make a simple buzzer with variable frequency, the sound will become higher and higher.

import machine
from machine import Pin
import time

led = Pin("LED",Pin.OUT)
buzze = machine.Pin(26)
pwm1 = machine.PWM(buzze)

frep1 = 100

pwm1.freq(frep1)
pwm1.duty_u16(32768)

while True:
    led.value(1)
    frep1 += 100
    pwm1.freq(frep1)
    time.sleep(.25)
    led.value(0)
    time.sleep(.25)
    if frep1 > 2400:
        frep1 = 200

Experiment 2: Based on the different frequencies corresponding to the different tones of the music, a modified version of the "Birthday Song" was created by other netizens.

蜂鸣器 生日快乐歌

The simplified score of Happy Birthday song is shown in the picture below. I don’t understand music so I won’t analyze it. Just set it according to the score above.

See the test code below.

from machine import Pin, PWM
from time import sleep

buzzer = PWM(Pin(20))
vol = 1300  #音量大小
def DO(time):
    buzzer.freq(1046) #1
    buzzer.duty_u16(vol)
    sleep(time)
def RE(time):
    buzzer.freq(1175) #2
    buzzer.duty_u16(vol)
    sleep(time)
def MI(time):
    buzzer.freq(1318) #3
    buzzer.duty_u16(vol)
    sleep(time)
def FA(time):
    buzzer.freq(1397) #4
    buzzer.duty_u16(vol)
    sleep(time)
def SO(time):
    buzzer.freq(1568) #5
    buzzer.duty_u16(vol)
    sleep(time)
def LA(time):
    buzzer.freq(1760) #6
    buzzer.duty_u16(vol)
    sleep(time)
def SI(time):
    buzzer.freq(1967) #7
    buzzer.duty_u16(vol)
    sleep(time)
def N(time):
    buzzer.duty_u16(0) #close
    sleep(time)
while True:

    SO(0.17) #5
    N(0.01) #0
    SO(0.17) #5
    
    LA(0.33) #6
    SO(0.33) #5
    DO(0.33) #1
    N(0.01) #0
    
    SI(0.33) #7-
    N(0.01) #0
    SI(0.33) #7-    
    SO(0.33) #5
    
    LA(0.33) #6
    SO(0.33) #5
    RE(0.33) #2    
    N(0.01) #0
    
    DO(0.33) #1
    N(0.01) #0
    DO(0.33) #1
    SO(0.33) #5

    SO(0.33) #5
    MI(0.33) #3
    DO(0.33) #1    
    N(0.01) #0
    DO(0.33) #1

    SI(0.33) #7-
    LA(0.33) #6
    N(0.01) #0
    LA(0.33) #6
    
    N(0.01) #0        
    FA(0.33) #4
    N(0.01) #0
    FA(0.33) #4

    MI(0.33) #3
    RE(0.33) #2
    DO(0.33) #1    
    N(0.01) #0
    
    DO(0.33) #1    
    N(0.67) #0    


This post is from DigiKey Technology Zone

Latest reply

ox   Details Published on 2023-6-26 21:31
 
 

82

Posts

0

Resources
2
 

ox

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list