Capacitive Touch Sensor Code Explanation

Code Breakdown

Including Libraries and Definitions

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

These lines include the necessary libraries for the program:

  • Wire.h is used for I2C communication, which is required for communicating with the MPR121 chip.
  • String.h is included to use the String class for easier string manipulation.
  • "Adafruit_MPR121.h" is the library specifically for the Adafruit MPR121 capacitive touch sensor.

Initialization

Adafruit_MPR121 cap = Adafruit_MPR121();
const int buttonPin = 6;

Here we initialize an instance of the Adafruit_MPR121 class to interact with the touch sensor and define buttonPin as 6, which is the digital pin on the Arduino that will be used to read button presses

Gesture Array

String gestures[] = {
  "Top_Left_Quadrant_Touch",
  "Top_Right_Quadrant_Touch",
  "Bottom_Left_Quadrant_Touch",
  "Bottom_Right_Quadrant_Touch",
  "Center_Touch"
};

gestures is an array of strings representing the names of different touch gestures detected by the sensor.

State Variables

int gestureIndex = 0;
bool isGestureBeingPerformed = false;

These are state variables to keep track of the current gesture index and whether a gesture is currently being performed.

Setup Function

void setup() {
  Serial.begin(19200);
  while (!Serial) {
    delay(10);
  }
  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!");
  pinMode(buttonPin, INPUT_PULLUP);
}

The setup function initializes the serial communication, checks for the presence of the MPR121 sensor, and configures the buttonPin as an input with an internal pull-up resistor.

Main Loop

void loop() {
  // Check button state
  if (digitalRead(buttonPin) == LOW) {
    isGestureBeingPerformed = true;
  } else if (isGestureBeingPerformed) {
    isGestureBeingPerformed = false;
    gestureIndex = (gestureIndex + 1) % 5; // to the next gesture when the button is released
  }

In the loop function, we read the state of the button. If the button is pressed (logic LOW), we start tracking a gesture. When the button is released, we increment the gestureIndex to cycle through the gesture states.

Sensor Data Collection

  String dataString = "";
  for (uint8_t j = 0; j < 12; j++) {
    int baseline = cap.baselineData(j);
    int filtered = cap.filteredData(j);
    int delta = filtered - baseline;
    dataString += "[" + String(baseline) + "," + String(filtered) + "," + String(delta) + "]";
    if (j < 11) {
      dataString += " ";
    }
  }

This loop collects the baseline and filtered data from the capacitive touch sensor for each electrode (0 to 11) and calculates the delta, which is the difference between the filtered data and the baseline.

Gesture Data Append

  if (isGestureBeingPerformed) {
    dataString += " Gesture: " + gestures[gestureIndex];
  } else {
    dataString += " Gesture: No Gesture";
  }
  Serial.println(dataString);
  delay(5);
}

Finally, we append the current gesture information to dataString and print it to the serial monitor. A short delay is added to stabilize the output.