QC

Piezo Christmas Songs

For a digital Christmas, let your Arduino play songs with a piezo buzzer.

Devices and components

Arduino Uno Rev3

Resistance 330 ohms

10k ohm resistance

12mm push button switch

Buzzer

Project description

Arduino Christmas Songs

arduino

1/*
2 Arduino Christmas Songs
3
4 Based on a project and code by Dipto Pratyaksa, updated on 31/3/13
5
6 Modified for Christmas by Joshi, on Dec 17th, 2017.
7*/
8
9#include "pitches.h"
10
11#define melodyPin 9
12
13// Jingle Bells
14
15int melody[] = {
16 NOTE_E5, NOTE_E5, NOTE_E5,
17 NOTE_E5, NOTE_E5, NOTE_E5,
18 NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
19 NOTE_E5,
20 NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
21 NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
22 NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
23 NOTE_D5, NOTE_G5
24};
25
26int tempo[] = {
27 8, 8, 4,
28 8, 8, 4,
29 8, 8, 8, 8,
30 2,
31 8, 8, 8, 8,
32 8, 8, 8, 16, 16,
33 8, 8, 8, 8,
34 4, 4
35};
36
37// We wish you a merry Christmas
38
39int wish_melody[] = {
40 NOTE_B3,
41 NOTE_F4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4,
42 NOTE_D4, NOTE_D4, NOTE_D4,
43 NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4,
44 NOTE_E4, NOTE_E4, NOTE_E4,
45 NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4,
46 NOTE_F4, NOTE_D4, NOTE_B3, NOTE_B3,
47 NOTE_D4, NOTE_G4, NOTE_E4,
48 NOTE_F4
49};
50
51int wish_tempo[] = {
52 4,
53 4, 8, 8, 8, 8,
54 4, 4, 4,
55 4, 8, 8, 8, 8,
56 4, 4, 4,
57 4, 8, 8, 8, 8,
58 4, 4, 8, 8,
59 4, 4, 4,
60 2
61};
62
63// Santa Claus is coming to town
64
65int santa_melody[] = {
66 NOTE_G4,
67 NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
68 NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, NOTE_C5,
69 NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
70 NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4,
71 NOTE_E4, NOTE_G4, NOTE_C4, NOTE_E4,
72 NOTE_D4, NOTE_F4, NOTE_B3,
73 NOTE_C4
74};
75
76int santa_tempo[] = {
77 8,
78 8, 8, 4, 4, 4,
79 8, 8, 4, 4, 4,
80 8, 8, 4, 4, 4,
81 8, 8, 4, 2,
82 4, 4, 4, 4,
83 4, 2, 4,
84 1
85};
86
87int switchOne = 0;
88int switchTwo = 0;
89int switchThree = 0;
90
91void setup(void) {
92 pinMode(9, OUTPUT); // Buzzer
93 pinMode(13, OUTPUT); // Led indicator when singing a note
94 pinMode(2, INPUT);
95 pinMode(3, INPUT);
96 pinMode(4, INPUT);
97}
98
99void loop() {
100 switchOne = digitalRead(2);
101 switchTwo = digitalRead(3);
102 switchThree = digitalRead(4);
103 if (switchOne == HIGH) {
104 sing(1);
105 } else if (switchTwo == HIGH) {
106 sing(2);
107 } else if (switchThree == HIGH) {
108 sing(3);
109 }
110}
111
112int song = 0;
113
114void sing(int s) {
115 // iterate over the notes of the melody:
116 song = s;
117 if (song == 3) {
118 Serial.println(" 'We wish you a Merry Christmas'");
119 int size = sizeof(wish_melody) / sizeof(int);
120 for (int thisNote = 0; thisNote < size; thisNote++) {
121
122 // to calculate the note duration, take one second
123 // divided by the note type.
124 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
125 int noteDuration = 1000 / wish_tempo[thisNote];
126
127 buzz(melodyPin, wish_melody[thisNote], noteDuration);
128
129 // to distinguish the notes, set a minimum time between them.
130 // the note's duration + 30% seems to work well:
131 int pauseBetweenNotes = noteDuration * 1.30;
132 delay(pauseBetweenNotes);
133
134 // stop the tone playing:
135 buzz(melodyPin, 0, noteDuration);
136
137 }
138 } else if (song == 2) {
139 Serial.println(" 'Santa Claus is coming to town'");
140 int size = sizeof(santa_melody) / sizeof(int);
141 for (int thisNote = 0; thisNote < size; thisNote++) {
142
143 // to calculate the note duration, take one second
144 // divided by the note type.
145 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
146 int noteDuration = 900 / santa_tempo[thisNote];
147
148 buzz(melodyPin, santa_melody[thisNote], noteDuration);
149
150 // to distinguish the notes, set a minimum time between them.
151 // the note's duration + 30% seems to work well:
152 int pauseBetweenNotes = noteDuration * 1.30;
153 delay(pauseBetweenNotes);
154
155 // stop the tone playing:
156 buzz(melodyPin, 0, noteDuration);
157
158 }
159 } else {
160
161 Serial.println(" 'Jingle Bells'");
162 int size = sizeof(melody) / sizeof(int);
163 for (int thisNote = 0; thisNote < size; thisNote++) {
164
165 // to calculate the note duration, take one second
166 // divided by the note type.
167 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
168 int noteDuration = 1000 / tempo[thisNote];
169
170 buzz(melodyPin, melody[thisNote], noteDuration);
171
172 // to distinguish the notes, set a minimum time between them.
173 // the note's duration + 30% seems to work well:
174 int pauseBetweenNotes = noteDuration * 1.30;
175 delay(pauseBetweenNotes);
176
177 // stop the tone playing:
178 buzz(melodyPin, 0, noteDuration);
179
180 }
181 }
182}
183
184void buzz(int targetPin, long frequency, long length) {
185 digitalWrite(13, HIGH);
186 long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
187 //// 1 second's worth of microseconds, divided by the frequency, then split in half since
188 //// there are two phases to each cycle
189 long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
190 //// multiply frequency, which is really cycles per second, by the number of seconds to
191 //// get the total number of cycles to produce
192 for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
193 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
194 delayMicroseconds(delayValue); // wait for the calculated delay value
195 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
196 delayMicroseconds(delayValue); // wait again or the calculated delay value
197 }
198 digitalWrite(13, LOW);
199
200}

pitches.h

arduino

1/*************************************************
2 * Public Constants
3 *************************************************/
4
5#define NOTE_B0 31
6#define NOTE_C1 33
7#define NOTE_CS1 35
8#define NOTE_D1 37
9#define NOTE_DS1 39
10#define NOTE_E1 41
11#define NOTE_F1 44
12#define NOTE_FS1 46
13#define NOTE_G1 49
14#define NOTE_GS1 52
15#define NOTE_A1 55
16#define NOTE_AS1 58
17#define NOTE_B1 62
18#define NOTE_C2 65
19#define NOTE_CS2 69
20#define NOTE_D2 73
21#define NOTE_DS2 78
22#define NOTE_E2 82
23#define NOTE_F2 87
24#define NOTE_FS2 93
25#define NOTE_G2 98
26#define NOTE_GS2 104
27#define NOTE_A2 110
28#define NOTE_AS2 117
29#define NOTE_B2 123
30#define NOTE_C3 131
31#define NOTE_CS3 139
32#define NOTE_D3 147
33#define NOTE_DS3 156
34#define NOTE_E3 165
35#define NOTE_F3 175
36#define NOTE_FS3 185
37#define NOTE_G3 196
38#define NOTE_GS3 208
39#define NOTE_A3 220
40#define NOTE_AS3 233
41#define NOTE_B3 247
42#define NOTE_C4 262
43#define NOTE_CS4 277
44#define NOTE_D4 294
45#define NOTE_DS4 311
46#define NOTE_E4 330
47#define NOTE_F4 349
48#define NOTE_FS4 370
49#define NOTE_G4 392
50#define NOTE_GS4 415
51#define NOTE_A4 440
52#define NOTE_AS4 466
53#define NOTE_B4 494
54#define NOTE_C5 523
55#define NOTE_CS5 554
56#define NOTE_D5 587
57#define NOTE_DS5 622
58#define NOTE_E5 659
59#define NOTE_F5 698
60#define NOTE_FS5 740
61#define NOTE_G5 784
62#define NOTE_GS5 831
63#define NOTE_A5 880
64#define NOTE_AS5 932
65#define NOTE_B5 988
66#define NOTE_C6 1047
67#define NOTE_CS6 1109
68#define NOTE_D6 1175
69#define NOTE_DS6 1245
70#define NOTE_E6 1319
71#define NOTE_F6 1397
72#define NOTE_FS6 1480
73#define NOTE_G6 1568
74#define NOTE_GS6 1661
75#define NOTE_A6 1760
76#define NOTE_AS6 1865
77#define NOTE_B6 1976
78#define NOTE_C7 2093
79#define NOTE_CS7 2217
80#define NOTE_D7 2349
81#define NOTE_DS7 2489
82#define NOTE_E7 2637
83#define NOTE_F7 2794
84#define NOTE_FS7 2960
85#define NOTE_G7 3136
86#define NOTE_GS7 3322
87#define NOTE_A7 3520
88#define NOTE_AS7 3729
89#define NOTE_B7 3951
90#define NOTE_C8 4186
91#define NOTE_CS8 4435
92#define NOTE_D8 4699
93#define NOTE_DS8 4978

Arduino Christmas Songs

arduino

1/*
2 Arduino Christmas Songs
3
4 Based on a project and code
5 by Dipto Pratyaksa, updated on 31/3/13
6
7 Modified for Christmas by Joshi,
8 on Dec 17th, 2017.
9*/
10
11#include "pitches.h"
12
13#define melodyPin
14 9
15
16// Jingle Bells
17
18int melody[] = {
19 NOTE_E5, NOTE_E5, NOTE_E5,
20
21 NOTE_E5, NOTE_E5, NOTE_E5,
22 NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
23 NOTE_E5,
24
25 NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
26 NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
27
28 NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
29 NOTE_D5, NOTE_G5
30};
31
32int tempo[]
33 = {
34 8, 8, 4,
35 8, 8, 4,
36 8, 8, 8, 8,
37 2,
38 8, 8, 8, 8,
39 8,
40 8, 8, 16, 16,
41 8, 8, 8, 8,
42 4, 4
43};
44
45// We wish you a merry Christmas
46
47int
48 wish_melody[] = {
49 NOTE_B3,
50 NOTE_F4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4,
51
52 NOTE_D4, NOTE_D4, NOTE_D4,
53 NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4,
54
55 NOTE_E4, NOTE_E4, NOTE_E4,
56 NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4,
57
58 NOTE_F4, NOTE_D4, NOTE_B3, NOTE_B3,
59 NOTE_D4, NOTE_G4, NOTE_E4,
60 NOTE_F4
61};
62
63int
64 wish_tempo[] = {
65 4,
66 4, 8, 8, 8, 8,
67 4, 4, 4,
68 4, 8, 8, 8, 8,
69
70 4, 4, 4,
71 4, 8, 8, 8, 8,
72 4, 4, 8, 8,
73 4, 4, 4,
74 2
75};
76
77//
78 Santa Claus is coming to town
79
80int santa_melody[] = {
81 NOTE_G4,
82 NOTE_E4,
83 NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
84 NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, NOTE_C5,
85
86 NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4,
87 NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4,
88
89 NOTE_E4, NOTE_G4, NOTE_C4, NOTE_E4,
90 NOTE_D4, NOTE_F4, NOTE_B3,
91 NOTE_C4
92};
93
94int
95 santa_tempo[] = {
96 8,
97 8, 8, 4, 4, 4,
98 8, 8, 4, 4, 4,
99 8, 8, 4,
100 4, 4,
101 8, 8, 4, 2,
102 4, 4, 4, 4,
103 4, 2, 4,
104 1
105};
106
107int switchOne
108 = 0;
109int switchTwo = 0;
110int switchThree = 0;
111
112void setup(void) {
113
114 pinMode(9, OUTPUT); // Buzzer
115 pinMode(13, OUTPUT); // Led indicator when
116 singing a note
117 pinMode(2, INPUT);
118 pinMode(3, INPUT);
119 pinMode(4, INPUT);
120}
121
122void
123 loop() {
124 switchOne = digitalRead(2);
125 switchTwo = digitalRead(3);
126 switchThree
127 = digitalRead(4);
128 if (switchOne == HIGH) {
129 sing(1);
130 } else if (switchTwo
131 == HIGH) {
132 sing(2);
133 } else if (switchThree == HIGH) {
134 sing(3);
135
136 }
137}
138
139int song = 0;
140
141void sing(int s) {
142 // iterate over the
143 notes of the melody:
144 song = s;
145 if (song == 3) {
146 Serial.println("
147 'We wish you a Merry Christmas'");
148 int size = sizeof(wish_melody) / sizeof(int);
149
150 for (int thisNote = 0; thisNote < size; thisNote++) {
151
152 // to calculate
153 the note duration, take one second
154 // divided by the note type.
155 //e.g.
156 quarter note = 1000 / 4, eighth note = 1000/8, etc.
157 int noteDuration =
158 1000 / wish_tempo[thisNote];
159
160 buzz(melodyPin, wish_melody[thisNote],
161 noteDuration);
162
163 // to distinguish the notes, set a minimum time between
164 them.
165 // the note's duration + 30% seems to work well:
166 int pauseBetweenNotes
167 = noteDuration * 1.30;
168 delay(pauseBetweenNotes);
169
170 // stop the
171 tone playing:
172 buzz(melodyPin, 0, noteDuration);
173
174 }
175 } else
176 if (song == 2) {
177 Serial.println(" 'Santa Claus is coming to town'");
178
179 int size = sizeof(santa_melody) / sizeof(int);
180 for (int thisNote = 0;
181 thisNote < size; thisNote++) {
182
183 // to calculate the note duration, take
184 one second
185 // divided by the note type.
186 //e.g. quarter note =
187 1000 / 4, eighth note = 1000/8, etc.
188 int noteDuration = 900 / santa_tempo[thisNote];
189
190
191 buzz(melodyPin, santa_melody[thisNote], noteDuration);
192
193 // to
194 distinguish the notes, set a minimum time between them.
195 // the note's duration
196 + 30% seems to work well:
197 int pauseBetweenNotes = noteDuration * 1.30;
198
199 delay(pauseBetweenNotes);
200
201 // stop the tone playing:
202 buzz(melodyPin,
203 0, noteDuration);
204
205 }
206 } else {
207
208 Serial.println(" 'Jingle
209 Bells'");
210 int size = sizeof(melody) / sizeof(int);
211 for (int thisNote
212 = 0; thisNote < size; thisNote++) {
213
214 // to calculate the note duration,
215 take one second
216 // divided by the note type.
217 //e.g. quarter note
218 = 1000 / 4, eighth note = 1000/8, etc.
219 int noteDuration = 1000 / tempo[thisNote];
220
221
222 buzz(melodyPin, melody[thisNote], noteDuration);
223
224 // to distinguish
225 the notes, set a minimum time between them.
226 // the note's duration + 30%
227 seems to work well:
228 int pauseBetweenNotes = noteDuration * 1.30;
229 delay(pauseBetweenNotes);
230
231
232 // stop the tone playing:
233 buzz(melodyPin, 0, noteDuration);
234
235
236 }
237 }
238}
239
240void buzz(int targetPin, long frequency, long length) {
241
242 digitalWrite(13, HIGH);
243 long delayValue = 1000000 / frequency / 2; // calculate
244 the delay value between transitions
245 //// 1 second's worth of microseconds,
246 divided by the frequency, then split in half since
247 //// there are two phases
248 to each cycle
249 long numCycles = frequency * length / 1000; // calculate the
250 number of cycles for proper timing
251 //// multiply frequency, which is really
252 cycles per second, by the number of seconds to
253 //// get the total number of
254 cycles to produce
255 for (long i = 0; i < numCycles; i++) { // for the calculated
256 length of time...
257 digitalWrite(targetPin, HIGH); // write the buzzer pin
258 high to push out the diaphram
259 delayMicroseconds(delayValue); // wait for
260 the calculated delay value
261 digitalWrite(targetPin, LOW); // write the buzzer
262 pin low to pull back the diaphram
263 delayMicroseconds(delayValue); // wait
264 again or the calculated delay value
265 }
266 digitalWrite(13, LOW);
267
268}




Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.

Easy remote control

Remotely control an LED with ease. Devices and components Arduino Nano 33 BLE with headers Box 525 Resistors precision 1% - 17 values Breadb...