With a capacitive touch sensor you can use everyday objects as switch inputs with the Raspberry Pi. You can use a variety of things as inputs including potatoes, pencil drawings, toys and anything else you can think of. In this post I’ll show you how to connect your Raspberry Pi to an Adafruit capacitive touch sensor to use potatoes as inputs for your Python programs.
You will need:
- A Raspberry Pi (Model A or Model B)
- A GPIO breakout with a ribbon cable (or jumper wires), like the Adafruit Pi Cobbler
- An Adafruit 5-point capacitive touch sensor
- 5 x 10k resistors
- A selection of breadboard jumper wires
- A breadboard
- Something fun to use as input (I’m using potatoes)
Step 1: Wiring
The first thing you need to connect the components on the breadboard. Use the following diagram:
Note: the capacitive touch sensor is slightly wider in real life so there will be fewer breadboard rows to work with. Other than that this will not affect the layout.
Step 2: Code
Before you start you need to have the GPIO Python module installed. To install it connect to the internet and run the following in a terminal:
$sudo apt-get update $sudo apt-get install python-dev $sudo apt-get install python-rpi.gpio
Now that you have the Python GPIO module installed you can run a Python program. Copy the following code into a Python file and save it as touchPotato.py:
import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # Set the GPIO pins input1 = 4 input2 = 17 input3 = 18 input4 = 22 input5 = 23 GPIO.setup(input1, GPIO.IN) GPIO.setup(input2, GPIO.IN) GPIO.setup(input3, GPIO.IN) GPIO.setup(input4, GPIO.IN) GPIO.setup(input5, GPIO.IN) # Used to make the switch only repeat once prevInput1 = True prevInput2 = True prevInput3 = True prevInput4 = True prevInput5 = True #repeats infinitely to check the GPIO input while True: # Get the state of the inputs. # True = not pressed # False = pressed buttonInput1 = GPIO.input(input1) buttonInput2 = GPIO.input(input2) buttonInput3 = GPIO.input(input3) buttonInput4 = GPIO.input(input4) buttonInput5 = GPIO.input(input5) # If input 1 has been pressed then print "Yellow" if not buttonInput1 and prevInput1: print "Yellow" prevInput1 = buttonInput1 # If input 2 has been pressed then print "Green" if not buttonInput2 and prevInput2: print "Green" prevInput2 = buttonInput2 # If input 3 has been pressed then print "White" if not buttonInput3 and prevInput3: print "White" prevInput3 = buttonInput3 # If input 4 has been pressed then print "Orange" if not buttonInput4 and prevInput4: print "Orange" prevInput4 = buttonInput4 # If input 5 has been pressed then print "Purple" if not buttonInput5 and prevInput5: print "Purple" prevInput5 = buttonInput5 time.sleep(0.05)
Step 3: Run it
Make sure everything is wired correctly and that the Raspberry Pi is connected to the breadboard with a ribbon cable. To run the code open the terminal and navigate to the directory that you saved touchPotato.py. Run the code with sudo:
sudo python touchPotato.py
Press a potato your program should print a colour to the terminal.
How it Works
When you press the potato connected to the the capacitive touch sensor the capacitance of the potato changes. Capacitance is the electronic charge stored by an object. The touch sensor detects this change and connects the corresponding input to ground changing the signal that goes to the inputs on the GPIO. Normally the GPIO receives 3.3v, but when the sensor connects it to ground the signal changes to 0v.
Our Python program checks the input from GPIO pins. Normally the GPIO receives 3.3v when the potato is not pressed. The Python program stores this state in the buttonInput variables as True. When the potato is pressed the input changes to 0v and the Python program stores this state as False in the buttonInput variables. When the buttonInput variable is False, code is run to print a colour string to the terminal. There are five buttonInput variables, one for each of the touch inputs.