Download Print this page

Advertisement

Quick Links

MPR121 Hookup Guide
CONTR IBUTORS:
T O N I _ K
MPR121 Overview
If you are interested in adding the 'magic' of touch to control your
electronics project, a capacitive touch sensor might be the way to go. This
hookup guide will show you how to use the MPR121QR2 sensor.
The MPR121QR2 is a capacitive touch sensor controller that makes it very
easy to integrate capacitive touch sensing into your project. It
communicates via I C, and works by measuring the capacitance of twelve
2
electrode points. When an object comes close to the electrode connector,
the measured capacitance changes. This signals the MPR121 that
something has touched a 'button'. The IC is also capable of driving LEDs or
basic GPIO functionality on electrode pins 4 through 11, giving you a lot of
freedom for setting up your project. The sensor works from 1.6V to 3.3V.
The sensor isn't very current-hungry, drawing only around 29 µA when
sampling every 16 milliseconds.
Materials
To work through this tutorial, you are going to need one of the three
versions of the MPR121 sensor:
• MPR121 Capacitive Touch Sensor Breakout Board
• Touch Shield
• MPR121 Capacitive Touch Keypad
You will also want a soldering iron, some hookup wires and a
microcontroller capable of I C communication. For our examples, we will be
using an Arduino Uno. You will also need some kind of material to act as a
capacitive sensing surface (also known as an electrode, which is not to be
confused with the character Electrode). Generally, aluminum foil works well.
However, you could also use coins, conductive paint, or copper tape.
2
Page 1 of 24

Advertisement

loading
Need help?

Need help?

Do you have a question about the MPR121 and is the answer not in the manual?

Questions and answers

Summary of Contents for sparkfun MPR121

  • Page 1 I C, and works by measuring the capacitance of twelve electrode points. When an object comes close to the electrode connector, the measured capacitance changes. This signals the MPR121 that something has touched a ‘button’. The IC is also capable of driving LEDs or basic GPIO functionality on electrode pins 4 through 11, giving you a lot of freedom for setting up your project.
  • Page 2: Suggested Reading

    Page 2 of 24 Suggested Reading The MPR121 is very easy to get started using, especially with the example code. However, if you haven’t worked with Arduino previously or aren’t familiar with I C communication, you should check out the tutorials below.
  • Page 3 In this first section of the code, the MPR121 library and the Wire library are initialized. The Wire library makes I C communication easy to use on the Arduino. The sketch also defines digital pin 2 as the IRQ pin connection, and creates 12 instances of the boolean variable touchStates.
  • Page 4 Page 4 of 24 void setup(){ pinMode(irqpin, INPUT); digitalWrite(irqpin, HIGH); //enable pullup resistor         Serial.begin(9600);         Wire.begin(); mpr121_setup();     }  The main loop of the code is incredibly simple, as it only calls a single function. void loop(){ readTouchInputs();     }  The function is actually described in the next section of the code. The Arduino requests the electrode states from the sensor in the first section, and the least significant bits and most significant bits are defined for the sensor.
  • Page 5 Page 5 of 24 void readTouchInputs(){ if(!checkInterrupt()){ //read the touch state from the MPR121     Wire.requestFrom(0x5A,2);      byte LSB = Wire.read();     byte MSB = Wire.read();     uint16_t touched = ((MSB << 8) | LSB); //16bits that make  up the touch states for (int i=0; i < 12; i++){  // Check what electrodes wer e pressed if(touched & (1<<i)){ if(touchStates[i] == 0){ //pin i was just touched           Serial.print("pin ");           Serial.print(i);           Serial.println(" was just touched");         }else if(touchStates[i] == 1){ //pin i is still being touched         }            touchStates[i] = 1;             }else{ if(touchStates[i] == 1){           Serial.print("pin ");...
  • Page 6 Page 6 of 24 The last major section of the code defines the threshold values for each electrode. Each electrode must have a touch threshold and a release threshold for the Arduino to compare the current state of the electrode.
  • Page 7 Page 7 of 24 void mpr121_setup(void){ set_register(0x5A, ELE_CFG, 0x00); // Section A ­ Controls filtering when data is > baseline. set_register(0x5A, MHD_R, 0x01); set_register(0x5A, NHD_R, 0x01); set_register(0x5A, NCL_R, 0x00); set_register(0x5A, FDL_R, 0x00); // Section B ­ Controls filtering when data is < baseline. set_register(0x5A, MHD_F, 0x01);...
  • Page 8 Page 8 of 24 set_register(0x5A, ELE10_T, TOU_THRESH); set_register(0x5A, ELE10_R, REL_THRESH); set_register(0x5A, ELE11_T, TOU_THRESH); set_register(0x5A, ELE11_R, REL_THRESH); // Section D // Set the Filter Configuration // Set ESI2 set_register(0x5A, FIL_CFG, 0x04); // Section E // Electrode Configuration // Set ELE_CFG to 0x00 to return to standby mode set_register(0x5A, ELE_CFG, 0x0C);...
  • Page 9 Touch Shield The Touch Shield is an Arduino R3 compatible shield that enables capacitive touch capabilities for your project using the MPR121 IC. The shield itself has 9 touch pads on it (conveniently numbered 1-9 in a 3x3 grid), and has headers for 3 additional electrode connections.
  • Page 10 Shield Tutorial. The shield also has 3 pins labeled ELE9, ELE10, and ELE11. These correspond to electrodes 9,10, and 11 on the MPR121 chip. You can solder additional buttons or connections on to these pins at this time if you want more than the 9 buttons already available on the shield.
  • Page 11 Page 11 of 24 // Match key inputs with electrode numbers #define ONE 8 #define TWO 5 #define THREE 2 #define FOUR 7 #define FIVE 4 #define SIX 1 #define SEVEN 6 #define EIGHT 3 #define NINE 0 //extras (not connected to button) #define ELE9 9 #define ELE10 10 #define ELE11 11...
  • Page 12 The serial bus is started at 9600 bps. Next, the code initializes the I C communication lines without using the Wiring library. The MPR121 chip is then configured with the proper sensitivity settings on the electrodes. The final step in the setup loop creates an interrupt in the code that will trigger when any of the buttons are hit.
  • Page 13 Page 13 of 24 void getNumber() int touchNumber = 0; uint16_t touchstatus; char digits; touchstatus = getTouchStatus(); for (int j=0; j<12; j++) // Check how many electrodes were pressed if ((touchstatus & (1<<j))) touchNumber++; if (touchNumber == 1) if (touchstatus & (1<<SEVEN)) digits = '7';...
  • Page 14 Page 14 of 24 else if (touchstatus & (1<<THREE)) digits = '3'; Serial.println(digits); //do nothing if more than one button is pressed, or if all a re released else if (touchNumber == 0) else The function actually returns at 16-bit value which gives getTouchStatus() the status of each button.
  • Page 15 (touchstatus & (1 << ELE11)) digits = 'C'; Capacitive Touch Keypad The Capacitive Touch Keypad is very similar to the MPR121 breakout board, but instead of having to attach your own electrodes, this board comes with a 12-pin keypad built-in.
  • Page 16 Page 16 of 24 • IRQ → D2 Check out the Fritzing diagram below to verify your connections. This diagram shows the bottom side of the keypad, in order to show you the location of the I C address jumper. In this diagram, the jumper is set to 0, but if you need to have 2 keypads on the same I C bus, you can change one of them to be set to 1.
  • Page 17 The second section of the code is the basic initialization of the serial communication port at 9600 bps, as well as setting the IRQ pin as an input on the Arduino. It also starts the configuraton of the MPR121 IC. void setup() { ...
  • Page 18 Page 18 of 24 The main loop in the code simply scans the MPR121 electrodes and looks for a phone number to be entered on the keypad. This is the function . The code then prints out the phone number dialed over getPhoneNumber() the serial monitor.
  • Page 19 Page 19 of 24 void getPhoneNumber() {  int i = 0; int touchNumber;   Serial.println("Please Enter a phone number..."); while(i<PHONE_DIGITS)   {  while(checkInterrupt())       ;      touchNumber = 0;     touchstatus = mpr121Read(0x01) << 8;     touchstatus |= mpr121Read(0x00); for (int j=0; j<12; j++)  // Check how many electrodes wer e pressed     {  if ((touchstatus & (1<<j)))         touchNumber++;     }  if (touchNumber == 1)     {  if (touchstatus & (1<<STAR))         phoneNumber[i] = '*';...
  • Page 20   data = i2cGetReceivedByte();  // Get MSB result i2cWaitForComplete(); i2cSendStop(); cbi(TWCR, TWEN);  // Disable TWI sbi(TWCR, TWEN);  // Enable TWI return data; }  The function is then defined, which again, steps through mpr121Write() the I C process of writing commands to the MPR121 sensor.
  • Page 21 Page 21 of 24 void mpr121Write(unsigned char address, unsigned char data) i2cSendStart(); i2cWaitForComplete(); i2cSendByte(MPR121_W);// write 0xB4 i2cWaitForComplete(); i2cSendByte(address); // write register address i2cWaitForComplete(); i2cSendByte(data); i2cWaitForComplete(); i2cSendStop(); function is then defined. In this function, all 12 mpr121QuickConfig() of the electrodes are enabled, and the touch and release thresholds for all of the sensors are set.
  • Page 22 Page 22 of 24 void mpr121QuickConfig(void) // Section A // This group controls filtering when data is > baseline. mpr121Write(MHD_R, 0x01); mpr121Write(NHD_R, 0x01); mpr121Write(NCL_R, 0x00); mpr121Write(FDL_R, 0x00); // Section B // This group controls filtering when data is < baseline. mpr121Write(MHD_F, 0x01);...
  • Page 23 Arduino, you can start customizing your project and going further. Resources and Going Further Now that you’ve figured out how to use the various versions of the MPR121 IC, what kind of cool projects can you come up with? If you have any questions still, check out the additional resources below, or feel free to leave a comment on the tutorial.
  • Page 24 All of these tutorials have I C communication. They can also both be used in conjunction with this sensor to act as a display for the button triggers or to monitor the temperature as well. • OpenSegment Hook-Up Guide • TMP006 Hook-Up Guide https://learn.sparkfun.com/tutorials/mpr121-hookup-guide?_ga=1.37706406.725293211.1423760740 2/12/2015...

This manual is also suitable for:

Mpr121qr2