Connect a capacitive touch sensor

In this example we will connect a capacitive sensor to a GPIO pin on our Raspberry PI, when we touch the sensor we will display a message to the user. A practical use may be a touch activated lamp.

This is the sensor we used which is low cost and available from multiple places

Capacitive_Touch

 

Here is a schematic representation, fairly straightforward

pi and cap sensor_schem

Lets look at the code, this is written in python and requires the RPi library to be installed, I recently downloaded NOOBs,I  installed Raspbian and python and this library were available by default.

Open the terminal and type in the following

[codesyntax lang=”bash”]

sudo idle

[/codesyntax]

Wait for IDLE to open,  then click File > New to open a new window. Or you can use CTRL + N.

Now enter the following code

[codesyntax lang=”python”]

    import time
    import RPi.GPIO as GPIO
	
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(23, GPIO.IN)
     
     
    while True:
		pressed = GPIO.input(23)
		if padPressed:
			print "cap pressed"
		
    time.sleep(0.5)

[/codesyntax]

Click File > Save when you are finished (Ctrl + S).

To run your program, click Run > Run (Ctrl + F5), your should output like the following

cap output

Related posts

How to Measure Internet Speed In Python Using Speedtest

Getting started with Tkinter on the Raspberry Pi

Samba Setup on a Raspberry Pi

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More