Sensing Capacitive Touch – MPR121 + Arduino

Tuesday, May 31 st , 2011

Every now and then you get sick of the typical push buttons and you want something cooler. And what is cooler than touch sensitive things? Remember that old lamp in your Grandma’s that changed brightness just by touching the base? Yeah, that’s right… We are talking THAT cool! The [[Capacitive_Touch_Sensor_-_MPR121 |MPR121 capitative touch sensor]] gives you 12 of such inputs that we can use with our Arduino, and you can get it in several forms from SparkFun – Im using the basic breakout board.

How? Well… Capacitive sensing is pretty simple in concept. Every object has [[capacitance]], (in laymen’s terms capacitance is just an objects ability to hold a charge), and when you come in contact with something you change its capacitance. Well the [[Capacitive_Touch_Sensor_-_MPR121 |MPR121]] just looks for these changes, and tells us when it happens.

Hooking It Up

Hooking it up to your Arduino is pretty simple, the [[Capacitive_Touch_Sensor_-_MPR121 |MPR121]] is an [[I2C]] device. [[I2C]] is a 2-wire [[serial]] connection, so you just need to connect the SDA (Data) and SCL (Clock) lines to your Arduino for communication. On your Arduino (everything but the [[Arduino Mega|mega]]) SDA is on analog pin 4, and SCL is on analog pin 5. On an [[Arduino Mega]], SDA is digital 20, and SCL is digital 21.

When the MPR121 senses a change, it pulls an interrupt pin LOW. Now if we were ninja’s we would use this oppertunity to create an interrupt on our Arduino and deal with changed immediately. However, to simplify things for us non-ninjas, we are just going to check that pin to see if it is LOW during our loop. To do this, this sensor also needs access to another digital pin, and in this case we are using digital 2.


This is a touch sensor, so we need something to touch right? We call these electrodes. Typically, electrodes can just be some piece of metal, or a wire, or my favorite, metal tape (just make sure you have at least 1/2in of bare wire in contact with the sticky part of the tape for good contact). But some times depending on the length of our wire, or the material the electrode is on, it can make triggering the sensor difficult. For this reason, the MPR121 allows you to configure what is needed to trigger and untrigger an electrode. So if you are getting false or no positives, you may need to change that.

Proximity Sensing

There is a small change in capacitance even as you approach an electrode, just through the air. So if you set the touch threshold, or release threshold too low you may notice it trigger even a few inches away. If you want it to trigger from a decent distance away, the MPR121 supports aggregating all of the electrodes together to use it as a single large proximity sensor – But you will need to read the doc sheet to figure that one out, sorry.


You may have noticed that SparkFun has example code for this sensor already – Actually, this code is slightly based off of theirs. But this setup and code is not limited to sensing one electrode at a time like their’s is. Also this is using the Wire Library to simplify the code to only 2 files. And… We can also sense when one of the electrodes is let go as well.

I know this code looks complicated, but most of it is just configuring the sensor (part based off of sparkFun’s code). All it does is output in the serial terminal when one of the electrodes is touched or untouched. You can touch all of them at once if you want, and it will show that, then just let go of one, and only that one will show as let go.

Feel free to change the setup values in section C to adjust the sensitivity of the electrodes. You will see constants used in the code such as TOU_THRESH, these values are defined in mpr121.h incase you need to change them.

If you want to trigger individual functions based on the electrode touched, you could do something like this and just add the call to your function under where it says “//X was touched”
[code lang=”arduino”]

if(touchStates[i] == 0){
//pin i was just touched
Serial.print(“pin “);
Serial.println(” was just touched”);

switch (i) {
case 0:
//0 was touched

case 1:
//1 was touched

case 2:
//2 was touched

case 3:
//3 was touched



I hope this was helpful in getting your MPR121 up and running with your Arduino, and if you have any questions, please use the discussion link.


Ciao – iPhone app for controlling your arduino

Saturday, May 7 th , 2011

If you have ever needed to control a networked Arduino from your phone, your time has come. A new iPhone application called Ciao just hit the app store last week, and creator Mike Colagrosso was kind enough to send me a copy to check it out.

Ciao uses apple’s network protocol called bonjour to automatically find available serves over the network. So there is no setup from the phone stand point. You just open it, and if there is a ciao enabled arduino (or processing sketch) on the network, it will find it.

From the Arduino standpoint Ciao has an Arduino Library that works with the ethernet library to make this happen. Included are several pretty simple examples, andn if you copy one of them to start from it is pretty easy to get going on something completely custom.

One of the really great things about Ciao is that you define the iPhone UI from the Arduino sketch. So you just say how many and what buttons you need and when you connect to it, they are just there. Currently, from what I could see, it is limited to just buttons, and I would love some other UI elements like sliders, checkboxes and so on, but for the sure simplicity of it all, you can’t beat it.

If you do not have a network enabled Arduino, ciao also has processing libraries that you can use, and then processing can talk to an Ardiuino connected to that computer. This may be Mac only, im not sure. But you may be able to get it to work on a PC if you install bonjour. Controlling a processing sketch is just as easy, and comes with a few examples, like changing the color of the screen depending on the button you press.

Just think… you could control your next robot, fire a paintball gun, unlock a door, or put on a light show, all from iPhone.

And yes, I know you could do a lot of this before with firmata. But seriously, you could not do it this easily.

All together, I think this is a great app, and I really hope it is taken further. Personal request: Please add the ability to send iPhone sensor data to the arduino. Think gyro, accelerometer, audio values.

Ciao for the iPhone

What’s the password? Arduino + Keypad

Thursday, May 5 th , 2011

Keypads are everywhere; on your cellphone, on your TV remotes, on your stereo and now on your Arduino. Wait…. Why do you want a keypad on your Arduino? Well it’s a pretty useful device to input numbers and letters (example: telephones), it can also be used for security measures like a keypad door lock, and it’s prefect when you need a low-cost and accessible interface for your next idea. After all, It wouldn’t be practical to use a single button or a potentiometer to input your Pin on an ATM. So for this tutorial, we will be going over Sparkfun’s 12 buttons keypad (0-9, #, * ), and get you all set up with some code and schematic too.

The buttons on this particular keypad are setup in a 3X4 matrix format so we only need 7 pins to detect the pressing of 12 keys. For example, when you hit the number 3 pins 5&2 are connected, 6 connects pins 5&7 and 9 connects 5&6. So in code we just look for the combination and we know what button is being pressed. So in simple terms, if pins 5 and 2 (of the keypad) are signaling the arduino, it means that button 3 has been pressed – It is setup like this to minimize the number of pins needed to control the keypad.

Hooking it up

So this wiring example looks really confusing, but it’s not. Make sure to look at the full screen version for help, but basically what you have here is that pins 3,5,6 & 7 all all connected through a [[resistor]] to 5[[v]] as well as a [[digital]] pin on the Arduino. The [[resistors]] can range from 1K to 10K [[ohm]] (I’m using 10k) but can not be omitted. After, connect the pins as shown in the diagram. For those of you that are better with words than pictures, I made you a chart.


For this tutorial we have 2 Arduino projects. The first example is pretty simple and just prints out the key that you press in the Arduino Software’s Serial Terminal. The second is just a little more complicated, and allows you to set up a 4 digit secret code. Both of these projects are significantly simplified because the Keypad and Password libraries are doing all the hard work for us.

To make this code work, before you load the code, or even open the Arduino program, we need to place both the “Keypad” AND “Password” folders into your Arduino Library. If you don’t know where that is by default, Look to the right.


If you click the download button to the right of “Arduino” you can download the whole thing as a zip, so you dont need to copy all the files.

Default Library Folder Location

On your Mac:: In (home directory)/Documents/Arduino/libraries
On your PC:: My Documents -> Arduino -> libraries
On your Linux box:: (home directory)/sketchbook/libraries

Note On The Keypad Library

The beauty of this library is the modularity. It can adapt to any keypad, you just need to map the pins with an ohm meter. Once you know which pin is which, simply change the keypad setup code to suit your configuration, and you are good to go. This means you can really interface with any keypad you can get a hold of.

Extending This

This is a basic tutorial, but you could build much more complicated projects with it.

Maybe you could add a passcode to your fancy new coil-gun so that your roommate doesn’t accidentally (insert something bad here). Use it as an input for your large electro-mechanical calculator. Or connect it to 12 different rock launchers so can easily fire them at will. And Yes… if you really must, im sure you could control some LEDs with it.

About The Author

Paul-Arthur Asselin
I’m a young maker, hardware hacker and computer guy. I like to design, draw, code, photoshop. Oh, and I’m french, and 15 years old.

We need your help

bildr is in need of people interested in helping write any sort of blog post for bildr just as Paul-Arthur did here. If you think you would like to help bildr by writing something, supplying code or schematic, or just have an idea for an article you think should be written, please contact us at or check out this page.