Buzzer Con Arduino
Buzzer Con Arduino
and Arduino!
by Prince 06/07/2014
Today I found a complete post on how to play Super Mario Bros theme song on a piezo buzzer!
Its very simple and fun, and great as a beginner Arduino project.
All fame goes to Dipto Pratyaksa for making the Sketch code and sharing it with us!
I modified the code posted by Dipto and added the PWM-pitches in directly into the Sketch, so
you dont have to mess around with Arduino libraries!
Original post:
http://www.linuxcircle.com/2013/03/31/playing-mario-bros-tune-with-arduino-and-piezobuzzer/
Contents [hide]
1 Hardware requirements
2 Connecting the components
o 2.1 Using an Arduino Uno
o 2.2 Using an Arduino Nano
3 Uploading the code
Hardware requirements
You can actually do without the 1 k ohm resistor! If you connect without the resistor, the
buzzer will be a lot louder, and the sound quality might degrade. But you can also lower the
resistance to get a little louder sound, and keep the sound quality. Another idea is using a
potentiometer instead of a resistor to act as a volume controller! For this tutorial well just be
using a 1 k ohm resistor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Arduino Mario Bros Tunes
With Piezo Buzzer and PWM
Connect the positive side of the Buzzer to pin 3,
then the negative side to a 1k ohm resistor. Connect
the other side of the 1 k ohm resistor to
ground(GND) pin on the Arduino.
by: Dipto Pratyaksa
last updated: 31/3/13
*/
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
201 6, 6,
202 18, 18, 18, 18, 18, 18,
203 10, 10, 10,
204 10, 10, 10,
205 3, 3, 3
206 };
207
208 void setup(void)
209 {
210 pinMode(3, OUTPUT);//buzzer
211 pinMode(13, OUTPUT);//led indicator when singing a note
212
213 }
214 void loop()
215 {
216 //sing the tunes
217 sing(1);
218 sing(1);
219 sing(2);
220 }
221 int song = 0;
222
223 void sing(int s) {
224 // iterate over the notes of the melody:
225 song = s;
226 if (song == 2) {
227 Serial.println(" 'Underworld Theme'");
228 int size = sizeof(underworld_melody) / sizeof(int);
229 for (int thisNote = 0; thisNote < size; thisNote++) {
230
231
// to calculate the note duration, take one second
232
// divided by the note type.
233
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
234
int noteDuration = 1000 / underworld_tempo[thisNote];
235
236
buzz(melodyPin, underworld_melody[thisNote], noteDuration);
237
238
// to distinguish the notes, set a minimum time between them.
239
// the note's duration + 30% seems to work well:
240
int pauseBetweenNotes = noteDuration * 1.30;
241
delay(pauseBetweenNotes);
242
243
// stop the tone playing:
244
buzz(melodyPin, 0, noteDuration);
245
246 }
247
248 } else {
249
250 Serial.println(" 'Mario Theme'");