Exercise 6 - Connecting a buzzer
1. INTRODUCTION
Buzzers, concretely, piezoelectric buzzers may be driven by an oscillating electronic circuit or other audio signal source.
There are two types of buzzers:
PASSIVE
|
|
With a "passive" device or speaker, you have to send it an AC "sound signal" and you can control the sound (PWM in Arduino). Arduino needs to generate the tone, this takes more processor timing.
|
ACTIVE
|
|
On the other hand, active buzzer can generate tone, you only need to energize it.
|
2. HOW TO CONNECT?
Materials:
· Arduino UNO board
· Protoboard
· Some wire Jumpers
· Passive buzzer
· Potentiometer
In this exercise, you are going to use a buzzer module, which has a passive buzzer inside. The module has three terminals: Vcc(5V), GND(Ground), I/O(Input/output, this terminal is that generates the tone).
In buzzer with two terminals, you can change the tone actuating on positive.
EXERCISE_A (Variating tone with potentiometer) |
EXERCISE_B (Building a song) |
3. PROGRAMMING
EXERCISE_A (Variating tone with potentiometer)
This program uses tone()function to change frequency on passive buzzer using a potentiometer. Also, send analog read and conversion to hertz by serial communication.
First buzzer and potentiometer have been associated to digital(PWM) and analog pins respectively
int buzzer=9;
int pot=A0;
void setup(){
pinMode(7,OUTPUT);
//To avoid to connect in pararel buzzer and potentiometer
//pin 7 is always high (5 volts).
digitalWrite(7,HIGH);
pinMode(buzzer,OUTPUT);
pinMode(pot,INPUT);
Serial.begin(9600);
}
In loop value variable equals to potentiometer read. It’s necessary to change the range with map() function, because tone() function works with hertz.
void loop(){
int value=analogRead(pot);//Read the value of potentiometer
Serial.print("Input: ");
Serial.print(value);
Serial.print(" ");
value=map(value,0,1023,31,20000);//This function converts
//range of analogs values (0-1023)
//to range of frecuency (0-20000Hz)
Serial.print("Output: ");
Serial.println(value);
tone(buzzer,value);//This function translate
//the Hz value to the buzzer
delay(100);
}
EXERCISE_B (Building a song)
This program uses tone()function to change frequency on active buzzer. First, you should declare notes storing in int variables for example, however you can define them as e.g. “#define Do 262;”
int buzzer=9;
int Do=262;
int Re=293;
int Mi=330;
int Fa=349;
int FaSos=370;
int Sol=392;
int La=440;
int Si=493;
int SiSos=523;
To simplify the process of creating a song, you can make a notes vector and a duration of notes vector. Both should have the same length.
int song[]={Do,Re,Mi,Do,Mi,Do,Mi,
Re,Mi,Fa,Fa,Mi,Re,Fa,Mi,Fa,Sol,
Mi,Sol,Mi,Sol,Fa,Sol,La,La,Sol,Fa,
La,Sol,Do,Re,Mi,Fa,Sol,La,0,La,Re,Mi,
FaSos,Sol,La,Si,0,Si,Mi,Fa,Sol,La,Si,
SiSos,0,Si,Si,La,Fa,Si,Sol,SiSos,Sol,Mi,
Re,SiSos,0,Do,Re,Mi,Fa,Sol,La,Si,SiSos
};
//Duration in quarters.
float duration[]={2,4,2,4,2,2,1,2,4,
4,4,4,4,0.69,2,4,2,4,2,2,1,2,
4,4,4,4,4,0.69,2,4,4,4,4,4,0.69,4,
2,4,4,4,4,0.69,4,4,4,2,2,2,2,2,2,2,
2,0.69,2,4,4,4,4,4,4,4,0.69
};
To generalize the algorithm its necessary to create a variable to calculate vector length. “z” equals to the total size of vector divided by the size of individual int. The result is the length of the vector.
int z=sizeof(song)/sizeof(int);
int note=0;
void setup(){
pinMode(buzzer,OUTPUT);
Serial.begin(9600);
}
In void loop() for loop is necessary to play the song, it increments note one by one. In each loop one note is playing in the buzzer.
Note is a variable, which have been declared to follow the song and duration vector position one by one.
void loop(){
for(note=0;note<=z;note++){
//converts duration from musical duration to milliseconds
//quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration=1000/duration[note];
tone(buzzer,song[note],noteDuration);
It’s recommended between notes to pause sometimes
int pause=noteDuration*1.30;
delay(pause);
// stop the tone playing:
noTone(buzzer);
Serial.print("Note number: ");
Serial.print(song[note]);
Serial.print(" ");
Serial.print("duracion: ");
Serial.println(noteDuration);
}
//When song finish program waits 4 seconds and it starts another time
if(note>=z){
note=0;
delay(4000);
}
}