# Sounds with arduino This is a note about how to make sounds and music with an arduino step by step. It's also relying in the amazing work of [Robson Couto](https://github.com/robsoncouto) ![image](https://hackmd.io/_uploads/H1i6LWk7-x.png) _Thanks, man_ :::info For people in the IB doing the new syllabus starting M27 this links to A1.2 (representing audio) and also B2 (arrays and lists) ::: Now, let's get the party started! First the connections! We're going to connect a piezo (a buzzer) with a small potenciometer (to adjust the volume, we don't want to make it too loud) following the next scheme. ![image](https://hackmd.io/_uploads/HJPs_Zy7bx.png) For this we're going to use the function `tone()` that is already integrated in arduino. You can see more information about it [here](https://docs.arduino.cc/language-reference/en/functions/advanced-io/tone/) The function tone is going to ask for 3 things (three numbers) * First the pin that you want to make the tone * Then the frequency that you want to set. Lower pitch will have lower numbers, higher pitch will need higher numbers * Last how long do you want to make this tone function (in milliseconds. ) ```cpp= int duration = 1000; int speakerPin = 8; void setup() {} void loop() { tone(speakerPin, 440, duration); //A4 "LA" delay(duration); noTone(speakerPin); delay(duration); tone(speakerPin, 880, duration); //A5 "la" delay(duration); noTone(speakerPin); delay(duration); } ``` :::info By now you should already guess what this code going to do! ::: Of course when we want to do a score for people who have done some music you will see something like this: ![image](https://hackmd.io/_uploads/ry2sq-kQWx.png) _Spanish Christmas song that I had to play with flute when I was a kid. [Source](https://tomapartituras.wordpress.com/2010/08/10/partitura-campana-sobre-campana-flauta/)_ This may (and it is) a bit convoluted but it basically says what is the **pitch** of each note and the **duration** of each note. So if we want to make a melody we only need to find a score and find the durations and the frequencies. Then we need to play them in sequence. The projects of Robson Couto will do something similar but a bit more convoluted than this. Let me paste here a simplified version for it. ```cpp= #define NOTE_D5 587 int buzzer = 8; int melody[] = { 659, 180, NOTE_D5, 180, 0, 320 }; int notes = 3; //sizeof(melody) / sizeof(melody[0]) / 2; //C++ doesn't have a "len()" function, we need to do it in harder way //but think of notes as len(melody) in python //melody[0] -> 659 (frequency of 1st note) //melody[1] -> 180 (duration of 1st note) //melody[2] -> 587 (frequency of 2nd note, written in the first line with #DEFINE) //melody[3] -> 180 (duration of 2nd note) //melody[4] -> 0 (frequency of 3rd note, a rest) //melody[5] -> 320 (duration of 3rd note) void setup() { for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) { tone(buzzer, melody[thisNote], melody[thisNote+1]*0.9 ); delay(melody[thisNote+1] ); noTone(buzzer); } } void loop() { } ``` Let's analyze it a bit. // TO-DO (done in class step by step) ```CPP= int buzzer = 4; int duration = 100; void setup() { Serial.begin(19200); } void loop() { for(int x = 40; x < 500; x = x +20){ //Serial.println("Frequency: " + x); tone(buzzer, x, duration); delay(duration); noTone(buzzer); } } ``` ## Other implementations for scores Another option is to just write tones and delays all the score down. If you want to see an example you can see it here https://github.com/AnonymousAlly/Arduino-Music-Codes/blob/master/Megalovania.ino