MPR121 causing Due to hang

Hi there,

I’m trying to run an Adafruit MPR121 Capacitive Touch sensor on a new Arduino Due along with a Adafruit PN532 NFC Shield, WINC1500 WiFi shield and a strange non-stackable pressure map matrix shield.

Everything is working except the MPR121 which we cannot get to work on the Due whatsoever - with or without the other components. I’ve seen few people in 2015 have this problem but have not found an answer.

An Adafruit forum post (Adafruit customer service forums • View topic - MPR121 Touch Sensor Breakout Demo Error) suggested to define _BV which we have tried but to no avail.

They symptom is that whenever the MPR121 enters a function, such as cap.begin() or cap.touched() the program hangs. (As you can see in the screenshot - even when cap.begin() is deleted cap.touched() will still cause a hang.)

The MPR121 has tested find on a Mega2560, I’ve checked the soldering, tried different SCL/SDA pins. If anyone has any ideas around what I could do or what I could test next it would be great.

I’ll upload a photo of the setup (excuse the wiring, it’s hectic due to the pressure matrix) along with the basic code I’ve been using to test with.

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

#ifndef _BV
#define _BV(bit) (1<<(bit))
#endif
    
Adafruit_MPR121 cap = Adafruit_MPR121();

uint16_t lasttouched = 0;
uint16_t currtouched = 0;
unsigned long time;
String left;
String right;
int l = 0;
int r = 0;
int finalise = 0;


void setup() {
  Serial.begin(9600);

  while (!Serial) {
    delay(100);
  }
  
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 

  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  delay(100);
  time = millis();
  currtouched = cap.touched();
  
  for (uint8_t i=0; i<12; i++) {
    
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i))) {
      Serial.print(i); Serial.print(" touched at "); Serial.println(time);

      if (i == 5){
        left = left + " " + time;
        l++;
      }

      else if (i == 7){
        right = right + " " + time;
        r++;
      }
    }
    
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i))) {
      Serial.print(i); Serial.print(" released at "); Serial.println(time);

      if (i == 5){
        left = left + " " + time;
        l++;
      }

      else if (i == 7){
        right = right + " " + time;
        r++;
      }
    }

    if(time > 60000 && finalise == 0){
      
      Serial.println(left);
      Serial.println(right);
      finalise++;
      delay(60000);
    }
  }
      lasttouched = currtouched;
}

Thanks!

A quick review at the Adafruit MPR121 Capacitive Touch sensor schematic doesn't show internal pull ups, therefore using DUE SDA/SCL WITH their builtin pull ups seems OK.

Did you try the example sketch that you can find here ?:

Do not go further in your project until this simple sketch works properly on the DUE.

Plus IMO there is something weird in your sketch at these lines:

#include "TimeLib.h"
#include "time.h"

Edit : if you go to the github page for the Adafruit Library, you will see that they have noted that this Library doesn't work for the SAM3x8e (DUE) !!!! You will have to write your own I2C code.

If its all Adafruit stuff, you could try asking them.

Thanks for pointing that out ard_newbie. I guess we never noticed it when we switched from the Mega to the Due. I’ll look into other sensors, I don’t think I’d be capable of writing a library myself.