MPR121 is a proximity and capacitive touch controller from NXP Semiconductors. Adafruit has this chip on a breakout board that can be interfaced with micro controllers. It has 12 pins that can be utilized as Capacitive touch sensing electrodes. A partial set of these pins can also be used as GPIO pins. This first version of the library is geared towards capacitive touch configuration only.
The hookup to Raspberry Pi is pretty straightforward. Apart from SCL/SDA and the Power/Ground hookups, only other hook up is the IRQ Pin to the GPIO Pin#5 on the Raspberry Pi 2. I am using an Adafruit Pi Cobbler but it can be hooked up directly to the PI. The Capacitive sensing pins are connected to some cardboard wrapped in aluminum foils.
MPR121 board can be interfaced using I2C. Once I2C connection is established, specific registers can be read to get the status of the pins. The touch status for each pin is maintained as a bit. 1 to indicate touch and 0 to indicate no touch. By reading a byte at register 0x00, the touch status of the first 8 pins can be obtained. Five bits on the next register 0x01 indicate the status of the remaining 5 pins. Here is the register map:
Brute force method is to read this register in a loop every few milliseconds to check if they are touched or not. Fortunately MPR121 provides an elegant way of notifying the status. Its has an IRQ pin that can be connected to any GPIO digital input pin on the micro controllers. The MPR121 pulls this pin Low when there is a status change on any of the capacitive pins. So instead of looping to detect change, the micro controller can read the Register only when the IRQ status changes. This library uses the IRQ pin for detecting cap sense change.
The code for this driver can be found at this Github repository.
1. Its a simple Universal Windows App class library. Since it needs to work with GPIO and I2C, added the Windows IoT Extensions for the UWP.
2. The library contains two constructors. The default one and the overloaded constructor to change the I2C address and the IRQ pin.
In order for the consumer hook up to simple Touched and Released events, i have included two events in the MPR121 class and invoke those two events with the appropriate event arguments.In addition i also update a Pins list the represents the continuous state of all the pins on the MPR121.