Comments: MPR121 Hookup Guide

Pages

Comments 15 comments

  • I can do use more than one MPR121 in arduino?

  • Naive question: the MPR121 functions as an I2C slave device, and the interrupt triggers the master device (in this case, an Arduino) to either receive data from the MPR121 (e.g. capsense) or write data to (e.g. LED controller), correct?

  • When wiring the mpr121 to the electric imp the response time was a little two slow for my application, it need it to read the touch inputs in real time so I have switched to a different wireless solution. My question is I want to send the touch data to Xively to keep track of the touch states, I understand that the data has to be turned into a string to be read by the API, there plenty of tutorials using temp sensors, etc, how does one transfer touch states into a json data string to be read by Xively? Any help would be grateful.

    • The code scans through each electrode that is initialized and reads the touch state. Then each electrode is assigned a value of either 0 or 1, representing the electrode being touched or not. You would simply need to pull out these values of 0 or 1 corresponding to each electrode and print that to your API to then be transmitted.

      • Ok I’ve tried a couple different ways to extract the data, I think I get caught up in all the code for the mpr, so this is where I am at, I am trying to send data to Xively, I have done all that, and wifi shield cooperates. I am struggling with creating the appropriate sensor instance, after which I understand I must convert these instances into integers that Xively can read, through JSON, my problem is defining that instance, and relating that to the integers of 1 and 0, would I refer it touchStates [i] and does it matter that I have each sensor printing out different information…. Or do I define an instance for each sensor, and transfer that data into a JSON string? (that would be a lot of data) Thanks, I will send you a case a beer……. :)

        • I’m a tad confused. Are you having trouble with the sensor instance in JSON, or just getting the code off of the MPR121 through the Electric Imp?

          • Ok, so I am using the CC3300, and my difficulty is getting the data from MPR121 loop to feed into the Xively, I have broken up each of the sensors into Case instances, similar to the Bildr tutorial, I am posting the code below. (if that helps) much appreciated.

            /*Copyright © 2010 bildr community

            Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

            The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

            THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //

            include <Adafruit_CC3000.h>

            include <ccspi.h>

            include <SPI.h>

            include <string.h>

            include “utility/debug.h”

            include <stdlib.h>

            include “mpr121.h”

            include <Wire.h>

            define ADAFRUIT_CC3000_IRQ 3

            define ADAFRUIT_CC3000_VBAT 5

            define ADAFRUIT_CC3000_CS 10

            Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);

            define WLAN_SSID “” // cannot be longer than 32 characters!

            define WLAN_PASS “”

            // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

            define WLAN_SECURITY WLAN_SEC_WPA2

            define WEBSITE “api.xively.com”

            define API_key “”

            define feedID “”

            //Somewhere in here I need to create an instance to register the mpr121? //possibly could be in the wrong order //have not been able to get the mpr121 to register to the internet yet, I have tried a couple different formats //I have both the Imp April and CC3300 break out //

            Serial.println(F(“\nInitializing…”)); if (!cc3000.begin()) { Serial.println(F(“Couldn’t begin()! Check your wiring?”)); while(1); } int irqpin = 2; // Digital 2 boolean touchStates[12]; //to keep track of the previous touch states

            void setup(){ pinMode(irqpin, INPUT); digitalWrite(irqpin, HIGH); //enable pullup resistor

            Serial.begin(115200); Wire.begin();

            mpr121_setup(); }

            void loop(){ readTouchInputs(); }

            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 were 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");
            
                  switch (i) {
                    case 0:
                     // 0 was just touched
                     Serial.print("");
                     Serial.print("");
                     break;
                     case 1:
                     // 1 was touched
                     Serial.print("Did you Wash your Hands! GROSS"); 
                     break;
                     case 2:
                     // 2 was touched
                     Serial.print(""); 
                     Serial.print("");
                     break;
                     case 3:
                     // 3 was touched
                     Serial.print(" ");
                     Serial.print(" ");
                     break; 
                     case 4:
                     // 4 was touched 
                     Serial.print(" ");
                     Serial.print(" ");
                     break;
                     case 5:
                     // 5 was just touched
                     Serial.print(" ");
                     break;
                     case 6: 
                     // 6 was just touched
                     Serial.print(" ");
                     break;
                     case 7:
                     // 7 was just touched
                     Serial.print(" ");
                     break;
                     case 8:
                     // 8 was just touched
                     Serial.print(" ");
                     break;
                     case 9:
                     // 9 was pressed 
                     Serial.print(" ");
                     break;
                     case 10:
                     // 10 was just pressed 
                     Serial.print(" ");
                     break;
                     case 11:
                     // 11 was just touched
                     Serial.print(" "); 
            
            
                  }           
            
                }else if(touchStates[i] == 1){
                  //pin i is still being touched
                }  
            
                touchStates[i] = 1;      
              }else{
                if(touchStates[i] == 1){
                  Serial.print(" pin ");
                  Serial.print(i);
                  Serial.println(" Shelf is no longer being touched");
            
                  //pin i is no longer being touched
               }
            
                touchStates[i] = 0;
              }
            
            }
            

            } }

            void mpr121_setup(void){

            set_register(0x5A, ELE_CFG, 0x00);

          • Looking at using the WiFly or something similar. Like the CC3300. Either way I am stuck on register a touch being pushed to Xively and shield picking up the touches, not the mpr121 yet the wifi module itself. If that clarifies. Thanks again….

          • Both actually. Looking over my previous reply that is a little confusing.

      • Awesome thanks!

  • Curious on how to serial print info for different sensors using the breakout, I followed the example, and have my breakout functioning fine. At first the sensor 11 acted up, yet after I disconnected the USB and re-plugged it works fine. I have been able to get the proximity settings to work as well. I basically want different information to be displayed based on the sensor pressed. Is that just building on the serial print function? In reviewing the data sheet I understand a person could make the senors less or more sensitive, trying to figure out exactly the numbers to put in to make it go up in sensitivity and down in sensitivity. I’m using the Bare Conductive Paint, as my electrodes, and it works great. I eventually want to connect this project to the electric imp…. Any step in the right direction would be awesome. A nudge in the right direction is what I’m looking for, the way to learn is by doing.

    Peace..

    • You’ve got it. You would simply set up a basic if statement in your code, saying if sensorA is pressed, Serial.print(info you want displayed). If you need more of a nudge than that, let us know :)

      • Thanks!

        Any idea about connecting an imp to one of these. More concerned about the pin layout to create a connection. Thanks for the prompt response.

        • The Imp has I2C connections available, so it would be just like hooking this up to an Arduino’s I2C connections. Check out our tutorial on the Imp for more info on that here.


If you've found a bug or have other constructive feedback for our tutorial authors, please send us your feedback!