Let's get started with the PiicoDev Capacitive Touch sensor and a Raspberry Pi Pico. In this tutorial we'll cover reading the touch-status of each pad, changing some setup options to tune the sensor for different tasks. We'll then remix the code to trigger OS commands at the touch of a button. You could use this to trigger your own scripts, or even safely shutdown your Pi.

Transcript

I'm going to show you how to get started using your PiicoDev capacitive touch sensor and a Raspberry Pi single board computer. We'll connect these two together, run some example code, and then we'll remix that code so that we can change the way the Raspberry Pi behaves at the touch of a button. Let's do it.

To follow along, you'll need a Raspberry Pi computer set up to run just like a desktop computer. Check out our Raspberry Pi for Beginners workshop for help with that. You'll need a PiicoDev capacitive touch sensor and adapter for Raspberry Pi, and a PiicoDev cable. 100mm or longer is best for Raspberry Pi projects.

Connect the adapter to your Pi, making sure that the Ethernet arrow is pointing towards the Ethernet connector on a Raspberry Pi 4. For Raspberry Pi 3, this will be the USB connector. Connect your PiicoDev cable to one of the four PiicoDev connectors on the adapter, and connect the other end to your capacitive touch sensor. And I've just mounted everything to this PiicoDev platform to keep it nice and stable for the rest of the tutorial.

Power on your Pi, navigate to the article for this tutorial in your browser, and find the Downloads section. We need to download the example code for this tutorial. Right click the main.py link, and select Save Link As. Save this somewhere that makes sense, like a PiicoDev directory in your Pi's home directory, and I'm going to name mine touch.py just for some context.

Open the File Manager, navigate to where you saved that file. Here it is, and double click to open in Thonny. And we'll just make sure we have the PiicoDev package installed. Go to Tools, Manage Packages, and search PiicoDev with two I's. Here it is, and just upgrade or install as necessary.

Once you've got PiicoDev installed, we are ready to run. You can press Ctrl R, or press the green Run Script button to begin running this.And you can see in the shell immediately we have the touchpad status, and three digits to indicate the status of each of these touchpads.

If I press touchpad 1, that first digit turns to a 1, and you can see the blue line in the plot has shot up to 1. I'll press 2, there's that second digit turning to a 1, and we have the orange line. And then touchpad 3, and there's the third digit turning to a 1, and the green line goes up.

If I touch 1 and 2, we can actually register simultaneous touch events. So the blue and orange lines have gone up, I'll release 2 so the orange goes back down to 0, and release 1 so the blue goes back down to 0. And of course you'll see that in the shell as well.

If you don't see this graph in the bottom right, you can go to View, and enable the plotter.

Let's take a look at the example script. We start by importing the capacitive touch module, and we also import a sleep function to sleep in milliseconds. We initialise the touch sensor as touch sensor, and then in the infinite loop, we just call touch sensor dot read, and that will read the status of each touchpad, and store it in a dictionary, which we assign to status.

Then in the print statement, we print a nice bit of meaningful text, touchpad status, and we concatenate onto that the status of the first touchpad, the status of the second touchpad, and the status of the third. We have to convert those to strings to print them easily. And then there's a 100 millisecond delay.

We have a few options setting up the touch sensor. When we initialise it, we can set the sensitivity and the touch mode. For example, if we want to reject multiple touch events, we can use the argument touch mode, and set that equal to single. By default we're in multi-touch mode, but this will reject multiple touches.

Run the script, and I'll touch pad 1, and now if I touchHello! It looks like you are experiencing some issues with your touch sensor. Currently, only one pad is active at a time, even when touching the rest of the pads. One solution is to change the sensitivity of the touch sensor. By setting the sensitivity argument, we can adjust the sensitivity of the touch pads. A number between 0 and 7 can be used, with 0 being the most sensitive and 7 being the least sensitive. Adjusting the sensitivity can help avoid spurious touch events if there is a lot of noise around the circuit.

Furthermore, if you wish to modify your touch sensor, you can add text and pictures to it to give each button a new function. You can also adjust the sensitivity of your touch sensor if you intent to attach it with alligator clips onto other objects and use those objects as touch sensors.

On a different note, did you know that you can execute operating system level commands from a Python script by using the OS module? In the interactive shell, we can import os, and then call os.system along with the command we want to run. ls with the l command enables us to view a list of files in our current working directory. This means that you can use a button to trigger OS level events in our script.

If you need further assistance, do not hesistate to let us know.Command will be executed, and you can see that reflected in the shell. That same data is coming through every time I press the 1 button.

I'll just hold it down. Now of course, listing files by itself isn't particularly useful, but this is just representative. This could be any script that you've written yourself.

You can trigger that script with the press of a button. That's pretty cool. Or you could keep things in Python and just branch your code by checking the status of the button as well.

There you have it. We've set up our PiicoDev touch sensor and run a little hello world script to just detect different touch events. But as you've seen, we can also do much more complex things like execute OS commands, and we can change the touch mode and sensitivity of our sensor.

If you make something cool out of this starter project, or if you just have some questions, then head over to the Core Electronics forums. We're full-time makers and here to help.

Until next time, thanks for watching.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.