树莓派Pico上使用无源蜂鸣器

有源蜂鸣器和无源蜂鸣器的区别:外形不同、测试声音不同、对输入信号的要求不同、有无震荡源、价格不同、高度不同、万用表测电阻不同、直流电压测试不同、优点不同。
外形不同
两种蜂鸣器的引脚郡朝上放置时,可以看出有绿色电路板的一种是无源蜂鸣器,没有电路板而用黑胶封闭的一种是有源蜂鸣器。
对输入信号的要求不同
有源蜂鸣器工作的理想信号是直流电,通常标示为vdc、vdd等。因为蜂鸣器内部有一简单的振荡电路,能将恒定的直流电转化成一定频率的脉冲信号,从面实出磁场交变,带动钼片振动发音。但是在某些有源蜂鸣器在特定的交流信号下也可以工作,只是对交流信号的电压和频率要求很高,此种工作方式一般不采用。而无源蜂鸣器没有内部驱动电路,有些公司和工厂称为讯响器,国标中称为声响器。无源蜂鸣器工作的理想信号方波。如果给预直流信号蜂鸣器是不响应的,因为磁路恒定,钼片不能振动发音。
有无震荡源
有源蜂鸣器内部带震荡源,所以只要一通电就会叫,而无源内部不带震荡源,所以如果用直流信号无法令其鸣叫。
无源蜂鸣器的优点是:便宜,声音频率可控,可以做出多来米发索拉西的效果,在一些特例中,可以和led复用一个控制口。有源蜂鸣器的优点是:程序控制方便。
接线
蜂鸣器模块的 vcc 接树莓派 pico 的 vsys 引脚;蜂鸣器模块的 gnd 接树莓派 pico 的 gnd 引脚;蜂鸣器模块的 i/o 引脚接树莓派 pico 的 gp22 引脚。
编程
树莓派 pico 通过 pwm 来驱动无源蜂鸣器发出不同频率的声音。我们直接使用现成的 python 库 buzzer_music,该库可以方便的实现音乐乐谱播放。
将下面的代码保存在 pico 上,命名为 buzzer_music.py。
micropython (raspberry pi pico)plays music written on onlinesequencer.net through a passive piezo buzzer.uses fast arpeggios with a single buzzer to simulate polyphonyalso supports multiple buzzers at once for real polyphonyhttps://github.com/james1236/buzzer_music from machine import pin, pwmfrom math import ceil tones = { 'c0':16, 'c#0':17, 'd0':18, 'd#0':19, 'e0':21, 'f0':22, 'f#0':23, 'g0':24, 'g#0':26, 'a0':28, 'a#0':29, 'b0':31, 'c1':33, 'c#1':35, 'd1':37, 'd#1':39, 'e1':41, 'f1':44, 'f#1':46, 'g1':49, 'g#1':52, 'a1':55, 'a#1':58, 'b1':62, 'c2':65, 'c#2':69, 'd2':73, 'd#2':78, 'e2':82, 'f2':87, 'f#2':92, 'g2':98, 'g#2':104, 'a2':110, 'a#2':117, 'b2':123, 'c3':131, 'c#3':139, 'd3':147, 'd#3':156, 'e3':165, 'f3':175, 'f#3':185, 'g3':196, 'g#3':208, 'a3':220, 'a#3':233, 'b3':247, 'c4':262, 'c#4':277, 'd4':294, 'd#4':311, 'e4':330, 'f4':349, 'f#4':370, 'g4':392, 'g#4':415, 'a4':440, 'a#4':466, 'b4':494, 'c5':523, 'c#5':554, 'd5':587, 'd#5':622, 'e5':659, 'f5':698, 'f#5':740, 'g5':784, 'g#5':831, 'a5':880, 'a#5':932, 'b5':988, 'c6':1047, 'c#6':1109, 'd6':1175, 'd#6':1245, 'e6':1319, 'f6':1397, 'f#6':1480, 'g6':1568, 'g#6':1661, 'a6':1760, 'a#6':1865, 'b6':1976, 'c7':2093, 'c#7':2217, 'd7':2349, 'd#7':2489, 'e7':2637, 'f7':2794, 'f#7':2960, 'g7':3136, 'g#7':3322, 'a7':3520, 'a#7':3729, 'b7':3951, 'c8':4186, 'c#8':4435, 'd8':4699, 'd#8':4978, 'e8':5274, 'f8':5588, 'f#8':5920, 'g8':6272, 'g#8':6645, 'a8':7040, 'a#8':7459, 'b8':7902, 'c9':8372, 'c#9':8870, 'd9':9397, 'd#9':9956, 'e9':10548, 'f9':11175, 'f#9':11840, 'g9':12544, 'g#9':13290, 'a9':14080, 'a#9':14917, 'b9':15804} #time, note, duration, instrument (onlinesequencer.net schematic format)#0 d4 8 0;0 d5 8 0;0 g4 8 0;8 c5 2 0;10 b4 2 0;12 g4 2 0;14 f4 1 0;15 g4 17 0;16 d4 8 0;24 c4 8 0 class music: def __init__(self, songstring='0 d4 8 0', looping=true, tempo=3, duty=2512, pin=none, pins=[pin(0)]): self.tempo = tempo self.song = songstring self.looping = looping self.duty = duty self.stopped = false self.timer = -1 self.beat = -1 self.arpnote = 0 self.pwms = [] if (not (pin is none)): pins = [pin] i = 0 for pin in pins: self.pwms.append(pwm(pins[i])) i = i + 1 self.notes = [] self.playingnotes = [] self.playingdurations = [] #find the end of the song self.end = 0 splitsong = self.song.split(;) for note in splitsong: snote = note.split( ) testend = round(float(snote[0])) + ceil(float(snote[2])) if (testend > self.end): self.end = testend #create empty song structure while (self.end > len(self.notes)): self.notes.append(none) #populate song structure with the notes for note in splitsong: snote = note.split( ) beat = round(float(snote[0])); if (self.notes[beat] == none): self.notes[beat] = [] self.notes[beat].append([snote[1],ceil(float(snote[2]))]) #note, duration #round up end of song to nearest bar self.end = ceil(self.end / 8) * 8 def stop(self): for pwm in self.pwms: pwm.deinit() self.stopped = true def tick(self): if (not self.stopped): self.timer = self.timer + 1 #loop if (self.timer % (self.tempo * self.end) == 0 and (not (self.timer == 0))): if (not self.looping): self.stop() return false self.beat = -1 self.timer = 0 #on beat if (self.timer % self.tempo == 0): self.beat = self.beat + 1 #remove expired notes from playing list i = 0 while (i < len(self.playingdurations)): self.playingdurations[i] = self.playingdurations[i] - 1 if (self.playingdurations[i] len(self.pwms)): self.pwms[len(self.pwms)-1].duty_u16(self.duty) if (self.arpnote > len(self.playingnotes)-len(self.pwms)): self.arpnote = 0 self.pwms[len(self.pwms)-1].freq(tones[self.playingnotes[self.arpnote+(len(self.pwms)-1)]]) self.arpnote = self.arpnote + 1 return true else: return false  
接下来就是导入乐谱,并进行播放了。下面我找到了一段 fc 红白机上「超级玛丽」游戏的 bgm 的乐谱。将下面的代码保存在 pico 上,命名为 main.py。
from buzzer_music import musicfrom time import sleep # 超级玛丽 bgm 乐谱song = '0 e5 1 0;0 f#4 1 0;0 d4 1 0;2 e5 2 0;2 d4 2 0;6 e5 2 0;6 d4 2 0;10 d4 1 0;10 f#4 1 0;10 c5 1 0;2 f#4 2 0;6 f#4 2 0;12 e5 2 0;12 f#4 2 0;12 d4 2 0;16 g5 2 0;16 g4 2 0;16 b4 2 0;23 g4 2 0;23 g4 2 0;30 c5 3 0;30 e4 3 0;30 g4 3 0;36 g4 1 0;36 c4 1 0;36 e4 1 0;42 e4 3 0;42 g3 3 0;42 c4 3 0;48 a4 2 0;48 f4 2 0;48 c4 2 0;52 b4 2 0;52 g4 2 0;52 d4 2 0;58 a4 2 0;58 c4 2 0;58 f4 2 0;56 a#4 1 0;56 f#4 1 0;56 c#4 1 0;62 c4 3 0;62 g4 3 0;62 e4 3 0;65 g4 3 0;65 e5 3 0;65 c5 3 0;68 e5 3 0;68 g5 3 0;68 b4 3 0;71 a5 2 0;71 c5 2 0;71 f5 2 0;75 d5 1 0;77 e5 2 0;75 a4 1 0;77 b4 2 0;75 f5 1 0;77 g5 2 0;81 e5 2 0;81 a4 2 0;81 c5 2 0;85 e4 1 0;85 c5 1 0;85 a4 1 0;87 f4 1 0;87 b4 1 0;87 d5 1 0;89 b4 1 0;89 g4 1 0;89 d4 1 0;95 c5 3 0;95 e4 3 0;95 g4 3 0;101 g4 1 0;101 c4 1 0;101 e4 1 0;107 e4 3 0;107 g3 3 0;107 c4 3 0;113 a4 2 0;113 f4 2 0;113 c4 2 0;117 b4 2 0;117 g4 2 0;117 d4 2 0;123 a4 2 0;123 c4 2 0;123 f4 2 0;121 a#4 1 0;121 f#4 1 0;121 c#4 1 0;127 c4 3 0;127 g4 3 0;127 e4 3 0;130 g4 3 0;130 e5 3 0;130 c5 3 0;133 e5 3 0;133 g5 3 0;133 b4 3 0;136 a5 2 0;136 c5 2 0;136 f5 2 0;140 d5 1 0;142 e5 2 0;140 a4 1 0;142 b4 2 0;140 f5 1 0;142 g5 2 0;146 e5 2 0;146 a4 2 0;146 c5 2 0;150 e4 1 0;150 c5 1 0;150 a4 1 0;152 f4 1 0;152 b4 1 0;152 d5 1 0;154 b4 1 0;154 g4 1 0;154 d4 1 0;159 c4 4 0;162 g5 1 0;162 e5 1 0;164 f#5 1 0;164 d#5 1 0;164 g4 1 0;166 f5 1 0;166 d5 1 0;168 d#5 2 0;168 b4 2 0;172 e5 2 0;172 c5 2 0;176 g#4 1 0;176 e4 1 0;178 f4 1 0;178 a4 1 0;180 g4 2 0;180 c5 2 0;184 a4 1 0;184 c4 1 0;186 c5 1 0;188 f4 1 0;188 d5 1 0;170 c5 2 0;174 f4 2 0;182 c5 1 0;186 e4 1 0;190 c4 3 0;207 f5 2 0;207 g5 2 0;207 c6 2 0;211 f5 1 0;211 g5 1 0;211 c6 1 0;213 f5 2 0;213 g5 2 0;213 c6 2 0;217 g4 2 0;221 c4 3 0;193 g5 1 0;193 e5 1 0;195 f#5 1 0;195 d#5 1 0;195 g4 1 0;197 f5 1 0;197 d5 1 0;199 d#5 2 0;199 b4 2 0;203 e5 2 0;203 c5 2 0;224 g5 1 0;224 e5 1 0;226 f#5 1 0;226 d#5 1 0;226 g4 1 0;228 f5 1 0;228 d5 1 0;230 d#5 2 0;230 b4 2 0;234 e5 2 0;234 c5 2 0;240 f4 1 0;238 e4 1 0;238 g#4 1 0;240 a4 1 0;242 g4 2 0;242 c5 2 0;246 c4 1 0;246 a4 1 0;248 e4 1 0;248 c5 1 0;250 d5 1 0;250 f4 1 0;256 d#5 3 0;256 g#4 3 0;262 d5 1 0;262 f4 1 0;267 e4 2 0;267 c5 2 0;252 c4 1 0;273 g4 1 0;275 g4 2 0;279 d4 2 0;283 c4 4 0;286 g5 1 0;286 e5 1 0;288 f#5 1 0;288 d#5 1 0;288 g4 1 0;290 f5 1 0;290 d5 1 0;292 d#5 2 0;292 b4 2 0;296 e5 2 0;296 c5 2 0;300 g#4 1 0;300 e4 1 0;302 f4 1 0;302 a4 1 0;304 g4 2 0;304 c5 2 0;308 a4 1 0;308 c4 1 0;310 c5 1 0;312 f4 1 0;312 d5 1 0;294 c5 2 0;298 f4 2 0;306 c5 1 0;310 e4 1 0;314 c4 3 0;331 f5 2 0;331 g5 2 0;331 c6 2 0;335 f5 1 0;335 g5 1 0;335 c6 1 0;337 f5 2 0;337 g5 2 0;337 c6 2 0;317 g5 1 0;317 e5 1 0;319 f#5 1 0;319 d#5 1 0;319 g4 1 0;321 f5 1 0;321 d5 1 0;323 d#5 2 0;323 b4 2 0;327 e5 2 0;327 c5 2 0;341 g4 2 0;345 c4 3 0;348 g5 1 0;348 e5 1 0;350 f#5 1 0;350 d#5 1 0;350 g4 1 0;352 f5 1 0;352 d5 1 0;354 d#5 2 0;354 b4 2 0;358 e5 2 0;358 c5 2 0;364 f4 1 0;362 e4 1 0;362 g#4 1 0;364 a4 1 0;366 g4 2 0;366 c5 2 0;370 c4 1 0;370 a4 1 0;372 e4 1 0;372 c5 1 0;374 d5 1 0;374 f4 1 0;380 d#5 3 0;380 g#4 3 0;386 d5 1 0;386 f4 1 0;391 e4 2 0;391 c5 2 0;376 c4 1 0;397 g4 1 0;399 g4 2 0;403 d4 2 0;407 c5 1 0;407 g#4 1 0;409 a4 2 0;409 c5 2 0;413 a4 2 0;413 c5 2 0;407 g#3 3 0;413 e4 1 0;417 c5 1 0;417 g#4 1 0;419 c#5 2 0;419 a#4 2 0;423 e5 1 0;423 g4 1 0;425 c5 2 0;425 e4 2 0;429 e4 1 0;429 a4 1 0;431 g4 2 0;431 c4 2 0;438 c5 1 0;438 g#4 1 0;440 c5 2 0;440 a4 2 0;444 c5 2 0;444 a4 2 0;448 c5 1 0;459 g4 2 0;463 c5 1 0;463 g#4 1 0;465 a4 2 0;465 c5 2 0;469 a4 2 0;469 c5 2 0;463 g#3 3 0;469 e4 1 0;473 c5 1 0;473 g#4 1 0;475 c#5 2 0;475 a#4 2 0;479 e5 1 0;479 g4 1 0;481 c5 2 0;481 e4 2 0;485 e4 1 0;485 a4 1 0;487 g4 2 0;487 c4 2 0;454 g4 2 0' find a piece of music on onlinesequencer.net, click edit,then select all notes with ctrl+a and copy them with ctrl+cpaste string as shown above after removing ;: fromthe end and online sequencer from the start from machine import pin mysong = music(song, pins=[pin(20)]) #four buzzers#mysong = music(song, pins=[pin(0),pin(1),pin(2),pin(3)]) while true: mysong.tick() sleep(0.03)  
给树莓派 pico 上电之后,音乐会开始播放。


国产封装材料代表厂商晨日科技发布2020年半年度报告
三星加大5G投资 华为遭遇强劲对手
Arduino Pro IDE你终于长大了!
创新科技保驾护航:地下车库智能监控系统助您安心停放
简述18年底至19年初DUI一共经历的3次大版本更新
树莓派Pico上使用无源蜂鸣器
云计算的历史/类型/优势/部署方法
感受科技与体育的碰撞:杭州亚运会智能办赛中的机器人亮点
PLC能在库房管理项目中发挥什么作用?
阿里巴巴发展机器人有什么样优势?
高精度双路相敏放大器的设计
我国工业互联网标识解析系统风险分析
飞兆推出内置3.3V低压降稳压器集成式低边栅极驱动器FAN3180
量子计算的商用之路还要多久才走到
2023世亚数博会,世亚软博会,加速推动数字产业“生根开花”
物联网重新定义制造业的9种方式
采用MSP430与PROFIBUS总线构建监测子站
单片机网络数据传输器
浅谈AI深度学习之于先进封装的重要性
浅析频谱分析仪的相位噪声和扫描时间