MPR121 capacitive touch + Photon help

Hello!

I am just starting to work with the Particle photon, It’s been great so far.
However, I do not seem to be able to get my MPR121 breakout board to work with my Photon.

I have written code for the MPR121 in Arduino IDE before, but even using the MPR121 library, it wont even find the board. :confused: I cannot seem to find out what I do wrong. Below I have added the code I used in Arduino IDE (which works on the Arduino Uno) — is there a good way to port this?
I have used the following hookup;

This is the code I used for my arduino uno

#include <Wire.h>
#include "Adafruit_MPR121.h"

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels? How many NeoPixels are there?
#define led            6
#define NUMPIXELS      1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, led, NEO_GRB + NEO_KHZ800);

void setup() {
  while (!Serial);        // needed to keep leonardo/micro from starting too fast!

  Serial.begin(9600);
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  
  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");


  strip.begin();
  strip.show();
}

void loop() {
// Get the currently touched pads
  currtouched = cap.touched();
  lasttouched = currtouched;

// LED
//dubbel
  if ( 
    (cap.touched() & (1 << 0))        // + pin 0 red
    && 
    (cap.touched() & (1 << 1))        // + pin 1 yellow
     && 
    (! (cap.touched() & (1 << 2)))    // - pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Serial.println("0+1");
     strip.setPixelColor(0, strip.Color(255, 127, 0));
     strip.show();
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (cap.touched() & (1 << 1))        // + pin 1 yellow
     && 
    (cap.touched() & (1 << 2))        // + pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Serial.println("1+2");
     strip.setPixelColor(0, strip.Color(0, 255, 0));
     strip.show();
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (! (cap.touched() & (1 << 1)))    // - pin 1 yellow
     && 
    (cap.touched() & (1 << 2))        // + pin 2 cyan
     && 
    (cap.touched() & (1 << 3))        // + pin 3 purple
    ){ 
     Serial.println("2+3");
     strip.setPixelColor(0, strip.Color(0, 0, 255));
     strip.show();
  }

// ENKEL
  else if ( 
    (cap.touched() & (1 << 0))        // + pin 0 red
    && 
    (! (cap.touched() & (1 << 1)))    // - pin 1 yellow
     && 
    (! (cap.touched() & (1 << 2)))    // - pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Serial.println("0");
     strip.setPixelColor(0, strip.Color(255, 0, 0));
     strip.show();
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (cap.touched() & (1 << 1))        // + pin 1 yellow
     && 
    (! (cap.touched() & (1 << 2)))    // - pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Serial.println("1");
     strip.setPixelColor(0, strip.Color(255, 255, 0));
     strip.show();
  }

  else if ( 
(! (cap.touched() & (1 << 0)))    // - pin 0 red
&& 
(! (cap.touched() & (1 << 1)))    // - pin 1 yellow
 && 
(cap.touched() & (1 << 2))        // + pin 2 cyan
 && 
(! (cap.touched() & (1 << 3)))    // - pin 3 purple
){ 
 Serial.println("2");
 strip.setPixelColor(0, strip.Color(0, 255, 255));
 strip.show();
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (! (cap.touched() & (1 << 1)))    // - pin 1 yellow
 && 
(! (cap.touched() & (1 << 2)))    // - pin 2 cyan
 && 
(cap.touched() & (1 << 3))        // + pin 3 purple
){ 
 Serial.println("3");
 strip.setPixelColor(0, strip.Color(127, 0, 255));
 strip.show();
  }

  else {
  strip.setPixelColor(0, strip.Color(0, 0, 0));
  strip.show();
  }
}

The code I’ve messed with inside Particle IDE

#include "application.h"
#include "Adafruit_MPR121.h"

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;


void setup() {
  while (!Serial);        // needed to keep leonardo/micro from starting too fast!

  Serial.begin(9600);
  Particle.publish("Adafruit MPR121 Capacitive Touch sensor test"); 

  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
  Particle.publish("MPR121 not found, check wiring?");
    while (1);
  }
  Particle.publish("MPR121 found!");
    }
    void loop() {
    // Get the currently touched pads
      currtouched = cap.touched();
      lasttouched = currtouched;

// LED
      if ( 
    (cap.touched() & (1 << 0))        // + pin 0 red
    && 
    (cap.touched() & (1 << 1))        // + pin 1 yellow
     && 
(! (cap.touched() & (1 << 2)))    // - pin 2 cyan
 && 
(! (cap.touched() & (1 << 3)))    // - pin 3 purple
){ 
 Particle.publish("0+1");
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (cap.touched() & (1 << 1))        // + pin 1 yellow
     && 
    (cap.touched() & (1 << 2))        // + pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Serial.println("1+2");
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (! (cap.touched() & (1 << 1)))    // - pin 1 yellow
     && 
    (cap.touched() & (1 << 2))        // + pin 2 cyan
     && 
    (cap.touched() & (1 << 3))        // + pin 3 purple
    ){ 
     Particle.publish("2+3");
  }

// ENKEL
  else if ( 
    (cap.touched() & (1 << 0))        // + pin 0 red
    && 
    (! (cap.touched() & (1 << 1)))    // - pin 1 yellow
     && 
    (! (cap.touched() & (1 << 2)))    // - pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Particle.publish("0");
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (cap.touched() & (1 << 1))        // + pin 1 yellow
     && 
    (! (cap.touched() & (1 << 2)))    // - pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Particle.publish("1");
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (! (cap.touched() & (1 << 1)))    // - pin 1 yellow
     && 
    (cap.touched() & (1 << 2))        // + pin 2 cyan
     && 
    (! (cap.touched() & (1 << 3)))    // - pin 3 purple
    ){ 
     Particle.publish("2");
  }

  else if ( 
    (! (cap.touched() & (1 << 0)))    // - pin 0 red
    && 
    (! (cap.touched() & (1 << 1)))    // - pin 1 yellow
     && 
    (! (cap.touched() & (1 << 2)))    // - pin 2 cyan
     && 
    (cap.touched() & (1 << 3))        // + pin 3 purple
    ){ 
     Particle.publish("3");
  }

  else {
     Particle.publish("Niet gedrukt");
  }
}

You got an Arduino fritzing but we’d need to know how you’ve wired the board to your Photon :wink:

Hi RWB! Thank you for your reply. I have tried this hookup and code, but no success so far :confused:

Hello @ScruffR ! Thank you for your reply. My bad for forgetting that! I have added it below;

I2C is located on D0/D1 so SDA should go to D0 and SCL to D1

You are using the SPI pins, but then it would be A3 for CLK

2 Likes

Thanks so much! That worked :smile:

1 Like

Hello NielsD, can you help with the code and diagrams that use with this captive touch, im new with that and wanna know if you can help me!