MPR121 I2C Capacitive Touch Sensor
Touch sensors are used in many embedded devices. The Freescale MPR121 is a second generation capacitive touch sensor controller with 12 capacitive sensing inputs. It has an I2C interface. Sparkfun recently introduced two breakout boards that use the MPR121. Sparkfun is currently out of stock on these devices and may not restock them, there are identical very low cost clones, search for "touch keypad breakout".
Sparkfun MPR121 Capacitive Touch Keypad
The Sparkfun Capacitive Touch Keypad breakout includes 12 keys and a MPR121 mounted on the back of the PCB. Header pins can be soldered to the board for use on a breadboard.
MPR121 Capacitive Touch Sensor Breakout Board
With the breakout board you can make slider wheels and slide bars with a custom designed electrode pattern or have up to 20 touch buttons in a matrix pattern.
A typical slide wheel and slide bar electrode pattern using 10 channels
More information can be found in the data sheet and application notes from Freescale.
MPR121 Datasheet
Application Notes
Here is a short video showing the touch keypad running on mbed. As each key is hit, note that the binary number for the key (1..12) appears in the LEDs. As each key is released the LEDs return to a value of 0 (i.e., no key hit).
The demo code for the mbed LPC1768 seen in the touch keypad video is based on C++ code posted earlier in the forum at http://mbed.org/forum/electronics/topic/1873/ by Anthony Buckton that can be found at http://mbed.org/users/abuckton/programs/MPR121/lnag0q.
A minor change was made to his demo code to output the key code to the LEDs for use in the video. After a somewhat involved initial power-on configuration sequence, each key hit or release generates an interrupt. By reading two 8-bit I2C registers, you can obtain the status of the 12-keys. Each key is a bit in a register. The interrupt routine reads the registers and outputs the keycode (1..12) to the LEDs in binary. All LEDs off indicates no key hit (not key 0).
Import programMPR121_Demo
MPR121 demo code for 12 key Sparkfun touch keypad - sends key number to LEDs. Based on Sparkfun MPR121 code ported to mbed by Anthony Buckton
The existing demo code is based on a Sparkfun example and it prints some test data to the PC's USB virtual com port (not seen in video). Once you have the touch pad working, you will probably want to delete that extra printf portion of the demo code (the other initialization code is still needed). Two additional files are used in the project, mpr121.cpp and mpr121.h. Here is the minimal demo code with the debug printfs removed:
Touch_keypad_demo_code
#include <mbed.h> #include <mpr121.h> DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); // Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); // Setup the i2c bus on pins 9 and 10 I2C i2c(p9, p10); // Setup the Mpr121: // constructor(i2c object, i2c address of the mpr121) Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); // Key hit/release interrupt routine void fallInterrupt() { int key_code=0; int i=0; int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; // LED demo mod i=0; // puts key number out to LEDs for demo for (i=0; i<12; i++) { if (((value>>i)&0x01)==1) key_code=i+1; } led4=key_code & 0x01; led3=(key_code>>1) & 0x01; led2=(key_code>>2) & 0x01; led1=(key_code>>3) & 0x01; } int main() { interrupt.fall(&fallInterrupt); interrupt.mode(PullUp); while (1) {} }
After soldering header pins to the touch keypad, some small plastic or felt sticky pads on the back side of the PCB about the same thickness as the black plastic on the header pins helps quite a bit to stabilize the touch keypad on top of a breadboard. It can also be attached using the screw holes provided.
Bottom of Touch Keypad
Wiring
Here are the connections for the demo code:
mbed | Touch Keypad | Pullup Resistors |
---|---|---|
Gnd | Gnd | |
p9 | SDA | 4.7K ohm - from p9/SDA to Vout (not in series!) |
p10 | SCL | 4.7K ohm - from p10/SCL to Vout (not in series!) |
p26 | IRQ | |
Vout(3.3V) | Vcc |
Warning!
The required I2C pullups on SDA and SCL are not provided on the touch keypad board, so add two 4.7K ohm pullups from the SDA and SCL (I2C signal lines) to 3.3V to the circuit. The other breakout board with the MPR121 only, has the pullups on the board. Without pullups, the I2C APIs will hang. Some I2C breakout boards have the pullup resistors on the board, but this one does not. In general, when using a new I2C board you would need to double check the schematic and look for the pullups. When several I2C boards are connected on a single I2C bus, they should only have one set of pullups.
11 comments on MPR121 I2C Capacitive Touch Sensor:
Please log in to post comments.
Hello there
I have modified your code a bit and in the fall_Interrupt I am lighting up a strip of LEDs ( therefore it takes about 2-3 seconds for the completion of the task). If u just tap, or release the electrode before completion, interrupts stop working whatsoever. I think it has to do with the fact that releasing your finger causes another interrupt and since the old interrupt is already running, it blocks any futher ones until restarting the whole MBED.
I am looking for a solution to the problem, and the only one I can think of is making a small ( wait(2) ) delay b/n the possibility of an interrupt being called. This should be done somewhere in the MPR121 files, I think, but I am not sure where at all..