Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Tal O
Published © GPL3+

Turn (Almost) Any Surface Into a Touch button with MPR121

Capacitance capabilities at the tip of your finger!

BeginnerFull instructions provided14,246
Turn (Almost) Any Surface Into a Touch button with MPR121

Things used in this project

Story

Read more

Schematics

Schematics

Code

Simple on/off led on pin 0/1

Arduino
It show the option of turn on and off a led that is connected on pin 3.
Using the pin 0 touch of the mpr121 for on and the 1 on release for off.
#include <MPR121.h>
#include <Wire.h>

#define numElectrodes 2

#define LEDPIN 3

void setup()
{
  Serial.begin(115200);
  Serial.println("System started");
  pinMode(LEDPIN, OUTPUT);

  Wire.begin();

  // 0x5C is the MPR121 I2C address on the Bare Touch Board
  if (!MPR121.begin(0x5A)) {
    Serial.println("error setting up MPR121");
    switch (MPR121.getError()) {
      case NO_ERROR:
        Serial.println("no error");
        break;
      case ADDRESS_UNKNOWN:
        Serial.println("incorrect address");
        break;
      case READBACK_FAIL:
        Serial.println("readback failure");
        break;
      case OVERCURRENT_FLAG:
        Serial.println("overcurrent on REXT pin");
        break;
      case OUT_OF_RANGE:
        Serial.println("electrode out of range");
        break;
      case NOT_INITED:
        Serial.println("not initialised");
        break;
      default:
        Serial.println("unknown error");
        break;
    }
    while (1);
  }

  
  MPR121.setInterruptPin(4);
  MPR121.setTouchThreshold(40);
  MPR121.setReleaseThreshold(20);
  MPR121.updateTouchData();
}

void loop()
{
  if (MPR121.touchStatusChanged()) {
    MPR121.updateTouchData();
    for (int i = 0; i < numElectrodes; i++) {
      if (MPR121.isNewTouch(i)) {
        if (i == 0) {
          digitalWrite(LEDPIN, HIGH);
        }

      } else if (MPR121.isNewRelease(i)) {
        if (i == 1) {
          digitalWrite(LEDPIN, LOW);
        }
      }
    }
  }
}

Credits

Tal O
20 projects • 55 followers
Maker @ heart

Comments