I recently posted here without much success on this subject but I've moved forward a bit and got stuck, again!

I have Raspberry Pi and a MCP3002 ADC chip because RPi doesn't have any analog pins. I have a piezo sensor connected to the ADC then connected directly to the RPi. I followed this site: here. I changed the code to loop indefinitely and print.

What I found is that if I hit the piezo it shoots up to 0.99.. and then slowly decreases over time. But then jumps randomly at times and will sometimes sit at zero. I was wondering if anyone knew, from a electronics point of view why this was? I intend to use the sensor on the head-stock of a guitar to determine the frequency of the note being played but when I sellotape it on I don't get an accurate reflection.

Thank you.

enter image description here

The Piezo disk is 35mm
Resonant Frequency: 2.6KHz ±0.3KHz
Resonant Impedance: 200W Capacitance: 24,000pF ±30%

EDIT: I'm aiming to replicate the techniques used in "clip-on" tuners. But can't find a schematic or help elsewhere.

  • \$\begingroup\$ Short answer is your ADC pins are floating. \$\endgroup\$ – Matt Young Sep 17 '15 at 17:58
  • \$\begingroup\$ No circuit = no-idea \$\endgroup\$ – Andy aka Sep 17 '15 at 17:58
  • \$\begingroup\$ @Andyaka Basic circuit can be found at the website. raspberry.io/media/images/project_gallery_images/ADC_schem.png Piezo positive is connected to CH0 on the MCP3002 and piezo negative is connected to ground. \$\endgroup\$ – JamesDonnelly Sep 17 '15 at 18:02
  • \$\begingroup\$ No, I want a circuit of how you interfaced the piezo to the ADC but am I to also assume you have not read the data sheet for the ADC and therefore not placed power decoupling capacitors? \$\endgroup\$ – Andy aka Sep 17 '15 at 18:05
  • \$\begingroup\$ New to electronics. Thought the guide would suffice. Could you explain please? \$\endgroup\$ – JamesDonnelly Sep 17 '15 at 18:10

The datasheet for that MCP3002 ADC shows in Figure 6-3, an example schematic of how you might connect the ADC, and how capacitors should be connected to ensure it has a stable power supply. The article you reference is not attempting to sample anything quick (like audio), and the circuit doesn't appear to have any decoupling.

The raspberry-Pi project is sampling a mechanical potentiometer, which can typically change at a few 10's of Hz or less. Their instruction "Slowly turn the trimpot knob and watch the number change. " seems to suggest they may have problems even tracking that. Further the raspberry-Pi project isn't trying to reconstruct a frequency from samples, it is just trying to establish the analogue 'state' for a very slow analogue system. So they aren't very sensitive to huge variation in the sampling frequency; it could vary by 100% and they wouldn't care.

An isolated potentiometer is a simpler electronic environment than you describe, so the electrical noise is likely only the MCP3002. So they may be able to get something usable without proper (normal) decoupling.

However, it sounds like your application may be in a much more electrically-noisy environment, so a good power supply, and hence decoupling will be important. So apply the decoupling capacitors recommended by the manufacturers MCP3002 ADC datasheet.

You likely need to sample at a much higher sampling rate (e.g. at least 10x) than the frequency of the guitar string to make things easier (aim for a much higher sample rate than the Shannon_Nyquist sampling limit). That appears feasible, as the MCP3002 datasheet says 200k samples/second.

A sufficient sample rate alone isn't sufficient. It is also important that the samples are taken at a reasonably stable sample rate. Otherwise it may be too difficult to calculate the actual frequency of a signal from samples when there is unknown sample rate variation (jitter). If the jitter were +/- 50%, that could transpose a note by more than an octave.

Looking at stuff on the Raspberry-Pi's SPI driver e.g. Read data at 2KHz sample rate, how to? it says "Using the current kernel driver, you can perform just over 8000 SPI transactions/sec."

I am not an R-Pi expert, so I would strongly recommend you try to get clarity on this.

However, if that really means 8000 samples/second, then it will be harder to solve your problem using only an R-Pi.

I am not a signal processing expert.

It will be even harder (maybe impossible) if the sample rate has significant jitter. Your code will need a reasonable number of samples to estimate frequency (hence the suggestion for at least 10x samples vs actual frequency). That calculation will be undermined if the actual sample rate varies significantly by an unknown amount. If your goal is to tune within a few % of a specific note, then jitter has to be small enough to enable that.

I am not trying to dishearten you. I am just trying to suggest you consider the whole problem, and not just a small part of it. IMHO, taking a sample of a microphone/piezo sensor using an ADC may be the easy part; it is necessary but not sufficient.

It may be worth using a simpler microcontroller based system, like an mbed, Arduino Duo, etc. to sample the signal, and do initial analysis. The benefit should be much lower jitter, and much higher (than 8000sps) sample rate than the R-Pi appears capable of. This would require you to use C/C++ which may be more work than you are planning.


If you have enough C experience to be comfortable, have a look at mbed.org or higher performance Arduino's like a Due. mbed have a cloud-based compiler, which saves hassle installing tools. You could have a poke around to see if it feels usable.

There are many low-cost mbed development boards. For example the ST Nucleos are under £8 (GBP) +VAT e.g. at Mouser/RS/Farnell/etc. They have, at least, 1msps ADC; the STM32F303 have several 5Msps 12bit ADCs. STM32F3/STM32F4 have hardware floating point, and so might even have enough 'oomph' to solve the analysis part of your project.

I'd recommend sketching out the whole problem, and annotating it with the information you know, or can quickly estimate or discover. This can act as a reference 'scope' so when things change, you can check that you don't forget the knock-on effects.


Piezo sensors can be awkward to use, and hence the reason people are asking for a schematic, because the output voltage can easily reach above 20V, which might damage the ADC.

You might protect the input of your ADC with e.g. a zener diode to prevent potentially lethal voltage swings reaching the ADCs input.

  • \$\begingroup\$ I added to two capacitors as shown in figure 6.3. I can't add the op-amp because I don't have the part, but will when I receive it. Can you explain what you mean by not attempting to sample anything quick? And how I would go about doing this? \$\endgroup\$ – JamesDonnelly Sep 18 '15 at 11:45
  • \$\begingroup\$ @JamesDonnelly - I added some clarification. I hope it helps. \$\endgroup\$ – gbulmer Sep 18 '15 at 13:01
  • \$\begingroup\$ The fundamental of the high E string on a guitar is only 330 Hz, so limited sample rate is not necessarily a problem. I would have thought that the piezo's output needs a DC bias to somewhere within the ADC's measurement range, though - say a 1 M resistor from input to ground and another from input to positive supply. The ADC has internal protection diodes, but a resistor in series with the input would also be wise. \$\endgroup\$ – nekomatic Sep 18 '15 at 13:25
  • \$\begingroup\$ @gbulmer thank you for so much information! It's all very helpful. My plan was to take some samples using the raspberry pi (it's the only hardware I can get my hands on for now) to try and test the sensitivity of the piezo. I never realised the other factors like sampling rate. I have some experience with C programming and will try and get a better microcontroller thank you \$\endgroup\$ – JamesDonnelly Sep 18 '15 at 13:55
  • \$\begingroup\$ @JamesDonnelly - I've updated my answer with a few more thoughts. I hope they help. \$\endgroup\$ – gbulmer Sep 18 '15 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.