0% found this document useful (0 votes)
192 views

Buzzer Con Arduino

This document describes a project to play the Super Mario Bros theme song using an Arduino, piezo buzzer, and code. It includes details on the hardware needed, including an Arduino, piezo buzzer, 1k resistor, and breadboard. It provides instructions on connecting the components and uploading the code to the Arduino. The code contains the musical notes for the Mario theme song and Underworld theme song represented by constants, and uses PWM to buzz the piezo and play the songs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views

Buzzer Con Arduino

This document describes a project to play the Super Mario Bros theme song using an Arduino, piezo buzzer, and code. It includes details on the hardware needed, including an Arduino, piezo buzzer, 1k resistor, and breadboard. It provides instructions on connecting the components and uploading the code to the Arduino. The code contains the musical notes for the Mario theme song and Underworld theme song represented by constants, and uses PWM to buzz the piezo and play the songs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Super Mario theme song w/ piezo buzzer

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

An Arduino(I used an Arduino Nano, any other is fine)


Piezo buzzer
1 k ohm resistor(any resistor between 333 ohm to 1 k should be fine in this project)
A breadboard
Some breadboard cable(s)

Connecting the components


If you have an Arduino Uno(which most people have), connect the components with the help of
the image below. If you have an Arduino Nano, look the the image in Using an Arduino
Nano.
Connect the positive side of the buzzer to digital 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.
Remember to connect the buzzer the right way, the buzzer has positive and negative pins!
So basically the buzzer, 1 k ohm resistor and Arduino should be connected like this:
Arduino digital pin 3 > Buzzer > 1 k ohm resisotor > Arduino ground(GND) pin.

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.

Using an Arduino Uno


Below is an illustration of how to connect the buzzer and resistor to an Arduino Uno.

Uploading the code


There is a pretty huge amount of code in this Sketch. Just press the Copy button on the top
right of the code text field for it to automatically highlight the whole code for you.

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

51 #define NOTE_GS3 208


52 #define NOTE_A3 220
53 #define NOTE_AS3 233
54 #define NOTE_B3 247
55 #define NOTE_C4 262
56 #define NOTE_CS4 277
57 #define NOTE_D4 294
58 #define NOTE_DS4 311
59 #define NOTE_E4 330
60 #define NOTE_F4 349
61 #define NOTE_FS4 370
62 #define NOTE_G4 392
63 #define NOTE_GS4 415
64 #define NOTE_A4 440
65 #define NOTE_AS4 466
66 #define NOTE_B4 494
67 #define NOTE_C5 523
68 #define NOTE_CS5 554
69 #define NOTE_D5 587
70 #define NOTE_DS5 622
71 #define NOTE_E5 659
72 #define NOTE_F5 698
73 #define NOTE_FS5 740
74 #define NOTE_G5 784
75 #define NOTE_GS5 831
76 #define NOTE_A5 880
77 #define NOTE_AS5 932
78 #define NOTE_B5 988
79 #define NOTE_C6 1047
80 #define NOTE_CS6 1109
81 #define NOTE_D6 1175
82 #define NOTE_DS6 1245
83 #define NOTE_E6 1319
84 #define NOTE_F6 1397
85 #define NOTE_FS6 1480
86 #define NOTE_G6 1568
87 #define NOTE_GS6 1661
88 #define NOTE_A6 1760
89 #define NOTE_AS6 1865
90 #define NOTE_B6 1976
91 #define NOTE_C7 2093
92 #define NOTE_CS7 2217
93 #define NOTE_D7 2349
94 #define NOTE_DS7 2489
95 #define NOTE_E7 2637
96 #define NOTE_F7 2794
97 #define NOTE_FS7 2960
98 #define NOTE_G7 3136
99 #define NOTE_GS7 3322
100 #define NOTE_A7 3520

101 #define NOTE_AS7 3729


102 #define NOTE_B7 3951
103 #define NOTE_C8 4186
104 #define NOTE_CS8 4435
105 #define NOTE_D8 4699
106 #define NOTE_DS8 4978
107
108 #define melodyPin 3
109 //Mario main theme melody
110 int melody[] = {
111 NOTE_E7, NOTE_E7, 0, NOTE_E7,
112 0, NOTE_C7, NOTE_E7, 0,
113 NOTE_G7, 0, 0, 0,
114 NOTE_G6, 0, 0, 0,
115
116 NOTE_C7, 0, 0, NOTE_G6,
117 0, 0, NOTE_E6, 0,
118 0, NOTE_A6, 0, NOTE_B6,
119 0, NOTE_AS6, NOTE_A6, 0,
120
121 NOTE_G6, NOTE_E7, NOTE_G7,
122 NOTE_A7, 0, NOTE_F7, NOTE_G7,
123 0, NOTE_E7, 0, NOTE_C7,
124 NOTE_D7, NOTE_B6, 0, 0,
125
126 NOTE_C7, 0, 0, NOTE_G6,
127 0, 0, NOTE_E6, 0,
128 0, NOTE_A6, 0, NOTE_B6,
129 0, NOTE_AS6, NOTE_A6, 0,
130
131 NOTE_G6, NOTE_E7, NOTE_G7,
132 NOTE_A7, 0, NOTE_F7, NOTE_G7,
133 0, NOTE_E7, 0, NOTE_C7,
134 NOTE_D7, NOTE_B6, 0, 0
135 };
136 //Mario main them tempo
137 int tempo[] = {
138 12, 12, 12, 12,
139 12, 12, 12, 12,
140 12, 12, 12, 12,
141 12, 12, 12, 12,
142
143 12, 12, 12, 12,
144 12, 12, 12, 12,
145 12, 12, 12, 12,
146 12, 12, 12, 12,
147
148 9, 9, 9,
149 12, 12, 12, 12,
150 12, 12, 12, 12,

151 12, 12, 12, 12,


152
153 12, 12, 12, 12,
154 12, 12, 12, 12,
155 12, 12, 12, 12,
156 12, 12, 12, 12,
157
158 9, 9, 9,
159 12, 12, 12, 12,
160 12, 12, 12, 12,
161 12, 12, 12, 12,
162 };
163 //Underworld melody
164 int underworld_melody[] = {
165 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
166 NOTE_AS3, NOTE_AS4, 0,
167 0,
168 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
169 NOTE_AS3, NOTE_AS4, 0,
170 0,
171 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
172 NOTE_DS3, NOTE_DS4, 0,
173 0,
174 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
175 NOTE_DS3, NOTE_DS4, 0,
176 0, NOTE_DS4, NOTE_CS4, NOTE_D4,
177 NOTE_CS4, NOTE_DS4,
178 NOTE_DS4, NOTE_GS3,
179 NOTE_G3, NOTE_CS4,
180 NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
181 NOTE_GS4, NOTE_DS4, NOTE_B3,
182 NOTE_AS3, NOTE_A3, NOTE_GS3,
183 0, 0, 0
184 };
185 //Underwolrd tempo
186 int underworld_tempo[] = {
187 12, 12, 12, 12,
188 12, 12, 6,
189 3,
190 12, 12, 12, 12,
191 12, 12, 6,
192 3,
193 12, 12, 12, 12,
194 12, 12, 6,
195 3,
196 12, 12, 12, 12,
197 12, 12, 6,
198 6, 18, 18, 18,
199 6, 6,
200 6, 6,

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'");

251 int size = sizeof(melody) / sizeof(int);


252 for (int thisNote = 0; thisNote < size; thisNote++) {
253
254
// to calculate the note duration, take one second
255
// divided by the note type.
256
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
257
int noteDuration = 1000 / tempo[thisNote];
258
259
buzz(melodyPin, melody[thisNote], noteDuration);
260
261
// to distinguish the notes, set a minimum time between them.
262
// the note's duration + 30% seems to work well:
263
int pauseBetweenNotes = noteDuration * 1.30;
264
delay(pauseBetweenNotes);
265
266
// stop the tone playing:
267
buzz(melodyPin, 0, noteDuration);
268
269 }
270 }
271 }
272
273 void buzz(int targetPin, long frequency, long length) {
274 digitalWrite(13, HIGH);
275 long delayValue = 1000000 / frequency / 2; // calculate the delay value between
276 transitions
277 //// 1 second's worth of microseconds, divided by the frequency, then split in half since
278 //// there are two phases to each cycle
279 long numCycles = frequency * length / 1000; // calculate the number of cycles for proper
280 timing
281 //// multiply frequency, which is really cycles per second, by the number of seconds to
282 //// get the total number of cycles to produce
283 for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
284 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
285 delayMicroseconds(delayValue); // wait for the calculated delay value
286 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
287 delayMicroseconds(delayValue); // wait again or the calculated delay value
288 }
289 digitalWrite(13, LOW);
}
After uploading the Sketch-code, your Arduino should start playing!

You might also like