I saw a project on Instructables which made a Arduino Tinfoil Piano. I wish to recreate that using the Raspberry Pi 3b+. The project involves in using the Capacitive Sensor Library in the Arduino, which converts the pins of the Arduino into the sensors. For the recreation I would have do capacitive sensing using the pi. Searching this online just points me towards Adafruit product links for their capacitive sensor hat. So I wanted to know, if I am able to do capacitive sensing with the pi without any hats, just like the Arduino.

  • You could try making something like playground.arduino.cc/Main/CapacitiveSensor and see if that works but I guess more accurate (but less fun) results come from using a sensor chip designed for the task!
    – user130616
    Commented Mar 14, 2021 at 4:35
  • define "hat" - Since the pi hardware out of the box does not do exactly what you want, additional hardware is required. A "hat" is just that- additional hardware. would a piece of foil, potentiometers, capacitors, and wires count? (could even be a tin-foil hat!)
    – Abel
    Commented Dec 18, 2021 at 13:18
  • theoretical equivalent is possible but requires hardware to overcome a few major differences- Significantly more noise at 3.3V instead of 5V (probably need to run at 5V and use hardware to get it safe for pi). Internal resistances, timings, and capacitance (everything has some) variations may need hardware to shift frequencies to usable ranges.
    – Abel
    Commented Dec 18, 2021 at 13:40
  • Doesn't really answer the Pi, but it seems that the cap sense code variations exist for all kinds of micro controllers; for instance, there is this project for the Pi Pico, it uses single GPIO pins, no hats or additional ICs needed: github.com/todbot/picotouch
    – 0__
    Commented Dec 19, 2021 at 1:25

2 Answers 2


You could port the Arduino Capacitive Sensor library to the Pi, using one of the available GPIO frameworks. You'll have to implement the exact same schematic: one output pin which changes state back and forth, and several input pins (connected via high-value resistors) which will follow the output pin after a certain delay, depending on how much capacitance that particular pin has.

Because capacitive sensing relies on precise timing, the implementation will be more complex and less reliable on the Pi compared to the Arduino - you'll have to figure out whether the delay you observe is due to the change in pin capacitance or to the scheduling of Linux. It should be possible to figure it out by repeating the same measurement several times: human body capacitance changes slowly, while scheduling delays are random. In any case, the capacitive sensing will be slower and less precise on the Pi, which may or may not be OK for the application you have in mind. You'll never achieve the sub-pF sensitivity of the Arduino with the Pi (required to detect a hand 10 inches away from the foil), but detecting an actual touch from a 100 pF human should be possible.


As Dmitry says, you can use capacitor-resistor network and measure the discharge timing to detect touch with the arduino software touch library.

This is the technically correct answer to your "no hats" requirement.

However I have a much more practical solution that will give you much better performance at a low cost and with only relatively minimal amount of programming required.

That solution is to use an ESP32 as they have builtin touch sensor on 10 GPIO pins.

The ESP32 is a low cost (4.5$USD for complete dev board) arduino like microcontroller with high performance plus wifi and bluetooth

So, with this board you can use the TouchLib library to read the hardware touch inputs.

Then you can send these out as messages over the UART on your RPi.

However, if you are feeling a little more fancy than that, I recommend using the Arduino-MIDI library to turn your keypress into proper MIDI keyboard.

You can then use either ipMIDI to send this information over wifi or ethernet to your Pi, or use BLE-MIDI to send it via bluetooth.

By making it a proper MIDI device, you can now use any MIDI software to turn your touches into sounds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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