无源蜂鸣器

我们已经在有源蜂鸣器部分介绍过有源蜂鸣器,因此这里只介绍无源蜂鸣器。

无源蜂鸣器没有震荡源,因此需要通过脉宽调制(PWM)输入特定的方波电压信号来发声。PWM原理可在其他教程中查找,本系列主要介绍传感器。
演奏一首音乐,最重要的每个音的音调(频率)和节拍(时长)。

C调音符对应的音调频率如下图:(每两个音调频率相差

音符 1 1# 2 2# 4 4# 5 5# 6 6# 7
C调低音 262 277 294 311 330 349 370 392 415 440 466 494
C调中音 523 554 587 622 659 698 740 784 831 880 932 988
C调高音 1046 1109 1175 1245 1318 1397 1480 1568 1661 1760 1865 1976

我们可以通过tone(pin,frequnce)函数来控制无源蜂鸣器的振动频率,利用delay()来控制节拍,利用noTone(pin)来终止振动。

接下来控制无源蜂鸣器演奏《天空之城》。天空之城是C调,一拍为四分之一音符长度,每一小节为四拍。

黑点在上为高音,在下为低音,没有为中音乐。而每两个竖杠之间为一小节,每一小节共有四个四分之一拍。下面如果画了一条横线,则是四分之一拍的二分之一,即八分之一拍;画了两条横线,则是四分之一的四分之一,即十六分之一拍。音符后面的横线则是前一音符的延续。

引脚说明

接线方式如下:

模块引脚 Stduino 开发板管脚
- GND
+ 5V
S D8

必需的组件

你将需要以下组件:

  • 1×无源蜂鸣器
  • 1×Stduino UNO/Nano

连接

按照电路图连接面包主板的组件,如下图所示。

Stduino代码

控制有源蜂鸣器每隔三秒蜂鸣一次,每次持续一秒。

#include <Arduino.h>
#define C0 (2)

#define C1_LOW  (262)
#define C2_LOW  (294)
#define C3_LOW  (330)
#define C4_LOW  (349)
#define C5_LOW  (392)
#define C6_LOW  (440)
#define C7_LOW  (494)

#define C1      (523)
#define C2      (587)
#define C3      (659)
#define C4      (698)
#define C4S     (740)//C4#
#define C5      (784)
#define C5S     (831)//C5#
#define C6      (880)
#define C7      (988)

#define C1_HIGH (1046)
#define C2_HIGH (1175)
#define C3_HIGH (1318)
#define C4_HIGH (1397)
#define C5_HIGH (1568)
#define C6_HIGH (1760)
#define C7_HIGH (1976)

#define WHOLE (1)
#define HALF (0.5)
#define QUARTER (0.25)
#define EIGHTH (0.125)
#define SIXTEENTH (0.0625)

int tune[]=                 //根据简谱列出各频率
{
    C6,C7,

    C1_HIGH,C7,C1_HIGH,C3_HIGH,//
    C7,C3,C3,
    C6,C5,C6,C1_HIGH,
    C5,C0,C3,C3,
    C4,C3,C4,C1_HIGH,
    C3,C0,C1_HIGH,C1_HIGH,C1_HIGH,
    C7,C4S,C4S,C7,

    C7,C0,C6,C7,//
    C1_HIGH,C7,C1_HIGH,C3_HIGH,
    C7,C0,C3,C3,
    C6,C5,C6,C1_HIGH,
    C5,C0,C3,
    C4,C1_HIGH,C7,C7,C1_HIGH,
    C2_HIGH,C2_HIGH,C3_HIGH,C1_HIGH,C0,

    C1_HIGH,C7,C6,C6,C7,C5S,//
    C6,C0,C1_HIGH,C2_HIGH,
    C3_HIGH,C2_HIGH,C3_HIGH,C5_HIGH,
    C2_HIGH,C0,C5,C5,
    C1_HIGH,C7,C1_HIGH,C3_HIGH,C0,C0,
    C6,C7,C1_HIGH,C7,C2_HIGH,C2_HIGH,
    C1,C5,C5,C0,

    C4_HIGH,C3_HIGH,C2_HIGH,C1_HIGH,//
    C3_HIGH,
    C3_HIGH,C0,C3_HIGH,
    C6_HIGH,C5_HIGH,C5_HIGH,
    C3_HIGH,C2_HIGH,C1_HIGH,C0,C1_HIGH,
    C2_HIGH,C1_HIGH,C2_HIGH,C2_HIGH,C5_HIGH,
    C3_HIGH,C0,C3_HIGH,
    C6_HIGH,C5_HIGH,
    C3_HIGH,C2_HIGH,C1_HIGH,C0,C1_HIGH,
    C2_HIGH,C1_HIGH,C2_HIGH,C2_HIGH,C7,

    C6,C0,C6,C7,

    C6
};
float durt[]=                   //根据简谱列出各节拍
{
    EIGHTH,EIGHTH,

    QUARTER + EIGHTH,EIGHTH,QUARTER,QUARTER,
    QUARTER + QUARTER + QUARTER,EIGHTH,EIGHTH,
    QUARTER + EIGHTH,EIGHTH,QUARTER,QUARTER,
    QUARTER + QUARTER,QUARTER,EIGHTH,EIGHTH,
    QUARTER + EIGHTH,EIGHTH,EIGHTH,QUARTER+EIGHTH,
    QUARTER + QUARTER,QUARTER,EIGHTH,EIGHTH,EIGHTH,
    QUARTER + EIGHTH,EIGHTH,QUARTER,QUARTER,

    QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
    QUARTER+EIGHTH,EIGHTH,QUARTER,QUARTER,
    QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
    QUARTER+EIGHTH,EIGHTH,QUARTER,QUARTER,
    QUARTER+QUARTER+QUARTER,EIGHTH,EIGHTH,
    QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,
    EIGHTH,EIGHTH,EIGHTH,QUARTER,QUARTER,

    QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,QUARTER,
    QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
    QUARTER+EIGHTH,QUARTER,QUARTER,QUARTER,
    QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,
    EIGHTH,EIGHTH,QUARTER,QUARTER,
    QUARTER+QUARTER,QUARTER,QUARTER,
    EIGHTH,EIGHTH,QUARTER,QUARTER,EIGHTH,EIGHTH,
    QUARTER+EIGHTH,EIGHTH,QUARTER,QUARTER,

    QUARTER,QUARTER,QUARTER,QUARTER,
    QUARTER+QUARTER+QUARTER,
    QUARTER+QUARTER,QUARTER,QUARTER,
    QUARTER+QUARTER,QUARTER,QUARTER,
    EIGHTH,EIGHTH,QUARTER,EIGHTH,EIGHTH,
    QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,
    QUARTER+QUARTER,QUARTER,QUARTER,
    QUARTER+QUARTER,QUARTER+QUARTER,
    EIGHTH,EIGHTH,QUARTER+QUARTER,EIGHTH,EIGHTH,
    QUARTER,EIGHTH,EIGHTH,EIGHTH,QUARTER,
    QUARTER+QUARTER,QUARTER,EIGHTH,EIGHTH,

    QUARTER+QUARTER+QUARTER

};

int Time = 1000;
int BUZZER = A0;

int length;

void setup() 
{
  // put your setup code here, to run once:
    length=sizeof(tune)/sizeof(tune[0]);   //计算长度
}

void loop()
{
  // put your main code here, to run repeatedly:
    for(int i=0;i<length;i++)
    {
        if(tune[i]!=0)
        {
        tone(BUZZER,tune[i]);
        }  
        else
        {
            noTone(BUZZER);
        }
        delay(Time*durt[i]); //这里用来乐曲速度。
    }
    delay(2000);
}

结果

将代码下载带Stduino Nano/UNO中,你就可以听到优美而熟悉的天空之城的旋律了!

文档更新时间: 2021-02-14 14:29   作者:admin