# Capacitive Touch Sensors on the Raspberry Pi

## Overview

Capacitive touch breakout boards are an excellent way to use household objects as inputs on your Raspberry Pi. Any conductive object can act as a switch when connected to sensor boards, including potatoes, apples, spoons and pencil graphite.

![](https://cdn-learn.adafruit.com/assets/assets/000/014/965/medium800/raspberry_pi_5apples.jpg?1394381373)

The capacitive touch sensors detect when you touch the board's pad or an object connected to the board. Humans carry a small electrical charge. When you touch a capacitive touch sensor it will detect this charge.   
  
With some simple wiring to the Raspberry Pi and a few lines of Python, you've got a fun and easy way to control your projects and games. For example at the end of this guide we'll show you how to control Minecraft: Pi Edition using a apples.  
  
There are three breakout boards that this guide will show you how to use with your Raspberry Pi.

# Momentary
The [momentary capacitive touch sensor](http://www.adafruit.com/products/1374 "Link: http://www.adafruit.com/products/1374") will be active as long as something is touching it. The LED shows whether a touch has been detected.  
  
The board has a large touch-pad. The small copper hole near the touch-pad can be used to connect the board to capacitive items such as a drawing made with pencil graphite.  
![raspberry_pi_1374-00.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/765/medium640/raspberry_pi_1374-00.jpg?1396903199)

# Toggle
The [toggle capacitive touch sensor](http://www.adafruit.com/products/1375 "Link: http://www.adafruit.com/products/1375") will activate when you press it and deactivate when you press it a second time. The LED will light up to indicate whether your touch has been detected.  
  
Like the momentary sensor, this sensor has a large touch-pad. The small copper hole near the touch-pad allows you to use a wire to connect your board to everyday objects, like a spoon, which can then be used as an input.  
  
  
![raspberry_pi_1375-00.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/766/medium640/raspberry_pi_1375-00.jpg?1396903228)

# 5-Pad
The [5-Pad capacitive touch sensor](http://www.adafruit.com/products/1362 "Link: http://www.adafruit.com/products/1362") offers a total of 5 inputs on one board. It is very similar to the momentary board, but includes more inputs. Unlike the other boards it does not have any pads, instead the pins on the right of the board are used to detect touch. These pins can be connected to wires, which can be attached to everyday objects, like bananas.  
![raspberry_pi_1362-00.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/768/medium640/raspberry_pi_1362-00.jpg?1396903280)

# Capacitive Touch Sensors on the Raspberry Pi

## Assembly

Assembling the capacitive touch sensors is very straightforward and can be achieved with minimal soldering.  
  
If you're new to soldering, check out the [Adafruit Guide to Excellent Soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/).

## Headers
Cut the headers and place them onto a solderless breadboard.  
![raspberry_pi_headers.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/769/medium640/raspberry_pi_headers.jpg?1396903446)

## Place the Board
Place the boards onto the headers and check that they are spaced correctly.  
![raspberry_pi_unsoldered.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/770/medium640/raspberry_pi_unsoldered.jpg?1396903617)

## Solder
Solder the pins with a soldering iron.   
  
To get the momentary and toggle boards to lie flat on the headers, solder a single pin. Reheat the solder on the pin and adjust the board until you are happy with its positioning.  
![raspberry_pi_soldering.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/771/medium640/raspberry_pi_soldering.jpg?1396903702)

## Admire
Take a moment to sit back and admire your work.  
![raspberry_pi_unsoldered.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/772/medium640/raspberry_pi_unsoldered.jpg?1396903862)

# Capacitive Touch Sensors on the Raspberry Pi

## Wiring

In this section we'll learn how to wire up the sensor boards to a Raspberry Pi. For this you will need:

- A capacitive touch sensor ([momentary](https://www.adafruit.com/product/1374), [toggle](https://www.adafruit.com/product/1375) or [5-pad](http://www.adafruit.com/products/1362))
- [A solderless breadboard](http://www.adafruit.com/products/64 "Link: http://www.adafruit.com/products/64")
- [Breadboarding/jumper wire](http://www.adafruit.com/products/153)
- Pi Cobbler (pick one):
  - 40-pin (Pi - A, B, B+, Zero) - [Assembled Pi Cobbler Plus](https://www.adafruit.com/product/2029)
  - 40-pin (Pi - A, B, B+, Zero)- [Assembled Pi T-Cobbler Plus](https://www.adafruit.com/product/2028)
  - 20-pin (Pi - Rev 1 and 2 only) - [Assembled&nbsp;Pi T-Cobbler](https://www.adafruit.com/product/1754)

- A 10k resistor (no resistors are required for the 5-pad board)
- Apples, potatoes, bananas or another fun object that you can connect the board to
- [A Raspberry Pi](https://www.adafruit.com/product/3775) - All Models except the compute node are supported.&nbsp;&nbsp;
- Power supply, mouse, monitor, monitor cable, keyboard and SD card for the Raspberry Pi

# Momentary Board Wiring
When wiring my boards I prefer to use Adafruit's Pi Cobbler to connect my Raspberry Pi to the breadboard, but you can also use jumper wire to connect to the Raspberry Pi's GPIO instead.

Below there are both 40-pin and 20-pin schematics for modern and older Raspberry Pi models.&nbsp;

## 40-Pin (A, B, B+ and Zero) T-Cobbler Plus Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/074/913/medium800/sensors_momentary-board-wiring-40-pin_bb.png?1556148660)

## 20-Pin (Raspberry Pi Rev 1 and Rev 2) Cobbler Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/014/467/medium800/raspberry_pi_momentary.png?1392827677)

# Toggle Board Wiring
The wiring for the momentary and toggle breakouts is not identical. The momentary board uses 4 header pins whereas the toggle board uses 5 header pins. The VCC is in a different position on the two boards.

Below there are both 40-pin and 20-pin schematics for modern and older Raspberry Pi models.&nbsp;

## 40-Pin (A, B, B+ and Zero) T-Cobbler Plus Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/074/914/medium800/sensors_toggle-board-wiring-40-pin_bb.png?1556148840)

## 20-Pin (Raspberry Pi Rev 1 and Rev 2) Cobbler Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/014/466/medium800/raspberry_pi_toggle.png?1392827563)

# 5-Pad Sensor Wiring

The 5-pad capacitive breakout works in the same way as the momentary and toggle breakouts. It has more inputs than the other boards and therefore requires more GPIO pins on the Raspberry Pi.  
  
Although there are 5 inputs on the 5-pad board, only one output can be active and transmitted to the Pi at a time.   
  
Each of the input wires can be connected to objects. I like to use apples and potatoes.

Below there are both 40-pin and 20-pin schematics for modern and older Raspberry Pi models.&nbsp;

## 40-Pin (A, B, B+ and Zero) T-Cobbler Plus Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/074/915/medium800/sensors_5pad-sensor-wiring-40-pin_bb.png?1556149067)

## 20-Pin (Raspberry Pi Rev 1 and Rev 2) Cobbler Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/014/961/medium800/raspberry_pi_5pad.png?1394375825)

Danger: 

# LEDs
When you press the pad on the board the LED should light up. You must have your Raspberry Pi switched on and connected to the board in order for this to happen. Even if you're not running any programs that use the GPIOs, the LEDs will still light up as the boards are connected to the power and ground pins on the Pi.   
  
If it works, well done, you're ready to move onto creating a Python program that uses the board.   
  
The 5-pad board has 5 LEDs, one for each input.   
# Connecting to Objects
Each of the boards can be connected to everyday objects that are conductive. This includes fruit, vegetables, plants, metals, animals, conductive fabrics, pencil graphite and conductive paint to name a few.   
  
With some objects you may find that the sensor will be activated when your hand is near the object, but not touching it. This is normal and very common with fruits and vegetables that contain a lot of water.  
  
Adafruit also stocks a number of materials which are excellent for connecting your sensors to:  

- [Bare Conductive paint](http://www.adafruit.com/products/1305) (also [available as a pen](http://www.adafruit.com/products/1306))
- [Copper tape](http://www.adafruit.com/products/1128)
- [Conductive fabric](http://www.adafruit.com/products/1167)
- [Conductive thread](http://www.adafruit.com/products/641)  
- [ITO (Indium Tin Oxide) Coated PET Plastic](http://www.adafruit.com/products/1309)  

  
Materials that are not conductive will not work. Plastics, wood, fabric and glass are all not conductors.  
  
Danger: 

# Capacitive Touch Sensors on the Raspberry Pi

## Programming

These example programs show you how to use the capacitive touch sensors with our CircuitPython Libraries that are part of adafruit-blinka. It is relatively straightforward to adapt the programs to do different things. Just change the lines with print "pressed".  
  
Two versions of the code are provided. One will continuously print output to the terminal while the touch sensor is pressed. The other will only print output once, irrelevant of how long the pad is pressed. Both pieces of code will work with the momentary and toggle boards.  
  
Code for 5-pad board can be found on the next page of the guide.

The software is exactly the same on all 40-pin and 20-pin Raspberry Pi models.&nbsp;

Make sure you have connected the boards using the wiring instructions in the previous section before continuing.

# CircuitPython Library Setup

See&nbsp;[CircuitPython Libraries on Raspberry Pi&nbsp;](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)to get a fresh Raspberry Pi setup.

If you have a running Raspberry Pi with an up to date copy of Raspbian you can simply run the following command to install adafruit-blinka.&nbsp;

```auto
$ sudo pip3 install adafruit-blinka

```

# Continuous Output
This version of the code will continuously print output while the pad is pressed.  
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/Continuous_Output.py

# Single Output
This version of the Python code will only print an output once each time the sensor detects a touch.   
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/Single_Output.py

# Running the Code

Open a terminal on your Pi.

Pull Down the two scripts directly onto your Raspberry Pi using wget.

```auto
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/Continuous_Output.py
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/Single_Output.py
```

Run the code using the following commands. Try tapping vs holding your finger on the keypad. You should see "pressed" output in continuous mode or per tap depending on the script.

```auto
$ sudo python3 ./Continuous_Output.py
```

![](https://cdn-learn.adafruit.com/assets/assets/000/074/964/medium800/sensors_cap-continuous.png?1556223473)

```auto
$ sudo python3 ./Single_Output.py
```

![](https://cdn-learn.adafruit.com/assets/assets/000/074/965/medium800/sensors_cap-single-output.png?1556223486)

# Capacitive Touch Sensors on the Raspberry Pi

## Programming the 5-Pad Sensor

The code for the 5-pad capacitive touch sensor can be found below. It cannot be swapped with either of the single pad boards as they are wired differently.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/5pad.py

![](https://cdn-learn.adafruit.com/assets/assets/000/015/775/medium800/sensors_raspberry_pi_pad5.gif?1448048063)

# Running the Code

Open a terminal on your Pi.

Pull Down the script directly onto your Raspberry Pi using wget.

```auto
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/5pad.py
```

```auto
$ sudo python3 ./5pad.py
```

The program works in a very similar way to the programs for the momentary and toggle sensor boards. If you're having trouble understanding how the code works for the 5-pad, have a look at the code on the previous section of this guide first.

# Capacitive Touch Sensors on the Raspberry Pi

## Controlling Minecraft: Pi Edition

The GPIO pins on the Raspberry Pi are one of the reasons for its popularity. In this guide we've used the CircuitPython libraries to connect to capacitive touch sensors. This has a range of applications and allows you to create some really creative projects.   
  
The Raspberry Pi has a special version of Minecraft that can be programmed with Python. Combining Minecraft on the Raspberry Pi with our capacitive touch sensors allows us to create programs with limitless potential for creativity and fun.

## Install Minecraft: Pi Edition

Before we begin you'll need to have Minecraft: Pi Edition installed on your Raspberry Pi. To install Minecraft Pi you can use [this guide on the Adafruit Learning System](http://learn.adafruit.com/running-minecraft-on-a-raspberry-pi).

## Copy the API
In order to connect to a Minecraft game you will need to copy the API.   
  
Open a terminal and create a new directory named _Minecraft_ within the _Documents_ directory with the following command: > mkdir ~/Documents/Minecraft

Next, we will copy the API into this folder with the following command:

> cp -r ~/mcpi/api/python/ ~/Documents/Minecraft

# Super Jump
This code uses the capacitive touch pad with Minecraft: Pi Edition to make the player jump 10 blocks into the air when the pad is pressed.  
  
This code works best with the momentary board, though can be used with the toggle board and can be adapted for the 5-pad board.  
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-Super_Jump.py

# Melons
This code will place a melon block at the current position of the player. If you hold down the touch pad of the capacitive touch sensor or use toggle sensor, a trail of melons will be created wherever the player walks. This example works particularly well with the toggle sensor board.  
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-Melons.py

# Control Panel (5-Pad Board)
In the last example we use the 5-pad capacitive touch sensor board to do 5 different things in Minecraft Pi.  
  
This is what each pad does:  

> 0 - Teleports the player to co-ordinates (0, 0, 0)  
> 1 - Places a flower at the player's position  
> 2 - Places explosive TNT at the player's position  
> 3 - Checks if the player is in water  
> 4 - Toggles whether or not blocks can be smashed

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-5pad.py

# Download the Code
Open a terminal on your Pi.

Pull Down the above three scripts directly onto your Raspberry Pi using wget.

```auto
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-Super_Jump.py
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-Melons.py
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-5pad.py
```

# Running the Code

To run any of the examples on this page you will need to be in Minecraft game world on your Raspberry Pi. You will get an error if you don't have Minecraft open or if you're on the title screen.  
  
All of these examples use the same wiring as the rest of this guide.

The code runs like any other Python program that uses the GPIO. You can run it from the terminal on your Raspberry Pi with the following commands:

```auto
$ sudo python3 ./MC-Super_Jump.py
```

```auto
$ sudo python3 ./MC-Melons.py
```

```auto
$ sudo python3 ./MC-5pad.py
```


## Featured Products

### Standalone Momentary Capacitive Touch Sensor Breakout

[Standalone Momentary Capacitive Touch Sensor Breakout](https://www.adafruit.com/product/1374)
This breakout board is the simplest way to create a project with a single "momentary" capacitive touch sensor. No microcontroller is required here - just power with 1.8 to 5.5VDC and touch the pad to activate the sensor.  
  
When a capacitive load is detected (e.g. a person...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1374)
[Related Guides to the Product](https://learn.adafruit.com/products/1374/guides)
### Standalone Toggle Capacitive Touch Sensor Breakout

[Standalone Toggle Capacitive Touch Sensor Breakout](https://www.adafruit.com/product/1375)
This breakout board is the simplest way to create a project with a single "toggle" capacitive touch sensor. No microcontroller is required here - just power with 1.8 to 5.5VDC and touch the pad to activate the sensor.  
  
This sensor is a toggle output type: touch-on then...

In Stock
[Buy Now](https://www.adafruit.com/product/1375)
[Related Guides to the Product](https://learn.adafruit.com/products/1375/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Breadboarding wire bundle

[Breadboarding wire bundle](https://www.adafruit.com/product/153)
75 flexible stranded core wires with stiff ends molded on in red, orange, yellow, green, blue, brown, black and white. These are a major improvement over the "box of bent wires" that are sometimes sold with breadboards, and faster than stripping your own solid core wires. Makes...

In Stock
[Buy Now](https://www.adafruit.com/product/153)
[Related Guides to the Product](https://learn.adafruit.com/products/153/guides)
### Assembled Pi Cobbler Plus - Breakout Cable

[Assembled Pi Cobbler Plus - Breakout Cable](https://www.adafruit.com/product/2029)
The Raspberry Pi B+ / Pi 2 / Pi 3 / Pi 4 / Pi 5 has landed on the Maker World like a 40-GPIO pinned, quad-USB ported, credit card sized bomb of DIY joy. And while you can use most of our great Model B accessories by hooking up our [downgrade...](https://www.adafruit.com/product/1986)

Out of Stock
[Buy Now](https://www.adafruit.com/product/2029)
[Related Guides to the Product](https://learn.adafruit.com/products/2029/guides)
### Assembled Pi T-Cobbler Plus - GPIO Breakout

[Assembled Pi T-Cobbler Plus - GPIO Breakout](https://www.adafruit.com/product/2028)
 **This is the assembled version of the Pi T-Cobbler Plus. &nbsp;It only works with the Raspberry Pi Model Zero, A+, B+, Pi 2, Pi 3, Pi 4, and Pi 5!** (Any Pi with 2x20 connector)  
  
The Raspberry Pi has landed on the Maker World like a 40-GPIO pinned, quad-USB ported,...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2028)
[Related Guides to the Product](https://learn.adafruit.com/products/2028/guides)
### Raspberry Pi 3 - Model B+ - 1.4GHz Cortex-A53 with 1GB RAM

[Raspberry Pi 3 - Model B+ - 1.4GHz Cortex-A53 with 1GB RAM](https://www.adafruit.com/product/3775)
The Raspberry Pi 3 Model B is the most popular Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 3 better? How about a&nbsp;_faster_ processor, 5 GHz WiFi, and updated Ethernet chip with PoE capability?...

In Stock
[Buy Now](https://www.adafruit.com/product/3775)
[Related Guides to the Product](https://learn.adafruit.com/products/3775/guides)
### Premium Female/Male 'Extension' Jumper Wires - 40 x 3" (75mm)

[Premium Female/Male 'Extension' Jumper Wires - 40 x 3" (75mm)](https://www.adafruit.com/product/825)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are approximately 3" (75mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on one end and 0.1" female...

In Stock
[Buy Now](https://www.adafruit.com/product/825)
[Related Guides to the Product](https://learn.adafruit.com/products/825/guides)

## Related Guides

- [OpenClaw on Raspberry Pi](https://learn.adafruit.com/openclaw-on-raspberry-pi.md)
- [Raspberry Pi E-mail Notifier Using LEDs](https://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds.md)
- [Adafruit's Raspberry Pi Lesson 4. GPIO Setup](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup.md)
- [Matrix and 7-Segment LED Backpack with the Raspberry Pi](https://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-pi.md)
- [Circadian Pi Desk Light](https://learn.adafruit.com/circadian-pi-desk-light.md)
- [Capacitive Touch Holiday Light Control](https://learn.adafruit.com/capacitive-touch-holiday-light-control.md)
- [LoRa and LoRaWAN Radio for Raspberry Pi](https://learn.adafruit.com/lora-and-lorawan-radio-for-raspberry-pi.md)
- [Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing.md)
- [Adafruit's Raspberry Pi Lesson 10. Stepper Motors](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-10-stepper-motors.md)
- [Adafruit AMG8833 8x8 Thermal Camera Sensor](https://learn.adafruit.com/adafruit-amg8833-8x8-thermal-camera-sensor.md)
- [Adafruit Capacitive Touch Sensor Breakouts](https://learn.adafruit.com/adafruit-capacitive-touch-sensor-breakouts.md)
- [Windows IoT Core Application Development: Headless Blinky](https://learn.adafruit.com/windows-iot-application-development-headless-application.md)
- [Raspberry Gear](https://learn.adafruit.com/raspberry-gear.md)
- [Raspberry Pi Analog to Digital Converters](https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters.md)
- [Large Pi-based Thermometer and Clock](https://learn.adafruit.com/large-pi-based-thermometer-and-clock.md)
