How to Use the MPR121 Capacitive Touch Sensor with Arduino

How to Use the MPR121 Capacitive Touch Sensor with Arduino

The MPR121 is a capacitive touch sensor controller that can detect up to 12 touch-sensitive electrodes. It’s commonly used in touch-sensitive interfaces, musical instruments, and other interactive projects. In this tutorial, we’ll show you how to connect and use the MPR121 with Arduino to create touch-based applications.


What You Will Need

  1. MPR121 Touch Sensor Module
  2. Arduino Board (e.g., Uno, Mega, Nano)
  3. Breadboard and Jumper Wires
  4. A computer with the Arduino IDE installed

Step 1: Understanding the MPR121 Module

The MPR121 uses I2C communication to interface with a microcontroller. It has 12 touch-sensitive inputs that can detect capacitive changes when a conductive object (e.g., a finger) is near the electrodes.

MPR121 Pinout

Pin Function
VCC Power Supply (3.3V/5V)
GND Ground
SDA I2C Data Line
SCL I2C Clock Line
IRQ Interrupt Output (optional)

Step 2: Wiring the MPR121 to Arduino

Here’s how to connect the MPR121 to an Arduino Uno:

MPR121 Pin Arduino Pin
VCC 5V
GND GND
SDA A4 (SDA)
SCL A5 (SCL)
IRQ (optional) Not connected

Note: For other Arduino boards, verify the I2C pins in your board’s documentation.


Step 3: Install the Required Library

The Adafruit MPR121 library simplifies working with the sensor.

Steps to Install:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Search for "Adafruit MPR121" and click Install.

Step 4: Upload the Code

Here’s an example sketch to detect touch inputs from the MPR121:

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

Adafruit_MPR121 mpr121 = Adafruit_MPR121();

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing MPR121...");

  if (!mpr121.begin(0x5A)) { // Default I2C address is 0x5A
    Serial.println("MPR121 not found. Check connections.");
    while (1);
  }

  Serial.println("MPR121 initialized.");
}

void loop() {
  // Read touch status
  uint16_t touchStatus = mpr121.touched();

  for (uint8_t i = 0; i < 12; i++) {
    if (touchStatus & (1 << i)) {
      Serial.print("Electrode ");
      Serial.print(i);
      Serial.println(" touched.");
    }
  }

  delay(100); // Update every 100ms
}

Step 5: Test the Setup

  1. Connect the Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port under the Tools menu.
  3. Upload the code to the Arduino by clicking Upload.
  4. Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to 9600.
  5. Touch the electrodes on the MPR121 module, and observe the Serial Monitor for detected touch inputs.

Optional: Using the IRQ Pin for Interrupts

The IRQ pin can be used to trigger an interrupt when a touch is detected, reducing the need to constantly poll the sensor.

  1. Connect the IRQ pin of the MPR121 to a digital pin on the Arduino (e.g., Pin 2).
  2. Modify the code to attach an interrupt and handle touch detection in the interrupt service routine (ISR).

Applications of the MPR121

  1. Capacitive touch interfaces
  2. Musical instruments
  3. Interactive art installations
  4. Gesture-based controls

Troubleshooting

  • No response from the sensor: Verify the I2C connections and address.
  • Inconsistent touch detection: Ensure the electrodes are clean and properly connected.
  • Library errors: Confirm that the Adafruit MPR121 library is installed.

Conclusion

You’ve successfully interfaced the MPR121 capacitive touch sensor with Arduino. This versatile sensor opens up possibilities for creating interactive and touch-sensitive projects. Experiment with different electrode designs and configurations to expand its functionality!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.