In this tutorial, we have to control buzzer using an Arduino board
and
play birthday tone.
A Buzzer or beeper is used for produces
sound. Uses of buzzer include security, alarm devices, and mouse click or
keystroke sound. We need Arduino to control a frequency of buzzer and produces
different sound to change frequency and duration.
Control
a buzzer using Arduino uno.
Requirement:-
1. Arduino Uno.
2. Active Buzzer.
3. Breadboard.
4. Jumper wire.
Circuit:-
Steps:-
1. Piezo buzzer having 2 pin, negative
and positive.
2. Connect a jumper wire from positive
pin of buzzer to Arduino pin 8.
3. Then connect a jumper wire from
negative pin of buzzer to Arduino GND pin.
4. Upload the code…..
5. Keep practices, to change frequency
and duration to create a musical tone.
Code:-
int buzzer = 8;
void setup()
{
// put your setup
code here, to run once:
pinMode(buzzer, OUTPUT);//sets the digital pin as OUTPUT
}
void loop()
{
// put your main
code here, to run repeatedly:
//creating a more notes by changing frequency AND duration
int notes[10]={234,324,432,543,232,267,876,368,123,980};
//an array having different frequency
for(int i = 0; i< 10; i++)
{
tone(buzzer,
notes[i]);
delay(1000);
}
noTone(buzzer);//
stops tone
delay(1000);
}
Control
a 3 LEDs, buzzer, and button using Arduino uno and playing birthday tone.
Requirement:-
1. Arduino Uno.
2. Active Buzzer.
3. 1 x 220 ohms Resistor.
4. 1 x 10k ohms Resistor.
5. 3 x LEDs 5mm.
6. Push button.
7. Breadboard.
8. Jumper wire.
Circuit:-
Steps:-
1. Piezo buzzer having 2 pin, negative
and positive.
2. Connect a jumper wire from positive
pin of buzzer to Arduino pin 2.
3. Then connect a jumper wire from
negative pin of buzzer to Arduino GND pin.
4. Attach a 220 ohms resistor to the
positive pin (anode) of all LEDs and at the end of the resistor is connected to
Arduino digital pin 5, 6 and 7 respectively.
5. Connecting Button
pin 4 to Arduino pin 5v using a jumper wire.
6. Attach a 10k ohms
resistor to button pin 2 and at the end of the resistor is connected to Arduino
pin GND.
7. Button pin 1 is
connected to Arduino digital pin 4 using a jumper wire.
8. Now circuit is readyyy…….. enjoy it…..
Note:-If you need different tone like birthday tone, then comment
it.
Code: -
//constant will be not change
const int speakerPin = 2;
const int led1 = 5;
const int led2 = 6;
const int led3 = 7;
const int button = 12;
int buttonState = 0;
int length = 28; // the number of notes
char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8,8, 16, 1,
2,2,8,8,8,8,16, 1,2,2,8,8,8,16 };
int tempo = 150;
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',
'c',
'd', 'e', 'f', 'g', 'a', 'b',
'x',
'y' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
956, 834, 765,
593, 468, 346,
224,
655 ,
715 };
int SPEE = 5;
// play the tone corresponding to the note name
for (int i = 0; i < 17; i++)
{
if (names[i] ==
note)
{
int newduration =
duration/SPEE;
playTone(tones[i], newduration);
}
}
}
void setup() {
pinMode(button, INPUT); //sets a pin as a input
pinMode(speakerPin, OUTPUT);// //sets a pin as a Output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
buttonState =
digitalRead(button);
if (buttonState ==
HIGH)
{
for(int k =0;
k< 2; k++ )
{
digitalWrite(led1,
HIGH);
digitalWrite(led2,
HIGH);
digitalWrite(led3,
HIGH);
for (int i = 0; i < length; i++)
{
if (notes[i] == '
') {
delay(beats[i] *
tempo); // rest
}
else
{
playNote(notes[i], beats[i] * tempo);
}
// pause between
notes
delay(tempo);
}
}
}
else
{
// turn LED off:
noTone(speakerPin);
digitalWrite(led1,
LOW);
digitalWrite(led2,
LOW);
digitalWrite(led3,
LOW);
}
}
Watch this................Tutorail.
Watch this................Tutorail.
hello. why my led not blinking
ReplyDeleteHeyy..
DeleteHe is using multicolor leds that why
how music is playing?
ReplyDeletedo you have a another songs?
ReplyDeleteplease help me!
ReplyDeleteWrite the Arduino sketch with the following functionality:
When the Arduino is powered up, the red LED
connected to pin 9 will be turned on. When the pushbutton
connected to pin 2 is pressed, the green LED connected to
pin 10 will be turned on and the red LED will be turned off.
There will be a message in the Serial Monitor tool “Let’s listen
to the melody.”, and the melody will be played. The piezo
buzzer must be connected to pin 8. Pressing the pushbutton
repeatedly while the melody is playing will have no effect
unless it has finished playing.
After the melody is finished playing, the green LED will be
turned off, the red LED will be turned on again, and there
will be a message in the next line after the first message in
the Serial Monitor tool “Hope you enjoy!”. If the pushbutton
is pressed again, the melody will be played again.
Use the following specifications in your sketch:
int melody[] = {_E6, _D6, _C6, _D6, _E6, _E6, _E6, _D6, _D6,
_D6, _E6, _E6, _E6, _E6, _D6, _C6, _D6, _E6,
_E6, _E6, _C6, _D6, _D6, _E6, _D6, _C6};
int noteDurations[] = {4, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 1};
if (notes[i] == ' ') {
ReplyDeleteI got error in this line... What to do