Difference between revisions of "Buzzer Alarm"

From Microduino Wiki
Jump to: navigation, search
(Created page with "{| style="width: 800px;" |- | ==Purpose== This coure will show you the way to use Microduino buzzer to play music. ==Equipment== *'''Microduino-CoreUSB''' *'''Microdu...")
 
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{Language| Buzzer Alarm}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
 
|
 
|
==Purpose==
+
==Objective==
  
This coure will show you the way to use Microduino buzzer to play music.  
+
This tutorial will show you how to use Microduino-buzzer with three examples.
 +
[[File:MicroduinoBuzzer.jpg|600px|center]]
  
 
==Equipment==
 
==Equipment==
Line 10: Line 12:
 
*'''[[Microduino-BUZZER]]'''
 
*'''[[Microduino-BUZZER]]'''
 
*'''[[Microduino-Sensorhub]]'''
 
*'''[[Microduino-Sensorhub]]'''
 +
*Other hardware equipment
 +
**USB cable  One
 +
[[File:MicroduinoBuzzer-Equipment.jpg|600px|center]]
  
*Other Hardware Equipment
+
==Description==
**USB cable  One
+
Generally, buzzer can be divided into active buzzer and passive buzzer.
 +
*Active buzzer
 +
**Internal oscillating and driving circuit. With voltage signal(high level), the buzzer can make sound.
 +
***Advantage: Easy to use。
 +
***Disadvantage: only one single tone left since fixed frequency.
 +
*Passive buzzer
 +
**Since there is no internal oscillating source, the buzzer can't make sound with DC signal. It has to be driven by changing power signal.
 +
***Advantage: With controllable sound frequency, you can achieve the effect of seven musical notes.
 +
***Disadvantage: A little hard to control.
  
==Buzzer Description==
+
==Example 1: Let the buzzer goes off==
Buzzer includes active buzzer and passive buzzer. Active buzzer has polarity like LED diode, whose long pin connects to VCC and the short pin to GND. With that connection, active buzzer can make continous sound. While the passive buzzer has no polarity. With three pins consisting of two power pins and an audio input pin, the passive buzzer can only make sound in audio output circuit. In this experiment, we adopt passive buzzer.
+
===Build module circuit===
 +
*Setup 1: Connect the Buzzer sensor to interface(6) on the Sensorhub module with connecting line.  
 +
[[File:MicroduinoBuzzer-D6.jpg|600px|center]]
 +
*Setup 2: Stack the Sensorhub and the CoreUSB together.
 +
[[File:MicroduinoBuzzer-hub-coreusb.jpg|600px|center]]
  
==Program==
+
===Debugging===
*Program code
+
*Setup 1: Use usb cable to connect CoreUSB and PC/Mac and then open Arduino IDE.
 +
[[File:CoreUSB_Ble_pc.jpg|600px|center]]
 +
* Setup 2: Download the code and copy it to IDE.
 
<source lang="cpp">
 
<source lang="cpp">
int song[] ={
+
#define buzzer_pin 6 //Define buzzer pin
    262,262,294,262,349,330,
+
#define buzzer_fre 600 //Define output frequency
    262,262,294,262,392,349,
+
 
    262,262,523,440,349,330,294,
+
void setup()
     494,494,440,349,392,349
+
{
};
+
  pinMode(buzzer_pin,OUTPUT);
 
+
}
int noteDurations[] = {
+
  4,4,2,2,2,1,
+
void loop()
  4,4,2,2,2,1,
+
{
  4,4,2,2,2,2,2,
+
     tone(buzzer_pin,buzzer_fre);    //Drive the buzzer
  4,4,2,2,2,1
+
}
};
+
</source>
 +
Select Microduino-CoreUSB as the board card as well as corresponding port(COMXX). Click the right arrow(—>)and download program. When you see upload finish notice, it means the program has been written into CoreUSB.  Then, you'll hear buzzer goes off. 
 +
*"tone(pin,fre)" Function
 +
**Fixed pin (pin) and output frequency (fre) signal.
 +
**You can change the value of buzzer_fre and watch buzzer change.
 +
==Example 2: Buzzer makes alarm==
 +
*Copy program code to IDE and download it to Core board.
 +
<source lang="cpp">
 +
#define buzzer_pin 6 //Define buzzer pin
  
void setup() {
+
void setup()
   for (int thisNote = 0; thisNote<25;thisNote++)
+
{
 +
  pinMode(buzzer_pin,OUTPUT);
 +
}
 +
 +
void loop()
 +
{
 +
   for(int i=200;i<=800;i++)  //Increase frequency from 200HZ to 800HZ in a circular manner.
 +
  {
 +
    tone(buzzer_pin,i);    //Output frequency in Number four port.
 +
    delay(5);      //The frequency lasts 5ms.
 +
  }
 +
  delay(2000);    //The highest frequency lasts 2s.
 +
  for(int i=800;i>=200;i--)
 
   {
 
   {
    int noteDuration = 1000/noteDurations[thisNote];
+
     tone(buzzer_pin,i);
     tone(8, song[thisNote],noteDuration);
+
     delay(10); //The frequency lasts 10ms.
    int pauseBetweenNotes = noteDuration *1.30;
 
     delay(pauseBetweenNotes);
 
    noTone(8);
 
 
   }
 
   }
}
 
void loop() {
 
  setup();
 
 
}
 
}
 
</source>
 
</source>
 +
"for(int i=200;i<=800;i++)" Description: The value of 'i' is 200 initially, which increases 1 each time the 'for' function executes and it will exit from 'for' loop until the value reaches 800. Users can change parameters and watch buzzer change.
  
*Function description
+
<br>The example adopts 'for' loop to change frequency and therefore make alarm.  
The music coming from Arduino board is controlled by tone(). The change of the function can change the rythem of the music. The code includes two forms—tone(pin, frequency, duration) and tone(pin, frequency).
 
● In the first function, “pin” represents the pin that connects a loudspeaker.
 
● If using the sencond function, then you alos need noTone() to ccontrol the music.
 
  
==Debugging==
+
==Example 3: Play music==
 +
 
 +
*Program code
 +
<source lang="cpp">
 +
#define buzzer_pin 6 //Define buzzer pin
 +
int song[] = {
 +
  262, 262, 294, 262, 349, 330,
 +
  262, 262, 294, 262, 392, 349,
 +
  262, 262, 523, 440, 349, 330, 294,
 +
  494, 494, 440, 349, 392, 349
 +
};
  
Step 1:
+
int noteDurations[] = {
Insert Microduino buzzer to D8 pin of Microduino-Sensorhub. 
+
  4, 4, 2, 2, 2, 1,
[[File:MicroduinoBuzzerSensor.png|600px|center|thumb]]
+
  4, 4, 2, 2, 2, 1,
 +
  4, 4, 2, 2, 2, 2, 2,
 +
  4, 4, 2, 2, 2, 1
 +
};
  
Step 2:
+
void setup() {
Connect your PC with USB cable, download the code and program it to Microduino-CoreUSB.
+
  pinMode(buzzer_pin, OUTPUT);
[[File:MicroduinoBuzzerSensor1.png|600px|center|thumb]]
+
}
  
Step 3:
+
void loop() {
Power on and you’ll hear the music.
+
  song_play();
[[File:MicroduinoBuzzerSensor2.png|600px|center|thumb]]
+
}
  
==Result==
+
void song_play()
 +
{
 +
  for (int thisNote = 0; thisNote < 25; thisNote++)
 +
  {
 +
    int noteDuration = 1000 / noteDurations[thisNote];
 +
    tone(buzzer_pin, song[thisNote], noteDuration);
 +
    int pauseBetweenNotes = noteDuration * 1.20;
 +
    delay(pauseBetweenNotes);
 +
    noTone(buzzer_pin);
 +
  }
 +
}</source>
 +
' song_play()' is music play function and the note (frequency) is saved in ' song[]'. ' noteDurations[]' represents rhythm.
 +
<br>Music is controlled by tone() and change this function means changing music rhythm. Two types of code: tone(pin, frequency, duration) or tone(pin, frequency)
 +
<br> Seen from the first function, 'pin' represents the pin connecting the speaker and 'frequency' represents sound frequency and 'duration' means time duration (The unit is 'ms').
 +
<br> If using the second function, you need another noTone() function to control noTone(pin) and stop music.
 +
*Connect the activated buttery box and BM module, stack them together and finishing buildup.
 +
[[File:MicroduinoBuzzer-Equipment-ok.jpg|600px|center]]
  
You can use buzzer to play music.
+
|}

Latest revision as of 07:25, 4 August 2017

Language: English  • 中文

Objective

This tutorial will show you how to use Microduino-buzzer with three examples.

MicroduinoBuzzer.jpg

Equipment

MicroduinoBuzzer-Equipment.jpg

Description

Generally, buzzer can be divided into active buzzer and passive buzzer.

  • Active buzzer
    • Internal oscillating and driving circuit. With voltage signal(high level), the buzzer can make sound.
      • Advantage: Easy to use。
      • Disadvantage: only one single tone left since fixed frequency.
  • Passive buzzer
    • Since there is no internal oscillating source, the buzzer can't make sound with DC signal. It has to be driven by changing power signal.
      • Advantage: With controllable sound frequency, you can achieve the effect of seven musical notes.
      • Disadvantage: A little hard to control.

Example 1: Let the buzzer goes off

Build module circuit

  • Setup 1: Connect the Buzzer sensor to interface(6) on the Sensorhub module with connecting line.
MicroduinoBuzzer-D6.jpg
  • Setup 2: Stack the Sensorhub and the CoreUSB together.
MicroduinoBuzzer-hub-coreusb.jpg

Debugging

  • Setup 1: Use usb cable to connect CoreUSB and PC/Mac and then open Arduino IDE.
CoreUSB Ble pc.jpg
  • Setup 2: Download the code and copy it to IDE.
#define buzzer_pin 6 //Define buzzer pin
#define buzzer_fre 600 //Define output frequency

void setup()
{
  pinMode(buzzer_pin,OUTPUT);
}
 
void loop()
{
    tone(buzzer_pin,buzzer_fre);    //Drive the buzzer 
}

Select Microduino-CoreUSB as the board card as well as corresponding port(COMXX). Click the right arrow(—>)and download program. When you see upload finish notice, it means the program has been written into CoreUSB. Then, you'll hear buzzer goes off.

  • "tone(pin,fre)" Function
    • Fixed pin (pin) and output frequency (fre) signal.
    • You can change the value of buzzer_fre and watch buzzer change.

Example 2: Buzzer makes alarm

  • Copy program code to IDE and download it to Core board.
#define buzzer_pin 6 //Define buzzer pin 

void setup()
{
  pinMode(buzzer_pin,OUTPUT);
}
 
void loop()
{
  for(int i=200;i<=800;i++)  //Increase frequency from 200HZ to 800HZ in a circular manner.
  {
    tone(buzzer_pin,i);    //Output frequency in Number four port. 
    delay(5);      //The frequency lasts 5ms. 
  }
  delay(2000);     //The highest frequency lasts 2s.
  for(int i=800;i>=200;i--)
  {
    tone(buzzer_pin,i);
    delay(10); //The frequency lasts 10ms. 
  }
}

"for(int i=200;i<=800;i++)" Description: The value of 'i' is 200 initially, which increases 1 each time the 'for' function executes and it will exit from 'for' loop until the value reaches 800. Users can change parameters and watch buzzer change.


The example adopts 'for' loop to change frequency and therefore make alarm.

Example 3: Play music

  • Program code
#define buzzer_pin 6 //Define buzzer pin 
int song[] = {
  262, 262, 294, 262, 349, 330,
  262, 262, 294, 262, 392, 349,
  262, 262, 523, 440, 349, 330, 294,
  494, 494, 440, 349, 392, 349
};

int noteDurations[] = {
  4, 4, 2, 2, 2, 1,
  4, 4, 2, 2, 2, 1,
  4, 4, 2, 2, 2, 2, 2,
  4, 4, 2, 2, 2, 1
};

void setup() {
  pinMode(buzzer_pin, OUTPUT);
}

void loop() {
  song_play();
}

void song_play()
{
  for (int thisNote = 0; thisNote < 25; thisNote++)
  {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzer_pin, song[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.20;
    delay(pauseBetweenNotes);
    noTone(buzzer_pin);
  }
}

' song_play()' is music play function and the note (frequency) is saved in ' song[]'. ' noteDurations[]' represents rhythm.
Music is controlled by tone() and change this function means changing music rhythm. Two types of code: tone(pin, frequency, duration) or tone(pin, frequency)
Seen from the first function, 'pin' represents the pin connecting the speaker and 'frequency' represents sound frequency and 'duration' means time duration (The unit is 'ms').
If using the second function, you need another noTone() function to control noTone(pin) and stop music.

  • Connect the activated buttery box and BM module, stack them together and finishing buildup.
MicroduinoBuzzer-Equipment-ok.jpg