还记得我费尽周折用PRI.GPIO通过香蕉派M3点亮一个LED嘛?好吧,其实我家里并不缺灯泡,LED手电筒这种家用电器我也有好几个,况且现在手机啥的都有手电筒功能,所以点亮个LED其实并没有什么大用。
其实我是初衷是弄一个蜂鸣器当作报警器,我明明记得我有好几个那种一通电就哇哇响的蜂鸣器,可是找了半天没找到,只好先用LED代替了。
然而问题来了,当我成功点亮LED后,我还是没有找到那种通电哇哇响的蜂鸣器,这下可麻爪了。还好最终我找到一大堆所谓的无源蜂鸣器,看看如何用这个凑合吧。
原理
百度一下无源蜂鸣器:
无源蜂鸣器利用电磁感应现象,为音圈接入交变电流后形成的电磁铁与永磁铁相吸或相斥而推动振膜发声,接入直流电只能持续推动振膜而无法产生声音,只能在接通或断开时产生声音。
百度一下声音的频率:
声音作为一种波,频率在20 Hz~20 kHz之间的声音是可以被人耳识别的。
所以要想让无源蜂鸣器的发声就得以20 Hz~20 kHz的频率通断电。
代码一
要做到这点也挺简单,只要用之前文章种控制GPIO的代码改改就可以实现了
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
def beep(pin, frq, duty):
c = 1 / frq
h = c / 100 * duty
l = c - h
GPIO.output(pin, GPIO.HIGH)
time.sleep(h)
GPIO.output(pin, GPIO.LOW)
time.sleep(l)
while True:
beep(11, 60000, 50)
#time.sleep(1)
#beep(11, 12000, 50)
#time.sleep(1)
代码2 PWM
如果就是驱动蜂鸣器,我们已经成功了,上述代码运行后,蜂鸣器会发出哔哔的声音。
可是如何我们想多做一些工作,比如说打开上述代码中注释掉的部分,发现代码并不如我们意料的那样工作,这是因为我们发出beep声音的是通过sleep控制周期实现的,而并非单独的硬件或者单独的线程。
那么如果想实现类似上述功能该怎么办呢?我不知道香蕉派有没有硬件PWM,即便有我也不知道咋控制,我也懒得去写多线程代码,来实现复杂控制。
好在RPI.GPIO中实现了一个PWM功能(我不确定是软件方式还是硬件方式),使用PWM功能重新实现上述代码:
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
Buzz = GPIO.PWM(11, 6000)
Buzz.start(50)
while True:
Buzz.ChangeFrequency(6000)
time.sleep(1)
Buzz.ChangeFrequency(12000)
time.sleep(1)
不但功能好用了,代码也简洁不少呢!
补充说明
因为不是硬件PWM(我猜测的),所以估计精度不是那么准确,声音不太正。
如果去研究香蕉派M3的硬件PWM,先不说硬件是否支持,如何控制学起来估计也挺费劲的。
考虑直接用Arduino控制蜂鸣器,然后香蕉派通过GPIO给Arduino控制信号来决定关闭还是打开蜂鸣器,不过这样又多了一些工作量,纠结呀。
参考资料
https://steemit.com/~witnesses type in
oflyhigh
and click VOTE
Vote @oflyhigh via Steemconnect
Thank you!
恭喜你!您的这篇文章入选 @justyy 今日 (2019-06-25) 榜单 【优秀的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
@justyy 是CN区的见证人,请支持他,给他投票,感谢!
Congratulations! This post has been selected by @justyy as today's (2019-06-25) 【Good Posts】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^
SteemIt 工具、API接口、机器人和教程
SteemIt Tools, Bots, APIs and Tutorial
*Join cnsteem Discord channel: *https://discord.gg/SnNaaYS
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thx
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
听说树莓派4出来了。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
是呀是呀,可惜现在还买不到。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
用pwm驱动蜂鸣器,频率的精度不需要很高。如果蜂鸣器的声音不太正常,基本上是因为驱动的频率不对。图片上的这种蜂鸣器一般驱动的中心频率是2k或者4k
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
哇,专家来啦
用2K、4K频率试了一下,效果还是不佳😳
不过这样的蜂鸣器用Arduino的硬件PWM驱动,声音就好听多啦(以前弄过)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
哈哈哈,我这是雕虫小技,比不上大神啊!
因为没有datasheet,就不好判断了,有源的一般是电流驱动能力不够,你这个无源的,如果不是频率问题,就是驱动电压不够了,大婶可以试试用两个引脚分别驱动蜂鸣器的两端,相位相反,我一般用这种方法来提升响度。
Posted using Partiko Android
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you so much for participating in the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@oflyhigh Can I ask you something . Are you interested in small steemit project , this will improve your witness position for sure.without any investment , but definitely you will be benefited with this project as it will highlight you in steemit. You can find me on discord adityajainxds#3203. Just listen to the project if you like you can join else no problem.i can explain you here also in the comment. Thanks have a nice day.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit