Arduino驱动无源蜂鸣器发声

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Arduino驱动⽆源蜂鸣器发声
tone()函数
tone(pin, frequency)
tone(pin, frequency, duration)
# 参数
pin: the pin on which to generate the tone
frequency: the frequency of the tone in hertz - unsigned int
duration: the duration of the tone in milliseconds (optional) - unsigned long
tone()的使⽤说明
产⽣指定频率的占空⽐为50%的⽅波. 可以指定持续时间, 若未指定, 则持续到调⽤noTone().
在同⼀时间只能产⽣⼀个频率的⽅波, 如果tone正执⾏在⼀个pin上, 在其他pin上调⽤tone()将不起作⽤. 如果在同⼀个pin上再次调⽤tone, 则会使⽤新指定的频率.
除了Mega开发板以外, 使⽤tone()会与pin 3 和 pin 11 脚的 PWM输出冲突.
输出的最低频率为31Hz.
noTone函数
noTone(pin)
# 参数
pin: the pin on which to stop generating the tone
注意: 如果在不同的pin脚上有多个喇叭/蜂鸣器, 在对下⼀个pin调⽤tone()前必须对前⼀个pin调⽤noTone().
接线
蜂鸣器的+脚接Arduino D6, -脚接GND
测试代码
#define TONEPIN 6
#define TONE_BASE 294
void setup() {
pinMode(TONEPIN, OUTPUT);
}
void loop() {
for(int i = 0; i < 120; i ++) {
tone(TONEPIN, TONE_BASE + i * 15, 100);
delay(2000);
noTone(TONEPIN);
}
delay(2000);
}
测试可以看到, tone⽅法是⾮阻塞的, 如果delay时间⽐tone的duration短, 则duration不起作⽤, 实际时间是delay的时间.。

相关文档
最新文档