MPR121 I2C Touch

MPR121 I2C Touch

This page is to Setup and Config MPR121 I2C Touch

Hardware and Software

Hardware

MPR121 I2C Touch, Arduino or Digispark / ESP8266

Software

Arduino IDE

Sanki Notes

    • I2C :

    • SDA : A4 (Digispark D0)

    • SCL : A5 (Digispark D2)

    • IRQ : D2

    • V++ : 3.3V

Examples

This example is ..........

            1. /*********************************************************

            2. This is a library for the MPR121 12-channel Capacitive touch sensor

            3. Designed specifically to work with the MPR121 Breakout in the Adafruit shop

            4. ----> https://www.adafruit.com/products/

            5. These sensors use I2C communicate, at least 2 pins are required

            6. to interface

            7. Adafruit invests time and resources providing this open source code,

            8. please support Adafruit and open-source hardware by purchasing

            9. products from Adafruit!

            10. Written by Limor Fried/Ladyada for Adafruit Industries.

            11. BSD license, all text above must be included in any redistribution

            12. **********************************************************/

            13. #include <Wire.h>

            14. #include "Adafruit_MPR121.h"

            15. // You can have up to 4 on one i2c bus but one is enough for testing!

            16. Adafruit_MPR121 cap = Adafruit_MPR121();

            17. // Keeps track of the last pins touched

            18. // so we know when buttons are 'released'

            19. uint16_t lasttouched = 0;

            20. uint16_t currtouched = 0;

            21. void setup() {

            22. while (!Serial); // needed to keep leonardo/micro from starting too fast!

            23. Serial.begin(9600);

            24. Serial.println("Adafruit MPR121 Capacitive Touch sensor test");

            25. // Default address is 0x5A, if tied to 3.3V its 0x5B

            26. // If tied to SDA its 0x5C and if SCL then 0x5D

            27. if (!cap.begin(0x5A)) {

            28. Serial.println("MPR121 not found, check wiring?");

            29. while (1);

            30. }

            31. Serial.println("MPR121 found!");

            32. }

            33. void loop() {

            34. // Get the currently touched pads

            35. currtouched = cap.touched();

            36. for (uint8_t i=0; i<12; i++) {

            37. // it if *is* touched and *wasnt* touched before, alert!

            38. if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {

            39. Serial.print(i); Serial.println(" touched");

            40. }

            41. // if it *was* touched and now *isnt*, alert!

            42. if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {

            43. Serial.print(i); Serial.println(" released");

            44. }

            45. }

            46. // reset our state

            47. lasttouched = currtouched;

            48. // comment out this line for detailed data from the sensor!

            49. return;

            50. // debugging info, what

            51. Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);

            52. Serial.print("Filt: ");

            53. for (uint8_t i=0; i<12; i++) {

            54. Serial.print(cap.filteredData(i)); Serial.print("\t");

            55. }

            56. Serial.println();

            57. Serial.print("Base: ");

            58. for (uint8_t i=0; i<12; i++) {

            59. Serial.print(cap.baselineData(i)); Serial.print("\t");

            60. }

            61. Serial.println();

            62. // put a delay so it isn't overwhelming

            63. delay(100);

            64. }