# Pluck Sensor Circuit

A [while ago](https://www.jefftk.com/p/mandolin-harp-sensor-placement) I finished the "user interface" portion of my electronic harp mandolin. I'm [happy with the signals](https://www.jefftk.com/p/prototyping-pluck-sensors) the piezos put out, but now I need some electrical engineering to get the signals into a computer where I'll be more at home.

Since I made a design with 13 piezos, I wanted something with at least that many analog to digital converters, and decided on the [Teensy 4.0](https://www.pjrc.com/store/teensy40.html) with 14. It turns out that this only has ten easily accessible ADCs, though, and in retrospect the [4.1](https://www.pjrc.com/store/teensy41.html) would have been a better choice. More on that later!

Reading the docs, each ADC pin converts an input voltage between 0 and +3.3v into a number between 0 and 1023. The piezo puts out voltages centered on zero, and not guaranteed to have a peak-to-peak of under 3.3v. So we have two problems: how do we bias the pizeo's output up, and how do we ensure it stays in range?

I talked to [my TAs](https://mas837.media.mit.edu/) and [posted on StackExchange](https://electronics.stackexchange.com/questions/707747/is-this-a-good-circuit-for-connecting-a-piezo-disc-sensor-to-a-microcontroller), and ended up with this circuit:

[![](https://www.jefftk.com/emandoharp-basic-circuit.png)](https://www.jefftk.com/emandoharp-basic-circuit-big.png)

To center the pizeo's output halfway between 0 and 3.3v I've used resistors to make a voltage divider. Since R1 through R4 all have the same values, as we go around the circuit each will drop the voltage by the same amount:

[![](https://www.jefftk.com/emandoharp-basic-circuit-annotated-resistors.png)](https://www.jefftk.com/emandoharp-basic-circuit-annotated-resistors-big.png)

To keep voltages from getting too high or low for the ADC I've used two diodes. With an ideal diode this would keep the ADC between +2.48 and -0.83:

[![](https://www.jefftk.com/emandoharp-basic-circuit-annotated.png)](https://www.jefftk.com/emandoharp-basic-circuit-annotated-big.png)

Since in practice the diode will have some resistance and some delay, this 2x margin should keep us between 0 and +3.3v.

The R5, in parallel with the piezo, is to pull the ADC pin back to the midpoint.

When running multiple sensors, only the piezo, R5, the diodes, and the ADC pin need to be duplicated; everything else can be shared.

I assembled a single-sensor version on a breadboard, and tested it with a [cheap oscilloscope](https://www.amazon.com/dp/B0BXC6XZVS). I'm not totally confident I was using it correctly, but I think it said the voltage was staying within range, so I assembled a two-sensor version and hooked up the microcontroller:

[![](https://www.jefftk.com/emandoharp-breadboard.jpg)](https://www.jefftk.com/emandoharp-breadboard-big.jpg)

I followed the [Teensy tutorial](https://www.pjrc.com/teensy/tutorial4.html) and wrote some code [github](https://github.com/jeffkaufman/teensy-adc/blob/main/peak-detector.ino) to look for a pluck and dump the ADC values around it. Here's what I saw:

[![](https://www.jefftk.com/emandoharp-adc-by-sample-index.png)](https://www.jefftk.com/emandoharp-adc-by-sample-index-big.png)

Looks pretty good! I played with the detection and got it to where it could reliably determine how hard I plucked, and almost always determine which direction. That "almost always" is probably not good enough for a musical instrument, though, so I'll need more work there if I want it bisonoric. Right now it's using that the second peak is nearly always higher than the first, but this isn't quite always true and I think code that looks at the shape of the peaks could help.

I'm also not using the ADC to it's full extent, which is fine for one or two pins but would be a problem when trying to use all of them. There [are docs](https://www.pjrc.com/teensy/adc.html) for optimal sampling, but I haven't gone through them yet.

I'd like to move this to a circuit board so I can make something more robust than my breadboard toy, and can include more sensors without driving myself nuts. It looks like [KiCad](https://www.kicad.org/) is the standard tool here, and while I normally don't like video for documentation I found [this one](https://www.youtube.com/watch?v=3FGNw28xBr0) did a great job with moving fast enough and skipping the repetitive bits. Here's the schematic I ended up with:

[![](https://www.jefftk.com/emandoharp-schematic-ten.png)](https://www.jefftk.com/emandoharp-schematic-ten-big.png)

If you count the pizeo inputs, why are there only ten? The problem is that only ADC pins A0 through A9 on the Teensy 4.0 are standard pins. The other four ADCs, A10 through A13, are surface mounts on the underside of the board:

[![](https://www.jefftk.com/teensy-40-bottom-card10b_rev1.png)](https://www.jefftk.com/teensy-40-bottom-card10b_rev1-big.png)

The [Teensy footprint library](https://github.com/XenGi/teensy.pretty) I'm using doesn't know about these pins, and I wouldn't want to solder them anyway. Possibly I'll update my design to use a Teensy 4.1, which has [pins for A0 through though A17](https://www.pjrc.com/teensy/pinout.html) but for now I just have a ten-piezo design.

[![](https://www.jefftk.com/emandoharp-ten-input-pcb.png)](https://www.jefftk.com/emandoharp-ten-input-pcb-big.png)

KiCad includes a 3D viewer, which is fun, though the library I used for the Teensy apparently doesn't specify a 3D model:

[![](https://www.jefftk.com/emandoharp-board-ten-input-3d-view.png)](https://www.jefftk.com/emandoharp-board-ten-input-3d-view-big.png)

I was thinking of ordering this from [JLCPCB](https://jlcpcb.com/), who seem to have very good pricing for low-volume orders?

I think my two main options at this point are to go ahead with this smaller version, or redo it with a Teensy 4.1 to get all the inputs?

If anyone wants to play with the design, the KiCad files are [on github](https://github.com/jeffkaufman/emh-pcb/tree/main/ten-input).