Hello guys, in this tutorial we will see how to make Arduino piano, it may not sound like original song, because of two reason first I am not a piano artist and second is I am playing song on this hardware which is little difficult. You can watch the video just down below of this tutorial. I hope you will like my effort. So let’s get started.

For this you will need

  1. Arduino,
  2. Jumper Wires,
  3. 1K Ohms resistor,
  4. One copper wire,
  5. Cart board,
  6. Breadboard,(Optional).

Do connection as shown in diagram.

How to make Arduino piano.

We are taking inputs from jumper wire to Arduino and send it to max software, which plays particular note. You need to download max software.  You will also need a max patch file. You can download

Piano MAX Patch File.zip

Sketch:
// drawMIDI
int myKey[12];
int myOldKey[12];
int myNote[] = {79, 77, 76, 72, 74, 71, 69, 67, 65, 64, 62, 60};
int keyPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// Pins for MIDI keys
// This is the value required to consider the key 'touched' and trigger a MIDI signal
int touchedCutoff = 36;
// This is the value required for the first part of goMIDI
int midiInfo = 144;
// This is the section that sets up serial communications

void setup(){
 Serial.begin(19200); //if using hardward MIDI or HIDUINO, change to 31250 
 //19200 baud, requires serial to MIDI conversion on your computer
}

// This is the section that reads the capacitance and determines what to do, noteOn or noteOff
void loop(){
// only send MIDI messages when something changes
for (int i; i<12; i++) { 
 myKey[i]=readCapacitivePin(keyPin[i]);
 if (myKey[i] > touchedCutoff){ 
 myKey[i]=127;
 }
 else{
 myKey[i]=0;
 }
 if (myKey[i] != myOldKey[i]) {
 myOldKey[i] = myKey[i];
 goMidi(midiInfo, myNote[i], myKey[i]);
  }
 }
}

// The readCapacitivePin function is used to read whether the graphite strip connected to each pin is actually being pressed or not
// this part of the code is based on an open example on the Arduino playground for 'Capacitive Sensing'
// readCapacitivePin
// Input: Arduino pin number
// Output: A number, from 0 to 17 expressing
// how much capacitance is on the pin
// When you touch the pin, or whatever you have
// attached to it, the number will get higher

uint8_t readCapacitivePin(int pinToMeasure){
 volatile uint8_t* port;
 volatile uint8_t* ddr;
 volatile uint8_t* pin;
 
 byte bitmask;
 if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
 port = &PORTD;
 ddr = &DDRD;
 bitmask = 1 << pinToMeasure;
 pin = &PIND;
 }
 
 if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
 port = &PORTB;
 ddr = &DDRB;
 bitmask = 1 << (pinToMeasure - 8);
 pin = &PINB;
 }
 
 if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
 port = &PORTC;
 ddr = &DDRC;
 bitmask = 1 << (pinToMeasure - 13);
 pin = &PINC;
 }
 
 // Discharge the pin first by setting it low and output
 *port &= ~(bitmask);
 *ddr |= bitmask;
 delay(1);
 
 *ddr &= ~(bitmask);

int cycles = 16000;
 for(int i = 0; i < cycles; i++){
 if (*pin & bitmask){
 cycles = i;
 break;
 }
 }
 *port &= ~(bitmask);
 *ddr |= bitmask;
 return cycles;
}

// This is the section that translates the touches into MIDI signals
void goMidi(int cmd, int data1, int data2) {
 Serial.write(cmd); // set value stays the same
 Serial.write(data1); // note number, 60 or 49 or whatever
 Serial.write(data2); // velocity, keep at 127 for ON or 0 for OFF
 delay(2);
}

Explanation:

This code we will upload to Arduino. You can change note from here.

int myNote[] = {79, 77, 76, 72, 74, 71, 69, 67, 65, 64, 62, 60};

if you are getting constantly false signal from pin number 13, for example Tx LED constantly blinking then delete pin number 13 and note.

int myNote[] = {79, 77, 76, 72, 74, 71, 69, 67, 65, 64, 62};
int keyPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

upload the code, after uploading close Arduino IDE software. Download this file Piano MAX Patch File.zip and open text file in notepad, copy this code and paste in new patcher file.

 

New Patcher

Add New Patcher

After Pasting we get this screen like this

After Pasting we will get this screen like this

Check max console, Arduino port is showing or not. If it is not showing remove USB cable and close max software and again restart.

cheking port on MAX

After opening patch file you have to turn ON serial output. And make sure it is same port where Arduino is connected. When we press jumper wire it touches 5v line and Arduino get high signal, and it goes to max software which play particular note.

You can see when I press jumper wire it plays note.

Till then keep learning keep making.

Categories: Arduino