Let's get started with the PiicoDev Capacitive Touch sensor and a Micro:Bit v2. In this tutorial we'll cover reading the touch-status of each pad, changing some setup options to tune the sensor for different tasks, and make a simple musical instrument - using the touch-pads like keys on a keyboard.

Transcript

G'day, I'm going to show you how to get started using your PiicoDev capacitive touch sensor with a Micro:bit V2. We'll connect these two together, get some example code running so we can measure touch events and then we'll remix the code to make a little musical instrument. Let's do it.

To follow along, you'll need a Micro:bit V2, a PiicoDev capacitive touch sensor and adapter for Micro:bit and a PiicoDev cable. Plug the Micro:bit into the adapter, making sure that the buttons are facing up. Plug your cable into one of the connectors on the adapter and connect the other end to your touch sensor. Now I've just mounted everything to this PiicoDev platform to keep it nice and stable for the rest of the guide.

Finally, connect your Micro:bit to your computer with a USB cable. Find the download section in the article to find three links to files that we'll need to download. Right click each link and select save link as. I'm going to save mine somewhere that makes sense, like a PiicoDev directory in my documents.

Open Thonny and navigate to that directory where you saved your files. If you haven't used Thonny before with the Micro:Bit, we have a guide to help get you started there. Or if you prefer to use a more online programming environment, we have a guide with using python.MicroBit.org with the micro bit.

Make sure your Micro:bit is selected in the run select interpreter menu and make sure we're running the Micro:bit interpreter and connect to it using the stop restart backend button. We should see in the file pane that Micro:bit appears.

We'll upload all three files to our Micro:Bit, so select the first one and hold shift and select the last one. Then right click and upload to micro bit. We can see that those three files are now uploaded.

Let's open up main.py on the Micro:bit to take a look. This is the example code that we're working.We initialize the touch sensor and then we call status equals touch sensor dot read. So we assign the status of each of these sensors to the status variable and then we just print it in the print statement.

Let's take it for a test drive. I'll press control D to restart and we can see in the shell that we have touch pad status and then a digit to represent the status of each of the pads, one, two, and three.

If I touch pad one, then that first digit turns to a one, pad two, the second digit turns to a one and pad three. You can see that reflected in the graph as well. If I press one and then press two as well and then release two and release one, we can see that red and blue line moving from zero to one and then back down.

Now you might've noticed that we can detect two touch events at the same time. Here I'm touching pads one and two and the corresponding digits are both one. I'll come off two and then come off one and we're back to zero.

If you want to reject multiple touch events, we can include an argument in the initialization function where we initialize the touch sensor. We can include the argument touch mode equals single. By default we initialize as touch mode equals multi, but if we initialize as touch mode equals single, press control C and then control R to save, re-upload and rerun. Now when I press pad one and pad two at the same time, I only get that first touch event, pad one. I'll press pad two and three and no matter how much I press, I can only register that first event, which was pad two.

You might also want to change the sensitivity. The sensitivity is tunable in eight steps from seven being the least sensitive to zero being the most sensitive. So we can include a keyword argument here, sensitivity equals, and I'll make it a really high sensitivity with a sensitivity of one.

Now if I bring my finger to one of the pads, it's detected right away, even with just a tiny touch. However, if I increase the sensitivity too much, it may be too sensitive and will be triggered even without touching the pad. So adjust the sensitivity to a level that fits your own needs.little messy. Then let's create a function called play_note that takes a single argument called note, and we're just going to pass that note argument straight to the music.play_tone function.

To not fully touching, I can just bring my finger near the touch pad and we can still register a touch event. You might want to tune the sensitivity if you want to put a label over your touch sensor. This way you can put your own text or pictures on the touch sensor and give each button a new meaning. You might also want to tune the sensitivity if you intend to clip onto these ring terminals with some alligator clips so that you can clip them onto other objects and use those objects as touch sensors.

So what's actually going on in this code? Well, we start by importing the device driver, the device module for the PKDev capacitive touch sensor, and we import a sleep function to create a delay. We initialize our touch sensor by calling the initialization function. And then in the infinite loop, the while true loop, we just call touch sensor dot read to read the status of each of these pads. And we assign that to a dictionary variable called status.

Then in the print statement, we just call print touch pad status, which is how we get this text in the shell, and then we just print a string of the status for key or pad number one, the status of pad number two, and the status of pad number three. And then there's just a 100 millisecond sleep.

So now that we know how to read the status of each pad, let's remix this code. I know that the Micro:bit V2 has an onboard speaker. So why don't we make a musical instrument where we have like a three key keyboard using the three touch pads of our sensor, depending on which pad you touch, the speaker will play a different note. I'm going to start by adding import music at the top.

I know that we're going to want to play some kind of musical note at the speaker. I'll comment out this print statement with a alt three to add a hash at the start of the line, because this is going to make things a little messy. Then let's create a function called play_note that takes a single argument called note, and we're just going to pass that note argument straight to the music.play_tone function.Bit confusing. Now we can just check which key is pressed and play that musical note. If status one, so if key one is equal to one, which means that it's pressed, then we can call music.play. And this is where we can play a note. I'll pick F sharp. That seems like a nice enough note, and we can just repeat that for the status of key two.

If key two is pressed, let's play an E and I'll paste it again if status three. So if pad three is pressed, we'll play a D note and I'll make sure up here that we have touch mode equals single, so I'll save.

Now, save the script and run, save and run with control R, and our musical instrument is now running. If I press pad one, that's an F sharp. That's a, what's that? An E and a D. Nice.

So there you have it, using the PiicoDev capacitive touch sensor, we can detect single or multiple touch events. We can tune sensitivity, and we can even change the branching of our program to do different things depending on which button is pressed. In this case, we made a simple musical instrument.

If you make anything cool out of this little starter project, or if you just have some questions, head over to the Core Electronics forums. We're full-time makers and we'd love to see you there. 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.