
arduino ile piyano yapımı
Bu projemizde Arduino kullanarak piyano kuracağız. Küçük ama eğlenceli olan bu projede 8 adet buton kullanarak notalar oluşturup kayıt özelliği sayesinde oluşturduğumuz müzikleri dinlememizi ve gerektiğinde tekrar tekrar çalmamızı sağlayacak. Ayrıntılar için BUYRUN DEVAM EDELİM…;)
Gerekli Malzemeler
Arduino Uno
16*2 LCD Ekran
Buzzer
Trimmer 10 dk
SPDT switch
Push Button (8 adet)
Dirençler (10k,560R,1,5k,2,6k,3,9,5,6k,6,8k,8,2k,10k)
Breadboard
Kablo
Muhtelif proje malzemeleri
Arduino Piyano Devre Şeması
Board üzreine kurulacak devrenin fritzing devre şeması aşağıda gösterilmiştir.

arduino ile piyano yapımı
Sadece devre şemasını takip ederek board üzerine yukarıdaki devreyi kurabilirsiniz.

arduino ile piyano yapımı
Direnç değerleri soldan başlayarak sırasıyla, 10k,560R,1,5k,2,6k,3,9,5,6k,6,8k,8,2k ve 10k’dır. DPST anahtarın aynısına sahip değilseniz, yukarıdaki sevre şemasında gösterildiği gibi normal geçiş anahtarı kullanabilirsiniz.

arduino ile piyano yapımı
8 butonu arduino’ya Analog A0 pini üzerinden bağlantısını yaptık. Arayüz için LCD ekranımız var. Analog pin üzerinden devreyi tamamlamak değişken direnç değerlerine sahip potansiyel bir bölücü oluşturuyoruz. Bu şekilde her düğmeye basıldığında Analog pin’e farklı bir analog voltaj verilir. Sadece iki dirençli ve iki basmalı düğmeli bir örnek devre aşağıda gösterilmiştir.

arduino ile piyano yapımı
Butonlara basılmadığı zaman ADC pini +5v değerini alır. Birinci butona basılınca potansiyel bölücü 560R direncinden geçer. İkinci düğmeye basıldığında 1,5k direnç üzerinden geçer.Bu şekilde ADC pimi tarafından alınan voltaj, potansiyel bölenin formüllerine bağlı olarak değişecektir.
Bunun dışında tüm bağlantılar LCD 8,9,10,11 ve 12 pinlerine bağlanır. Sesli uyarı pimi / nolu pine ve SPDT anahtarı 6 nolu pine bağlanır.Projeyi bilgisayar usb portu ile veya DC jack üzerinden 9V veya 12V besleme bağlayabilirsiniz.
Arduino ‘nun Tone () İşlevi
Arduino, sesli uyarı kullanarak farklı sesler üretmek için kullanılabilecek çeşitli frekans sinyalleri üretmek için bir tone () fonksiyonuna sahiptir. Toen () işlevi belirli bir pimde belirli bir frekans üretir. Gerekirse zaman süresi de belirtebilir:
1 2 3 4 5 6 7 8 |
<strong>Syntax</strong> tone(pin, frequency) tone(pin, frequency, duration) <strong>Parameters</strong> pin: the pin on which to generate the tone frequency: the frequency of the tone in hertz – unsigned int duration: the duration of the tone in milliseconds (optional1) – unsigned long |
Pin değerleri dijital pinlerden herhangi biri olabilir. Burada 8 numaralı pini kullandık. Üretilecek frekans Arduino kartınızdaki zamanlayıcının büyüklüğüne bağlıdır. Genel olarak Arduino kartlarda üretilebilecek min frekans 31 Hz’dir ve üretilebilecek max frekans 65535 Hz’dir. Ancak biz insanlar sadece 2000Hz ve 5000 Hz arasındaki frekansları duyabiliriz.
Arduino’da Piyano Tonları Çalmak
Arduino’da belirli bir notanın seslerini çalmak için Brett Hagman tarafından yazılan pitches.h adlı bir kütüphane var. Bu kütüphane hangi frekansın piyanoda hangi notaya eşdeğer olduğu hakkında tüm bilgileri içerir.
Bu projede seçilen notalar sırasıyla, 1 ile 8 arasındaki düğmeler kullanılarak çalınabilen C4,D4,E4,F4,G4,A4,B4 VE C5 notalarıdır.
Arduino Programlama
Arduino programımızda, A0 pininden analog voltajı okumalı, ardından hangi tuşa basıldığını tahmin etmeli ve bu tuş için ilgili tonu çalmalıyız. Bunu yaparken de kullanıcının hangi tuşa bastığını ve ne kadar süre bastığını kaydetmeliyiz, böylece daha sonra kullanıcının çaldığı sesi yeniden oluşturabiliriz.
Mantık bölmüne geçmeden önce çalacağımız 8 notayı tanımlıyoruz. Notalar için ilgili frekans daha sonra pitches.h kütüphanesinden alınıp bir dizi oluşturulur. Burada C4 notasının çalma frekansı 262’dir.
1 |
<strong>int notes[] = {262, 294, 330, 349, 392, 440, 494, 523}; // Set frequency for C4, D4, E4, F4, G4, A4, B4,</strong> |
Şimdi de LCD ekranın hangi pinlere bağlı olduğunu belirtiyoruz.
1 2 |
<strong>const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; //Pins to which LCD is connected</strong> <strong>LiquidCrystal </strong><strong>lcd</strong><strong>(rs, en, d4, d5, d6, d7);</strong> |
Sırada kurulum fonksiyonumuzda hata ayıklama için sadece LCD modülünü ve seri monitörü başlatıyoruz. Bir giriş mesajı gösterip işlerin yolunda gittiğinden emin oluyoruz. Ana döngü içinde iki adet döngü mevcut. İlk döngü SPDT anahtarı kayıt yaptığı sürece döner. Kayıt modunda, kullanıcı çalma ve kaydetme yapabilir.
1 2 3 4 5 6 7 |
<strong> while (digitalRead(6) == 0) //If the toggle switch is set in recording mode { lcd.setCursor(0, 0); lcd.print("Recording.."); lcd.setCursor(0, 1); Detect_button(); Play_tone(); }</strong> |
Detect_button () işlevi kullanıcının hangi düğmeye bastığını, Play_tone () fonksiyonu ise butonun ne kadar süre basıldığını kayıt eder.
Detect_button () işlevi içinde, A0 pininden gelen analog voltajı okuyoruz ve hangi düğmeye basıldığını bulmak için önceden tanımlanmış bazı değerler ile karşılaştırıyoruz. Değer, seri monitör kullanarak belirlenebilir.
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 |
<strong>void Detect_button()</strong> <strong>{</strong> <strong> analogVal = analogRead(A0); //read the analog voltag on pin A0</strong> <strong> pev_button = button; //remember the previous button pressed by the user</strong> <strong> if (analogVal < 550)</strong> <strong> button = 8;</strong> <strong> if (analogVal < 500)</strong> <strong> button = 7;</strong> <strong> if (analogVal < 450)</strong> <strong> button = 6;</strong> <strong> if (analogVal < 400)</strong> <strong> button = 5;</strong> <strong> if (analogVal < 300)</strong> <strong> button = 4;</strong> <strong> if (analogVal < 250)</strong> <strong> button = 3;</strong> <strong> if (analogVal < 150)</strong> <strong> button = 2;</strong> <strong> if (analogVal < 100)</strong> <strong> button = 1;</strong> <strong> if (analogVal > 1000)</strong> <strong> button = 0;</strong> <strong>/****</strong><strong>Rcord</strong><strong> the pressed buttons in </strong><strong>a array</strong><strong>***/</strong> <strong> if (button != pev_button && pev_button != 0)</strong> <strong> {</strong> <strong> recorded_button[button_index] = pev_button;</strong> <strong> button_index++;</strong> <strong> recorded_button[button_index] = 0;</strong> <strong> button_index++;</strong> <strong> }</strong> <strong>/**End of Recording program**/</strong> <strong>} </strong> |
Bu işlev içinde düğmelerin basıldığı diziyi de kaydediyoruz. Kaydedilen değerle, saved_button [] dizininde saklıyoruz. İlk önce yeni bir tuşa basılıp basılmadığını kontrol ederiz, eğer basılırsa, 0 (sıfır) tuşu olup olmadığını kontrol ederiz. Burada 0 (sıfır) butonu hiçbir şey değildir yani butona basılmamıştır. if döngüsünün içinde, button_index değişkeninin verdiği index lokasyonundaki değeri saklarız ve aynı zamanda bu index değerini artırırız, böylece aynı yere yazmayız.
1 2 3 4 5 6 7 8 9 |
<strong> /****</strong><strong>Rcord</strong><strong> the pressed buttons in </strong><strong>a array</strong><strong>***/</strong> <strong> if (button != pev_button && pev_button != 0)</strong> <strong> {</strong> <strong> recorded_button[button_index] = pev_button;</strong> <strong> button_index++;</strong> <strong> recorded_button[button_index] = 0;</strong> <strong> button_index++;</strong> <strong> }</strong> <strong>/**End of Recording program**/</strong> |
Play_tone() fonksiyonu basılı buton için tonu çalacak. İçinde butona basıldığı zamanı kaydedeceğimiz saved_time[] dizisi kullanacağız. Butona ne kadar süre basıldığını belirlemek için millis() işlevini kullanıyoruz. Ayrıca değişkenin boyutunu azaltmak için değeri 10’a böleriz.
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<strong>void Play_tone()</strong> <strong>{</strong> <strong> /****Rcord the time delay between each button press in a array***/ </strong> <strong> if (button != pev_button)</strong> <strong> {</strong> <strong> lcd.clear(); //Then clean it</strong> <strong> note_time = (millis() - start_time) / 10;</strong> <strong> recorded_time[time_index] = note_time;</strong> <strong> time_index++;</strong> <strong> start_time = millis();</strong> <strong> }</strong> <strong> /**End of Recording program**/</strong> <strong> if (button == 0)</strong> <strong> {</strong> <strong> noTone(7);</strong> <strong> lcd.print("0 -> Pause..");</strong> <strong> }</strong> <strong> if (button == 1)</strong> <strong> {</strong> <strong> tone(7, notes[0]);</strong> <strong> lcd.print("1 -> NOTE_C4");</strong> <strong> }</strong> <strong> if (button == 2)</strong> <strong> {</strong> <strong> tone(7, notes[1]);</strong> <strong> lcd.print("2 -> NOTE_D4");</strong> <strong> }</strong> <strong> if (button == 3)</strong> <strong> {</strong> <strong> tone(7, notes[2]);</strong> <strong> lcd.print("3 -> NOTE_E4");</strong> <strong> }</strong> <strong> if (button == 4)</strong> <strong> {</strong> <strong> tone(7, notes[3]);</strong> <strong> lcd.print("4 -> NOTE_F4");</strong> <strong> }</strong> <strong> if (button == 5)</strong> <strong> {</strong> <strong> tone(7, notes[4]);</strong> <strong> lcd.print("5 -> NOTE_G4");</strong> <strong> }</strong> <strong> if (button == 6)</strong> <strong> {</strong> <strong> tone(7, notes[5]);</strong> <strong> lcd.print("6 -> NOTE_A4");</strong> <strong> }</strong> <strong> if (button == 7)</strong> <strong> {</strong> <strong> tone(7, notes[6]);</strong> <strong> lcd.print("7 -> NOTE_B4");</strong> <strong> }</strong> <strong> if (button == 8)</strong> <strong> {</strong> <strong> tone(7, notes[7]);</strong> <strong> lcd.print("8 -> NOTE_C5");</strong> <strong> }</strong> <strong>}</strong> |
Son olarak kullanıcı, kayıttan sonra sesi çalmak için SPDT’yi diğer yöne çevirmelidir. Bu yapıldığında program önceki while döngüsünden ayrılır ve daha önce kaydedilmiş olan bir süre boyunca basılan butonların sırasındaki notaları oynattığımız ikinci döngüsüne girer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<strong>while (digitalRead(6) == 1) //If the toggle switch is set in Playing mode</strong> <strong> {</strong> <strong> lcd.clear();</strong> <strong> lcd.setCursor(0, 0); lcd.print("Now Playing..");</strong> <strong> for (int i = 0; i < sizeof(recorded_button) / 2; i++)</strong> <strong> {</strong> <strong> delay((recorded_time[i]) * 10); //Wait for before paying next tune</strong> <strong> if (recorded_button[i] == 0)</strong> <strong> noTone(7); //user dint touch any button</strong> <strong> else</strong> <strong> tone(7, notes[(recorded_button[i] - 1)]); //play the sound corresponding to the button touched by the user</strong> <strong> }</strong> <strong> }</strong> <strong>}</strong> |
Oynat,Kaydet ve Tekrarla
Devre şemasına göre montaj işlemini yapıp kodu yüklüyoruz. SPDT’yi kayıt moduna getirip seçtiğimiz ses tonlarını çalıyoruz. Her butona basıldığında farklı bir ton çalacaktır. Bu modda LCD ekranda “Recording…” görüntülenir ve ikinci satırda aşağıda gösterildiği gibi şu anda basılı olan notanın adını görürsünüz.

arduino ile piyano yapımı
Nota çalma işlemi yaptıktan sonra SPDT butonunu diğer tarafa çeviridğimizde LCD ekranda “Now Playing…” görüntülenir ve daha sonra çaldığınız sesi çalmaya başlar.

arduino ile piyano yapımı
Projenin tam çalışması aşağıda verilen videoda bulunabilir. Umarım projeyi anlar ve yapmaktan zevk alırsınız.
Arduino ile Piyano Proje Yapım Kodu
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
/* Arduino based Piano and Record and play option Code by: B. Aswinth Raj Website: www.circuitdigest.com Dated: 22-05-2017 */ #include <LiquidCrystal.h> int notes[] = {262, 294, 330, 349, 392, 440, 494, 523}; // Set frequency for C4, D4, E4, F4, G4, A4, B4, C5 const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; //Pins to which LCD is connected LiquidCrystal lcd(rs, en, d4, d5, d6, d7); char button = 0; int analogVal; char REC = 0; int recorded_button[200]; int pev_button; int recorded_time [200]; char time_index; char button_index = 0; unsigned long start_time; int note_time; void setup() { Serial.begin(9600); pinMode (6, INPUT); lcd.begin(16, 2); //We are using a 16*2 LCD display lcd.print("Arduino Piano"); //Display a intro message lcd.setCursor(0, 1); // set the cursor to column 0, line 1 lcd.print("-CircuitDigest"); //Display a intro message delay(2000); //Wait for display to show info lcd.clear(); //Then clean it } void loop() { while (digitalRead(6) == 0) //If the toggle switch is set in recording mode { lcd.setCursor(0, 0); lcd.print("Recording.."); lcd.setCursor(0, 1); Detect_button(); Play_tone(); } while (digitalRead(6) == 1) //If the toggle switch is set in Playing mode { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Now Playing.."); for (int i = 0; i < sizeof(recorded_button) / 2; i++) { delay((recorded_time[i]) * 10); //Wait for before paying next tune if (recorded_button[i] == 0) noTone(7); //user dint touch any button else tone(7, notes[(recorded_button[i] - 1)]); //play the sound corresponding to the button touched by the user } } } void Detect_button() { analogVal = analogRead(A0); //read the analog voltag on pin A0 pev_button = button; //remember the previous button pressed by the user if (analogVal < 550) button = 8; if (analogVal < 500) button = 7; if (analogVal < 450) button = 6; if (analogVal < 400) button = 5; if (analogVal < 300) button = 4; if (analogVal < 250) button = 3; if (analogVal < 150) button = 2; if (analogVal < 100) button = 1; if (analogVal > 1000) button = 0; /****Rcord the pressed buttons in a array***/ if (button != pev_button && pev_button != 0) { recorded_button[button_index] = pev_button; button_index++; recorded_button[button_index] = 0; button_index++; } /**End of Recording program**/ } void Play_tone() { /****Rcord the time delay between each button press in a array***/ if (button != pev_button) { lcd.clear(); //Then clean it note_time = (millis() - start_time) / 10; recorded_time[time_index] = note_time; time_index++; start_time = millis(); } /**End of Recording program**/ if (button == 0) { noTone(7); lcd.print("0 -> Pause.."); } if (button == 1) { tone(7, notes[0]); lcd.print("1 -> NOTE_C4"); } if (button == 2) { tone(7, notes[1]); lcd.print("2 -> NOTE_D4"); } if (button == 3) { tone(7, notes[2]); lcd.print("3 -> NOTE_E4"); } if (button == 4) { tone(7, notes[3]); lcd.print("4 -> NOTE_F4"); } if (button == 5) { tone(7, notes[4]); lcd.print("5 -> NOTE_G4"); } if (button == 6) { tone(7, notes[5]); lcd.print("6 -> NOTE_A4"); } if (button == 7) { tone(7, notes[6]); lcd.print("7 -> NOTE_B4"); } if (button == 8) { tone(7, notes[7]); lcd.print("8 -> NOTE_C5"); } } |
VİDEO