Use data from micro piezo to retrieve music

Hello,

(I'm not 100% sure if it's the good place to post this topic. If not, please apologize.

Anyway I'm new in the marvelous Arduino world. I recently bought a Uno card and began to make simple circuits.

I also play guitar and owned a micro Piezo like this one : Micros-guitares-acoustiques-dean-markley-artist-piezo-universel-p19639.html | Woodbrass N°1 Français.
I disassembled it to perform the "Knock" tutorial (https://www.arduino.cc/en/Tutorial/Knock). Still no problem.

However there's something I don't understand. With this tutorial, I can measure a hit. Now let's say I stick the piezo on my guitare : how do I do if I want to retrieve the whole sound ?

I found a sketch that was reading the 6 analog input pins whereas the piezo was only connected on the A0. I did it and well read 6 values, all changing when hitting the piezo, but I'm not sure it was really related.

Anyway if someone can explain me, or send me to an article or something...

Thanks in advance !

Florian

The piezo is picking up the vibrations from the guitar body, yes?
"- Electric piezo cell with high output level, fully reproduces the natural sound of acoustic instruments with steel or nylon strings.

  • Can be placed on any other surface in vibration to transmit sound to amplification. Its design prevents any parasitic vibration and any diffusion"
    So it will pick up the complex waveform that is the result of all 6 strings vibrating.
    You can use analogRead to sample that waveform at ~10,000 sample rate at 10 bit resolution. What do you want to do with that data? Store it to an SD card? You can find SD card data loggers to save the info.
    To play it back, you will need an DAC, the Uno does not have one.

Hi,

Indeed the piezo works the way you explain it.

I already use analogRead to retrieve the data from A0, but it's only a simple integer value.
What do you mean by "You can use analogRead to sample that waveform at ~10,000 sample rate at 10 bit resolution" ? How to do that ?

And I don't know yet what to do with the data. Maybe perform a FFT on it to cut it into several parts, and then... I'll see. I'll find :slight_smile: Actually it's not the important stuff for the moment.

I already use analogRead to retrieve the data from A0, but it's only a simple integer value.

Yes and a whole sequence of readings give you the whole sound. That is what digitised audio is all about.

sample rate at 10 bit resolution" ? How to do that ?

You keep on doing an analogue read over and over in a loop putting the data into memory. You can get up to 0.2 seconds of sound sampled inside an Arduino Uno.

Yes and a whole sequence of readings give you the whole sound. That is what digitised audio is all about.

What is a "whole sequence" ? On all analog input pin ?

MrFlo666:
What is a "whole sequence" ? On all analog input pin ?

No. The basic form of digital audio data is what's called PCM, or Pulse-Coded Modulation. That sounds fancy, but all it really means is that, to record a sound, you're:

  • Turning sound waves into voltages (that's what your piezo sensor is doing).
  • Capturing those voltages many, many times (thousands of times--often tens of thousands of times) per second.
  • Converting each captured voltage into a number
  • Storing each number

The Arduino function "analogRead()" performs a single capture and a single conversion, giving you back a number that represents the amplitude of your sound wave during a very brief span of time--less than 1/10,000th of a second.

So after you've called analogRead() thousands of times per second for however many seconds you want to record sound, you'll have many thousands of numbers. To play back that recorded sound, you then have to convert each of those numbers back into a voltage and supply those voltages to an output device like a speaker (typically with some amplifiers in between). If the rate at which you supply those voltages matches the rate at which you captured them up above, you'll hear whatever pleasant sound you recorded. Do it faster or slower, and you'll get a higher- or lower-pitched result, accordingly.

As CrossRoads mentioned, the Arduino has no built-in facility for converting the numbers gotten from analogRead() back into voltages. There are amusing ways to do a low-fidelity job of this using the Arduino's digital outputs and a few cheap components, but the best way is to get a DAC, or Digital-to-Analog Converter, which is a device made for doing exactly this sort of thing.

thanks for your explanation, jahlomes, it's far more clear !

Actually I don't intend to recreate the sound by any way. I'd prefer to analyze the sound, so in a first time be able to divide it into several packs of frequencies with an associated intensity. But I don't know (yet) if the Arduino can perform this kind of things...

But I don't know (yet) if the Arduino can perform this kind of things...

Yes it can. You need to put the data through a FFT ( Fast Fourier Transform ), there are libraries to do this.

That confirms what I thought. Just do it now.

Thanks everyone !

Actually I don't intend to recreate the sound by any way. I'd prefer to analyze the sound, so in a first time be able to divide it into several packs of frequencies with an associated intensity. But I don't know (yet) if the Arduino can perform this kind of things...

Do a search for "Arduino Spectrum Analyzer".

And if you can get-away with only 7 frequency bands, the [u]MSGEQ7[/u] is a cool little chip that does the frequency filtering for you. (That makes your software a lot easier, and it frees-up processor time for other stuff.)

Note that the Arduino doesn't have a lot of processing power so the examples you find are more of a visual effect than an actual spectrum analyzer [u]instrument[/u] used for science & engineering.

If you want to do "serious" audio analysis a regular computer (with the appropriate software) is better, and you might need to learn how to use MATLAB (or a MATLAB clone).

What do you mean by "You can use analogRead to sample that waveform at ~10,000 sample rate at 10 bit resolution" ?

The [u]Audacity website[/u] has a nice-easy introduction to how digital audio works. CD audio is sampled at 44.1kHz (44,100 times per second) but the Arduino's ADC cannot accurately-sample that fast.

How to do that ?

You have to use the [u]micros()[/u] for the timing (or maybe there are a more-advanced ways of doing it with the chips internal timers & interrupts). The 10-bits come "automatically" from the Arduino's ADC.

Note that the Arduino can't read the negative-half of the audio cycle* so the analog input is usually [u]biased[/u] (2 equal-value resistors and a capacitor). With the piezo, you can use higher-value resistors (~1M Ohm) and you'll get a stronger signal. And, you can also leave-out the capacitor (since the piezo is electrically 'like' a capacitor already).

And, if the signal from the piezo is too weak you may need a preamp. Or, you can use a [u]microphone board[/u] that has a microphone, preamp, and bias circuit.

  • The Arduino can actually be damaged by negative voltages, although the piezo probably doesn't put-enough current to do any damage.

Wow I can't say more that : thanks a lot !!

[UPDATE] Actually someone got the same issue. Solution is described there : Problem in serial monitor with FFT library - Audio - Arduino Forum


Hello again,

SO i just tested this FFT example : Example - Open Music Labs Wiki
But the output just looks like nothing readable, for instance :

ÿàÐJ8,+%%#eeee

However the value changes when I hit my piezo, so it seems to work "well".

Any explanation ?

Thanks.