689 views|1 replies

274

Posts

8

Resources
The OP
 

【Digi-Key Follow me Issue 1】Tenth Post - Buzzer [Copy link]

 

I thought the buzzer was the easiest, but it is indeed the hardest. If you want to be able to sing it, you have to learn some music.

The onboard buzzer should be an active buzzer. The simplest usage is to make a sound at a high level and no sound at a low level. The code is as follows:

from machine import Pin
import time

beep = Pin(16,Pin.OUT)
beep.on() #发声
time.sleep(2)
beep.off() #不发声

If you want to generate music, you need to use the PWM module. I was confused by the number of notes in the library that everyone called. I thought there were only do, re, mi, fa, sol, la, si. But what I saw in the library were indeed C, C# and the like. So I supplemented my knowledge of notes and asked chatGPT this time.

It can be seen that the frequency corresponding to the C note is 256hz. If it is one octave higher, the frequency will be twice as high.

Then there is the correspondence between each note and do, re, mi and the corresponding frequency. Therefore, if you want to make a do sound, the PWM frequency should be 261Hz, and the duty cycle should be set to 50%. So the code to make do, re, mi, fa, sol, la, si is as follows:

from machine import Pin,PWM
import time

yinfu = [261,293,330,349,392,440,494]

beep = PWM(Pin(16))
for i in range(7):
    beep.freq(yinfu[i])
    beep.duty_u16(0x8000)
    time.sleep(1)

beep.deinit()

If you want to play music, it is best to call other libraries, such as

链接已隐藏,如需查看请登录或者注册
.

This post is from DigiKey Technology Zone

Latest reply

If you want to play music, it is best to call other libraries, such as https://github.com/james1236/buzzer_music . It is convenient to have this library.   Details Published on 2023-6-20 07:22
 
 

6822

Posts

11

Resources
2
 

If you want to play music, it is best to call other libraries, such as

链接已隐藏,如需查看请登录或者注册
.

It is convenient to have this library.

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