Devices & Components
Arduino Uno Rev3
Buzzer
Jumper wires (generic)
Project description
Code
Main Code
c_cpp
This is the main code.
1/* 25/09/2022 2 * Code by Ilaria Brambilla */ 3 4 5 6 7#include "pitches.h" // including the library with the frequencies of the note 8 9int melody[] = { 10 NOTE_C4, NOTE_A3, NOTE_A3, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_AS3, NOTE_A3, NOTE_C4, NOTE_D4, NOTE_D4 11}; // declaring the notes of the melody (they change depending on the song you wanna play) 12 13int durations[] = { 14 2, 2, 3, 3, 3, 2, 3, 3, 2, 1, 3, 3, 3, 5, 5, 5, 1, 2, 1, 3, 3, 3, 3, 1 15}; // declaring the duration of each note (4 is a quarter note ecc) 16 17int songLength = sizeof(melody)/sizeof(melody[0]); // defining the song length, in this case it is equal to the length of the melody 18 19void setup () { 20 for (int thisNote = 0; thisNote < songLength; thisNote ++) { 21 int duration = 1000/ durations[thisNote]; 22 tone(8, melody[thisNote], duration); 23 int pause = duration * 1.5; // adding a little delay in between each note to separate the sounds 24 delay(pause); 25 noTone(8); 26} 27} 28void loop() { 29 // no need to loop the melody 30}
Pitches Library
c_cpp
This is the pitches library you'll need for the buzzer to play each frequency of the notes. To use it you'll need to create another tab using the little arrow in the upper right of the screen.
1 2#define NOTE_B0 31 3#define NOTE_C1 33 4#define NOTE_CS1 35 5#define NOTE_D1 37 6#define NOTE_DS1 39 7#define NOTE_E1 41 8#define NOTE_F1 44 9#define NOTE_FS1 46 10#define NOTE_G1 49 11#define NOTE_GS1 52 12#define NOTE_A1 55 13#define NOTE_AS1 58 14#define NOTE_B1 62 15#define NOTE_C2 65 16#define NOTE_CS2 69 17#define NOTE_D2 73 18#define NOTE_DS2 78 19#define NOTE_E2 82 20#define NOTE_F2 87 21#define NOTE_FS2 93 22#define NOTE_G2 98 23#define NOTE_GS2 104 24#define NOTE_A2 110 25#define NOTE_AS2 117 26#define NOTE_B2 123 27#define NOTE_C3 131 28#define NOTE_CS3 139 29#define NOTE_D3 147 30#define NOTE_DS3 156 31#define NOTE_E3 165 32#define NOTE_F3 175 33#define NOTE_FS3 185 34#define NOTE_G3 196 35#define NOTE_GS3 208 36#define NOTE_A3 220 37#define NOTE_AS3 233 38#define NOTE_B3 247 39#define NOTE_C4 262 40#define NOTE_CS4 277 41#define NOTE_D4 294 42#define NOTE_DS4 311 43#define NOTE_E4 330 44#define NOTE_F4 349 45#define NOTE_FS4 370 46#define NOTE_G4 392 47#define NOTE_GS4 415 48#define NOTE_A4 440 49#define NOTE_AS4 466 50#define NOTE_B4 494 51#define NOTE_C5 523 52#define NOTE_CS5 554 53#define NOTE_D5 587 54#define NOTE_DS5 622 55#define NOTE_E5 659 56#define NOTE_F5 698 57#define NOTE_FS5 740 58#define NOTE_G5 784 59#define NOTE_GS5 831 60#define NOTE_A5 880 61#define NOTE_AS5 932 62#define NOTE_B5 988 63#define NOTE_C6 1047 64#define NOTE_CS6 1109 65#define NOTE_D6 1175 66#define NOTE_DS6 1245 67#define NOTE_E6 1319 68#define NOTE_F6 1397 69#define NOTE_FS6 1480 70#define NOTE_G6 1568 71#define NOTE_GS6 1661 72#define NOTE_A6 1760 73#define NOTE_AS6 1865 74#define NOTE_B6 1976 75#define NOTE_C7 2093 76#define NOTE_CS7 2217 77#define NOTE_D7 2349 78#define NOTE_DS7 2489 79#define NOTE_E7 2637 80#define NOTE_F7 2794 81#define NOTE_FS7 2960 82#define NOTE_G7 3136 83#define NOTE_GS7 3322 84#define NOTE_A7 3520 85#define NOTE_AS7 3729 86#define NOTE_B7 3951 87#define NOTE_C8 4186 88#define NOTE_CS8 4435 89#define NOTE_D8 4699 90#define NOTE_DS8 4978
Downloadable files
Connection
In this case the buzzer is attached to pin 8
Connection

Comments
Only logged in users can leave comments