Arduino Xylophone

159,656

447

68

Introduction: Arduino Xylophone

About: I used to work for instructables.com, now I just make stuff. // follow me to see what I'm up to: https://www.echoechostudio.com



I made a xylophone that uses an Arduino Mega to detect when a note is struck, and generate MIDI output. This project is wondeful because I essentially made a xylophone, a drumkit, and any other MIDI controlled sound instrument, with one tool. The following steps  will outline what I used to make this xylophone.

Step 1: You Will Need...

To construct the xylophone I used the following parts from Radioshack:

(x1) Arduino Mega 2560 (Radioshack #276-127)
(x12) Piezo Element (Radioshack #273-073)
(x12) 1M Ohm resistors (Radioshack #271-1356)
(x1) Single Core wire (Radioshack #278-1221)
(x1) Printed Circuit Board (Radioshack #276-170)
(x1) USB 2.0 Cord (Radioshack #26-714)
(misc) Electrical Tape (Radioshack #64-2373)
(misc) Heat shrink (Radioshack #55048444)

The housing for the xylophone was easy to build. I used:
1/4" plywood
1/8" acrylic sheeting
wood glue
1/4" vinyl tubing
1 1/2" long machine screws.
2" masking tape

Tools:
laser cutter
scissors
cotton swabs
small flat head screw driver

Step 2: Free the Piezos, Then Solder Longer Leads.

For this project, I used piezo elements to detect when each note is struck on the xylophone. These piezos detect vibration, or a knock. Often the elements come in a housing, to protect the disc from being bent or smashed - but for this project I needed to remove them from their plastic.

By gently pressing around the edges with my fingers, you could hear the glue crack apart from the plastic, I loosened the bottom of the casing. Carefully, I insterted a precision flat-head screw driver, and popped the bottom of the case off.

The piezo element could then be removed from the outside of the housing.

Because I am using an Arduino Mega Board, I could have up to 16 Analog inputs, or 16 Piezos. I decided to just include an octave & a half, 12 notes, so I used 12 piezos.

After they were free from their case, I soldered longer wires to each piezo element, to prepare them to be inserted into the xylophone. When I was done soldering longer leads on to each piezo, I wrapped my solder points with heat shrink or electrical tape.

Step 3: The Bars and Housing.

I used CorelDraw to draft vector files that would guide the laser cutter for the housing and bars of the xylophone.

The acrylic bars were each 10x2 inches. Each bar has two holes in them that will guide a machine screw through the bar, and mount to the top panel of the wooden housing.

The wooden housing I designed is 10.5x30x3 inches. It forms a shallow box that supports the electronics embedded within it. I used woodglue and a cotton swab to secure all of the corners, and allowed 24 hours to cure before I sanded down all of the edges.

The CorelDraw file for the base housing is attached to this step.

Step 4: Attach the Piezos to the Bars

I threaded the Piezo wires through the middle holes in the top panel of the housing. Then, I centered the piezo element on each acrylic bar, and used 2 inch blue masking tape to adhere the piezo to the bar.

Step 5: Attach the Bars to the Top Panel.

I used 1 1/2" machine screws and nuts to secure the bars to the wooden paneling.

To prevent excessive shake or vibration on each bar, I decided to use vinyl tubing as a shock absorber on each machine screw. With 12 bars, I used 24 machine screws and nuts, and 24 3/4" lengths of vinyl tubing. Thread the machine screw through the bar, then the vinyl, and slip it into the paneling. When the screw was through the panel, I was able to twist on the nut to fully secure it to the panel.

All of these should only be finger-tight, to avoid stress on the paneling, or on the bar.

Step 6: Build the Circuit.

Before connecting the piezos to the Arduino, I connected a 1-megohm resistor in parallel to the Piezo element to limit the voltage and current produced by the piezo, and to protect the analog input ports on the Arduino. On the PCB, I marked with a permanent marker which piezo goes to each analog input port on the Arduino. I also made the same markings on the back of the top wooden panel.

After I soldered the resistors into place, I ran a small jumper wire from one end of the resistor, to the longest rail on the PCB, and designated it my ground rail. Next, I soldered all of the piezos' ground wires into place, in line wtih the same end of each grounded resistor. The positive lead from the piezos is soldered in to the same rail as the other end of each resistor. 

I cut 12 lengths of green wire to be my "signal wire" to the Arduino. Each signal wire is soldered into the same positive resisted rail of the piezo.



The fritzing diagram file is attached to this step.

Step 7: Connecting to the Arduino

I took all of the signal wires, and the ground wire running from the PCB and marked each one with blue masking tape, writing which port each wire was designated to. I then fed all of the marked single core wire into the corresponding ports of the Arduino.

All remaining Analog ports must be grounded! Otherwise it will affect your serial output from the arduino. I used 4 black wires, running from the ground rail of the PCB directly to the open analog ports of the Mega board. (A12, A13, A14, and A15)

When you have completed your circuit, you can load the xylophone program, or sketch, onto the Arduino board from the Arduino Developing Environment.

Here is the sketch I used.

Step 8: Power and Communication.

The Arduino can be powered via USB, which doubles as a communication port. I ran a USB cable through the housing of the xylophone. By drilling a small hole, that was big enough for the ends of the cable, I could hide most of the USB cord in the housing.

I drilled a second hole to hold my mallots.

Step 9: Serial to Midi

To get the xylophone coupled with my comptuer I used software called Hairless. It converts the serial out signal from the Arduino into a MIDI signal that programs like GarageBand, Logic, and Ableton can read and record. Be sure that the bridge is running before you try and import MIDI data from the xylophone. 

Note: Disable the bridge while trying to update the sketch on the Arduino board. You cannot have the bridge running while trying to communicate to the board from the Arduino software.

Step 10: Jam It!

After you are up and running, you can play the xylophone like drums. Record your beat track. Then kick change the MIDI instrument to a bass synth, and record a rhythm track. Finally turn it back into a xylophone and create a melody track for the best song ever. This thing is a lot of fun!

Hurricane Lasers Contest

Participated in the
Hurricane Lasers Contest

3 People Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

68 Discussions

0
sassabyfilm
sassabyfilm

Question 6 months ago on Step 1

I know this is a very old project now, but I'm interested in building a similar instrument - with more keys, I know the Arduino mega has 16 inputs, and people have suggested using a multiplexer to get more inputs, just wondering if anyone has successfully made a marimba with a larger number of keys, that's velocity sensitive - that's quite important for my project.

The ultimate thing also would be if I could attach a sustain pedal as well and an octave up/down button, but that's a real luxury!

1
carter714
carter714

6 years ago on Introduction

My son and I made this project together. We had a hard time getting the proper response from the note keys. When we watched the keyboard view in Garageband we noticed a few different notes were sometimes playing when we struck only one key. We isolated the problem to vibration. We replaced the vinyl tubing with rubber and felt pads with a through hole drilled into them. This isolated only the note we wanted to play.

We had to change the code a bit to get all 12 keys to play. I do not know why the changes worked since this was our first project, but we studied other code and experimented until everything worked as expected.

This is a great project, fun, interesting, challenging, and we ended up with this really cool device that can be easily switched to produce a wide range of instrument sounds as the original author explained.

The few lines of code we modified were as follows:

We changed the pinAssignments to [12]. In fact we switched all arrays to [12].

We changed the char pinAssignments to an int function.

We removed all single quotes from the pinAssignments [12] ={A0,A1, ...n}

Changed PadCutOff values to 40

Changed MaxPlaytime values to 70

Changed byte status1; to byte status;

Set hitavg = 127;

We used a note off value of 128 instead of the note on with velocity 0 in the MIDI_TX(128,PadNote[pin], 0)

And in the Transmit MIDI section we changed the status1 statement to status.

Again, I do not know if these were required to make our project work or why it made it work, but it did and we wanted to share and tell everyone that this is a great project to build.

photo.JPG
0
sassabyfilm
sassabyfilm

Reply 6 months ago

Amazing! So did yours end up being velocity sensitive?

1
audreyobscura
audreyobscura

Reply 6 years ago on Introduction

So AWESOME! So glad it worked <3 it's pretty fun. I am in the process of overhauling this project based on the code suggestions made on this comment thread. I really appreciate such an awesome community, its wonderful that this little project has become so collaborative :D

0
Leond31
Leond31

3 years ago

OK, so I have adapted the code to accommodate 36 keys. There were a couple of errors in the code, but they served to educate me on the commands. I have bought the Arduino Due, and a multiplexer. The Due has 12 analog inputs. Using the Arduino IDE I was able to verify the code for my board. It works and uploads flawlessly.

So far, I have correctly modified for the number of notes and range (F2 to E5). I am building a three-octave rig. I think I need one more (F5), but this will be no problem.

Here is he first section of the code. I have questions.



I have not yet done the physical build. However, it is my understanding that there is a bit of velocity sensitivity with the piezos. This is why resistors are needed across their terminals. They are so sensitive that they would otherwise produce very high values when struck. Doug Beney at diy.midcontroller.com discusses this in just enough more detail to not put a neophyte like me to sleep.

0
sassabyfilm
sassabyfilm

Reply 6 months ago

Did you ever build this? I'm interested in doing the same thing with 19-24 keys :D

0
David_Coup
David_Coup

Reply 3 years ago

Hi Leond31!
I'm really interested in building the same thing like you. I just want 32 keys, but it should work like yours I think.
I would really appreciate it, if you would contact me to exchange some of your experiences.
Sorry for my bad english, greetings from Germany!

David

FB: https://www.facebook.com/david.renting.3
E-Mail: d.renting@outlook.de

0
Sax6M
Sax6M

Reply 8 months ago

Just to show you how much I lost momentum, my account was apparently dumped for non-usage, so I had to set up a new account, which was no problem. I am getting the project back up and running again.

What about you? How is your project?

0
GaboM1
GaboM1

Reply 8 months ago

Hey can I have the 37 keys code bro?

2
RAFALAMAO
RAFALAMAO

2 years ago

Thank you very much!!!! :D
This works perfect with my mini drum set, no delay, no unwanted repeat notes, everything cool!!!!

I modified the code for only five piezo and I turned off velocity and it worked beautiful!!!

This is the code:

//Xylophone
//Adapted for an ArduinoMega
//from Jenna deBoisblanc and Spiekenzie Labs initial code
//*******************************************************************************************************************
// User settable variables
//*******************************************************************************************************************
int pinRead;
char pinAssignments[5] ={'A0','A1','A2','A3','A4'};
byte PadNote[5] = {53,69,11,57,36}; // MIDI notes from 0 to 127 (Mid C = 60)
//53 Campana
//69 Tarola
//11 Hithat cerrado
//57 Crash
//36 Bombo
int PadCutOff[5] = {500,200,350,300,400}; // Minimum Analog value to cause a drum hit
int MaxPlayTime[5] = {90,90,90,90,90}; // Cycles before a 2nd hit is allowed
#define midichannel 1; // MIDI channel from 0 to 15 (+1 in "real world")
boolean VelocityFlag = true; // Velocity ON (true) or OFF (false)
//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************
boolean activePad[5] = {0,0,0,0,0}; // Array of flags of pad currently playing
int PinPlayTime[5] = {0,0,0,0,0}; // Counter since pad started to play
byte status1;
int pin = 0;
int hitavg = 0;
//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************
void setup()
{
Serial.begin(57600); // SET HAIRLESS TO THE SAME BAUD RATE IN THE SETTING
}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
void loop()
{
for(int pin=0; pin < 5; pin++) //
{
//int pin = 3;
// for (pinRead=0; pinRead < 16, pin++){
hitavg = analogRead(pinAssignments[pin]);
//Serial.println(hitavg);
// read the input pin
if((hitavg > PadCutOff[pin]))
{
if((activePad[pin] == false))
{
if(VelocityFlag == true)
{
hitavg=127;
//hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin])); // With full range (Too sensitive ?)
//hitavg = (hitavg / 8) -1 ; //8 // Upper range
}
else
{
hitavg = 127;
}
MIDI_TX(144,PadNote[pin],hitavg); //note on
PinPlayTime[pin] = 0;
activePad[pin] = true;
}
else
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;
}
}
else if((activePad[pin] == true))
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;
if(PinPlayTime[pin] > MaxPlayTime[pin])
{
activePad[pin] = false;
MIDI_TX(144,PadNote[pin],0);
}
}
}
}
//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(byte MESSAGE, byte PITCH, byte VELOCITY)
{
status1 = MESSAGE + midichannel;
Serial.write(status1);
Serial.write(PITCH);
Serial.write(VELOCITY);
}

Ohhh yea, I used FL Studio´s EZdrumer´s plugin that simluate drumset.

Regards :D!!

Sin t&iacute;tulo.png
0
mariamvelquis826
mariamvelquis826

Reply 11 months ago

Hi Rafalamao I'm using your code and it works perfect but I don't get any sensibility in any piezo.. how can I add sensibility to the piezos for your code?

0
RAFALAMAO
RAFALAMAO

Reply 7 months ago

What do you mean with "Sensibility"?

0
diyfreak2001
diyfreak2001

Question 2 years ago

Hi, I tried to make a version of this using an arduino mega and 8 piezos. Hairlessmidi and FL studio 12. I tried replacing my arduino as well as the piezos and every component I used to make sure the hardware is fine but only the first six analog pins(A0-A5) are responding to signals from the piezos.

Here's the code I used :

//Xylophone

//Adapted for an ArduinoMega

//from Jenna deBoisblanc and Spiekenzie Labs initial code

//*******************************************************************************************************************

// User settable variables

//*******************************************************************************************************************

int pinRead;

char pinAssignments[16] ={

'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','A10','A11','A12','A13','A14','A15'};

byte PadNote[16] = {

57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72}; // MIDI notes from 0 to 127 (Mid C = 60)

int PadCutOff[16] =

{

400,400,200,800,400,400,400,400,400,400,400,400,400,400,400,400}; // Minimum Analog value to cause a drum hit

int MaxPlayTime[16] = {

90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90}; // Cycles before a 2nd hit is allowed

#define midichannel 1; // MIDI channel from 0 to 15 (+1 in "real world")

boolean VelocityFlag = true; // Velocity ON (true) or OFF (false)

//*******************************************************************************************************************

// Internal Use Variables

//*******************************************************************************************************************

boolean activePad[16] = {

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Array of flags of pad currently playing

int PinPlayTime[16] = {

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Counter since pad started to play

byte status1;

int pin = 0;

int hitavg = 0;

//*******************************************************************************************************************

// Setup

//*******************************************************************************************************************

void setup()

{

Serial.begin(57600); // connect to the serial port 115200

}

//*******************************************************************************************************************

// Main Program

//*******************************************************************************************************************

void loop()

{

for(int pin=0; pin < 16; pin++) //

{

//int pin = 3;

// for (pinRead=0; pinRead < 16, pin++){

hitavg = analogRead(pinAssignments[pin]);

//Serial.println(hitavg);

// read the input pin

if((hitavg > PadCutOff[pin]))

{

if((activePad[pin] == false))

{

if(VelocityFlag == true)

{

// hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin])); // With full range (Too sensitive ?)

hitavg = (hitavg / 8) -1 ; // Upper range

}

else

{

hitavg = 127;

}

MIDI_TX(144,PadNote[pin],hitavg); //note on

PinPlayTime[pin] = 0;

activePad[pin] = true;

}

else

{

PinPlayTime[pin] = PinPlayTime[pin] + 1;

}

}

else if((activePad[pin] == true))

{

PinPlayTime[pin] = PinPlayTime[pin] + 1;

if(PinPlayTime[pin] > MaxPlayTime[pin])

{

activePad[pin] = false;

MIDI_TX(144,PadNote[pin],0);

}

}

}

}

//*******************************************************************************************************************

// Transmit MIDI Message

//*******************************************************************************************************************

void MIDI_TX(byte MESSAGE, byte PITCH, byte VELOCITY)

{

status1 = MESSAGE + midichannel;

Serial.write(status1);

Serial.write(PITCH);

Serial.write(VELOCITY);

}

I have also attached the circuit schematic if that may be needed.

Can anyone please help with this?

Thanks

circuit diagram.jpg
0
RAFALAMAO
RAFALAMAO

Answer 2 years ago

Try with this code, dude:

AND REMEMBER CHANGE BAUD RATE TO 57600 IN HAIRLESS/FILE/PREFERENCES

//Xylophone

//Adapted for an ArduinoMega

//from Jenna deBoisblanc and Spiekenzie Labs initial code

//*******************************************************************************************************************

// User settable variables

//*******************************************************************************************************************

int pinRead;

char pinAssignments[12] ={'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','A10','A11'};

byte PadNote[12] = {57,58,59,60,61,62,63,64,65,66,67,68}; // MIDI notes from 0 to 127 (Mid C = 60)

int PadCutOff[12] ={400,400,200,800,400,400,400,400,400,400,400,400}; // Minimum Analog value to cause a drum hit

int MaxPlayTime[12] = {90,90,90,90,90,90,90,90,90,90,90,90}; // Cycles before a 2nd hit is allowed

#define midichannel 1; // MIDI channel from 0 to 15 (+1 in "real world")

boolean VelocityFlag = true; // Velocity ON (true) or OFF (false)

//*******************************************************************************************************************

// Internal Use Variables

//*******************************************************************************************************************

boolean activePad[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; // Array of flags of pad currently playing

int PinPlayTime[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; // Counter since pad started to play

byte status1;

int pin = 0;

int hitavg = 0;

//*******************************************************************************************************************

// Setup

//*******************************************************************************************************************

void setup()

{

Serial.begin(57600); // connect to the serial port 115200

}

//*******************************************************************************************************************

// Main Program

//*******************************************************************************************************************

void loop()

{

for(int pin=0; pin < 12; pin++) //

{

//int pin = 3;

// for (pinRead=0; pinRead < 16, pin++){

hitavg = analogRead(pinAssignments[pin]);

//Serial.println(hitavg);

// read the input pin

if((hitavg > PadCutOff[pin]))

{

if((activePad[pin] == false))

{

if(VelocityFlag == true)

{

// hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin])); // With full range (Too sensitive ?)

hitavg = (hitavg / 8) -1 ; // Upper range

}

else

{

hitavg = 127;

}

MIDI_TX(144,PadNote[pin],hitavg); //note on

PinPlayTime[pin] = 0;

activePad[pin] = true;

}

else

{

PinPlayTime[pin] = PinPlayTime[pin] + 1;

}

}

else if((activePad[pin] == true))

{

PinPlayTime[pin] = PinPlayTime[pin] + 1;

if(PinPlayTime[pin] > MaxPlayTime[pin])

{

activePad[pin] = false;

MIDI_TX(144,PadNote[pin],0);

}

}

}

}

//*******************************************************************************************************************

// Transmit MIDI Message

//*******************************************************************************************************************

void MIDI_TX(byte MESSAGE, byte PITCH, byte VELOCITY)

{

status1 = MESSAGE + midichannel;

Serial.write(status1);

Serial.write(PITCH);

Serial.write(VELOCITY);

}

0
ElijahD16
ElijahD16

2 years ago on Introduction

Code works but 5 of the tiles are not working. Help!!!!!

0
joeyjerry
joeyjerry

6 years ago on Introduction

are these pads velocity sensitive?

And do we have to make any changes when we are making a drumkit in this way?

0
Jfieldcap
Jfieldcap

Reply 4 years ago

I'm a bit late to your comment, but here's an answer: No, they would not be velocity sensitive. Piezos are sensitive to a sharp taps, and then they'll produce a spike of electricity. It's possible you could get some minor velocity sensitivity, but I wouldn't think it would be very reliable. As for the drumkit, I wouldn't think so, but because I haven't done it myself, here's another instructable showing just that:

https://www.instructables.com/id/MIDI-Arduino-Drums/

1
RickG5
RickG5

Reply 3 years ago

I'm not so sure; I've got a system set up here with piezo sensors connected to an arduino uno and I'm getting variable input between 0-150 based and pretty responsive to how hard I tap. I see no reason it can't be velocity sensitive.

0
Jfieldcap
Jfieldcap

Reply 3 years ago

Well then! Good to hear! I (Sit) corrected! ;D

0
klc6
klc6

4 years ago

is this circuit possible with direct midi din out ?