Need help with a MIDI project

I have already posted in the programming questions and got 0 replies after two days, so I’ll try my luck here, please do not delete for cross-posting.

Hi! So I want to make an electric 37-key Vibraphone, which looks, if you google it, very similar to a xylophone with a pedal. I want to use 37 piezo sensors to analog, 2 16-channel multiplexers, the Arduino Mega/Uno, and a button as the pedal.
I want to connect 32 piezos through the MUXs and the last five to the analog pins and make them react to how hard you hit the key, harder means louder, when you press the button it needs to stop any existing sound and if you hit a key while you are pressing the button it needs to make a shorter sound (basically the opposite of the piano pedal) and all that while the sounds are coming from an SD card and not a computer, and feeding it onto headphones.
Pretty complicated, and I’m struggling really hard with the code. Especially with the multiplexer piezos sensitivity part. I’ve already started writing the code, but I won’t have access to my computer for a while so thanks beforehand for any help, I’ll really appreciate it.

P.S - if you have any ideas on better ways to build and code my project I’d love to hear them!


Daniela_choresh: all that while the sounds are coming from an SD card and not a computer, and feeding it onto headphones.

So there's no MIDI involved then?

The Arduino is a microcontroller, it doesn't play audio. You'll have to use an external sampler if you want good quality audio.

Reading piezos is tricky. Their impedance is too high for the Arduino's ADC, so you're likely to experience crosstalk between channels, and they produce an AC signal, so you need some way to get the envelope/peak value, rather than the actual real-time voltage across the terminals.


@Daniela_choresh, please do not cross-post. Threads merged.

Yes the most difficult part of this is the hardware which is probably why you didn't get a response from the programming section. Although I would have thought the Audio section was a better match.

I would suggest that you need an oscilloscope to do this project so you can look at the wave forms you are getting from those sensors. If you have one sensor for each of your 37 keys then you need some form of analogue multiplexer and it is going to be a lot of wiring.

If there is going to be MIDI involved, what does that bring to the party?

You might need some electronics for each piezo. First of all, hitting a piezo with a mallet will most sure create tens of volts. Make sure the signal won't toast your Arduino. Second, the signal is very short. If your software polls 37 signal lines somehow (I'm no expert on multiplexers), you might need electronics to sustain the signal at its peak level until you have time to read it.

And if you really want to playback sampled sounds, you should probably use a Raspberry Pi. While an Arduino is perfect for MIDI. I have some projects going on with Arduino and MIDI. And an old Roland synth, which does the playback. Works great.

I think I explained it wrong, the piezo will be under the keys, and an insolation foam will stop the vibrations from crossing to nearby keys. About the oscilloscope, isn't there one in the Arduino program? Also I'll put a resistor to make the piezo less sensitive. I guess there's really no MIDI, I just want to play sampled sounds, but from an SD card so a computer won't be a must everytime I want to play, it isn't a big deal but still. What electronics do I need for the piezos? Didn't understand.. I really am a starter, but I'll try my best, I saw this video about electronic drums and he shared the code, he made a threshold and read the peak of the piezos. And I didn't found any way to feed the audio through to headphones without a computer, I thought about MIDI because I know you can connect it to a computer, record your playing, feed different sampled sounds and hear with headphones but honestly I have no idea how to involve it. I'm lost. I just want to learn more Arduino and do it through this project.


This is a typical waveform for the voltage across a piezo when it is hit.
As you can see, it is an AC-signal (half of it is negative, and it is centered around 0 V). It oscillates very quickly. The frequency and how fast it decays depends on the surface it’s mounted to, and the properties of the piezo in question, however, the point is that you can’t just sample it at a single point, and then use that as the amplitude value. If you randomly sample at a point in time, you may measure 0 V, because you are at a point where the voltage across the piezo is negative or zero, you might think that it has not been hit. However, mere microseconds earlier, it might have been at a very high peak that you just missed, because your only sampling the voltage.
If you have to measure many inputs, the interval between samples grows significantly, so your chances of missing a peak increase as well.
The solution would be to have a peak detector on each piezo. (Maybe a simple RC-filter circuit will do.)
You also need to get rid of the negative voltages, as they might damage the Arduino’s inputs.

The crosstalk I was talking about is electrically, not acoustically. When changing the MUX channel, you have to charge/discharge the sample-and-hold capacitor in the Arduino’s ADC. This requires current to flow from or to the piezo, in order to equalize the voltage. However, because the output impedance of a piezo is very high, it takes a very long time to charge/discharge that cap (because only a very small current can flow). This means that the charge of the previous measurement partially remains, and you will measure it again, even if that second piezo produced no voltage.

These are some pictures I took of a sensor being hit.
Note how high that voltage is from a raw sensor. It is over 30V
This is after I restricted it to logic levels.
You can still get some ringing on the edge.

I would recommend a voltage follower on the input to make sure you don’t loose the peak voltage.

About the oscilloscope, isn’t there one in the Arduino program?

Yes but not one that would operate that fast.