MakeCation 2014 — hardware 2.0

With stable MPR121-PB and capacitive touch pads, I started work on the code for the Arduino Uno.  The Tutorial from the Adafruit website was easy to follow and the downloads  included example code for the Arduino IDE (Adafruit_MPR121_Library-master.zip).  Code below includes modifications to accommodate for four MPR121-PB boards.

/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor

Designed specifically to work with the MPR121 Breakout in the Adafruit shop 
  ----> https://www.adafruit.com/products/

These sensors use I2C communicate, at least 2 pins are required 
to interface

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.  
BSD license, all text above must be included in any redistribution

All modifications made by 505access are in the public domain:
aks      : added capability to accommodate for up to 4 Adafruit
           MPR121 Breakout boards (0x5A, 0x5B, 0x5C and 0x5D);
           boards are searched in that order (i.e. if less than 4
           boards are used, the unused ones start at 0x5D down to
           0x5B).
**********************************************************/

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

#define DEBUG      1

int numBoards = 0; // will set to maximum number of boards found

// You can have up to 4 on one i2c bus
Adafruit_MPR121 cap0 = Adafruit_MPR121();
Adafruit_MPR121 cap1 = Adafruit_MPR121();
Adafruit_MPR121 cap2 = Adafruit_MPR121();
Adafruit_MPR121 cap3 = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched0 = 0;
uint16_t lasttouched1 = 0;
uint16_t lasttouched2 = 0;
uint16_t lasttouched3 = 0;

uint16_t currtouched0 = 0;
uint16_t currtouched1 = 0;
uint16_t currtouched2 = 0;
uint16_t currtouched3 = 0;

void setup() {
  Serial.begin(9600);
  if (DEBUG) {
    Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  }
  
  // Default address is 0x5A,
  // if ADDR tied to 3.3V it's 0x5B
  // If ADDR tied to SDA it's 0x5C and
  // if ADDR tied to SCL then it's 0x5D
  if (cap0.begin(0x5A)) {
    numBoards = 1;
    if (DEBUG) {
      Serial.println ("MPR121(0) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(0) not found, check wiring?");
    }
  }
  if (cap1.begin(0x5B)) {
    numBoards = 2;
    if (DEBUG) {
      Serial.println ("MPR121(1) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(1) not found, check wiring?");
    }
  }
  if (cap2.begin(0x5C)) {
    numBoards = 3;
    if (DEBUG) {
      Serial.println ("MPR121(2) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(2) not found, check wiring?");
    }
  }
  if (cap3.begin(0x5D)) {
    numBoards = 4;
    if (DEBUG) {
      Serial.println ("MPR121(3) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(3) not found, check wiring?");
    }
  }
  if (DEBUG) {
    Serial.print ("finish setup: using ");
    Serial.print (numBoards);
    Serial.println (" MPR121 boards");
  }
}

void loop() {
  // Get the currently touched pads
  currtouched0 = cap0.touched();
  if (numBoards > 1) {
    currtouched1 = cap1.touched();
  }
  if (numBoards > 2) {
    currtouched2 = cap2.touched();
  }
  if (numBoards > 3) {
    currtouched3 = cap3.touched();
  }
  
  for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched0 & _BV(i)) && !(lasttouched0 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i); Serial.println(" touched");
      }
    }
    if ((currtouched1 & _BV(i)) && !(lasttouched1 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+12); Serial.println(" touched");
      }
    }
    if ((currtouched2 & _BV(i)) && !(lasttouched2 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+24); Serial.println(" touched");
      }
    }
    if ((currtouched3 & _BV(i)) && !(lasttouched3 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+36); Serial.println(" touched");
      }
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched0 & _BV(i)) && (lasttouched0 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i); Serial.println(" released");
      }
    }
    if (!(currtouched1 & _BV(i)) && (lasttouched1 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+12); Serial.println(" released");
      }
    }
    if (!(currtouched2 & _BV(i)) && (lasttouched2 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+24); Serial.println(" released");
      }
    }
    if (!(currtouched3 & _BV(i)) && (lasttouched3 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+36); Serial.println(" released");
      }
    }
  }

  // reset our state
  lasttouched0 = currtouched0;
  lasttouched1 = currtouched1;
  lasttouched2 = currtouched2;
  lasttouched3 = currtouched3;
}

At the time I was working on MPR121-PB and touch-pads, I was also working with Panaiotis and Lisa Maynes. Panaiotis created Bandojo — cool music software that allows people to create beautiful music with little to no [music] experience. Lisa is helping Panaiotis expand the capabilities of Bandojo.

Panaiotis and Lisa saw a demo of my MPR121-PB prototype and wanted one immediately — I didn’t have time to make a prettier version for them! So Version 2.0 came quickly thereafter.

In MPR121-PB v2, I alternated the direction of each of the 12-inputs to the four MPR121 breakout boards to make easier (more room) for plugging and unplugging.  In addition to the 12 male plugs for each MPR121, 12 female plugs were added — only one may be used for each of the 12 inputs.  However, it’s now possible to use either male or female connectors (or some combination thereof) for each input.  The power/ground and I2C data/clock were also separated as the connections for each pair appear next to each other on all of the Arduino boards we used.

MPR121 Capacitive Touch Protoboard version 2 (front)

MPR121-PB V2 (front)

MPR121 Capacitive Touch Protoboard version 2 (back)

MPR121-PB V2 (back)

This entry was posted in capacitive touch, Controllers, Custom Designs, Do It Yourself, Electronics, Input Devices, research, Uncategorized, Wired Input devices. Bookmark the permalink.