MPR121 - 12 Channel Capacitive Touch Sensor Controller Module - I2C Interface
The MPR121 is a 12-channel capacitive touch sensor controller module that communicates with a microcontroller through an I2C interface. This module is ideal for applications requiring multiple touch sensors, such as touch panels, keypads, and buttons.
12 independent touch sensors
I2C interface for communication with microcontrollers
Adjustable sensitivity and filtering
Supports multiple touch detection
Low power consumption
| Pin | Function |
| --- | --- |
| VCC | Power supply (3.3V or 5V) |
| GND | Ground |
| SCL | I2C clock line |
| SDA | I2C data line |
| INT | Interrupt output (optional) |
| ADDR | Address select (optional) |
### Example 1: Basic Touch Sensor Reading with Arduino
This example demonstrates how to read touch sensor data using an Arduino Uno board and the MPR121 module.
```c++
#include <Wire.h>
#define MPR121_ADDR 0x5A // Default I2C address
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
uint16_t touchData = 0;
Wire.beginTransmission(MPR121_ADDR);
Wire.write(0x00); // Register address for touch data
Wire.endTransmission();
Wire.requestFrom(MPR121_ADDR, 2);
touchData = Wire.read() << 8 | Wire.read();
Serial.print("Touch data: 0x");
Serial.println(touchData, HEX);
if (touchData & (1 << 0)) { // Check if touch sensor 0 is touched
Serial.println("Touch sensor 0 is touched!");
}
delay(50);
}
```
### Example 2: Touch Sensor with Interrupt Handling using Raspberry Pi (Python)
This example demonstrates how to read touch sensor data and handle interrupts using a Raspberry Pi and the MPR121 module.
```python
import smbus
import RPi.GPIO as GPIO
# Set up I2C bus and GPIO for interrupt handling
bus = smbus.SMBus(1)
mpr121_addr = 0x5A
GPIO.setmode(GPIO.BCM)
int_pin = 17
GPIO.setup(int_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def read_touch_data():
data = bus.read_i2c_block_data(mpr121_addr, 0x00, 2)
return (data[0] << 8) | data[1]
while True:
if GPIO.input(int_pin) == 0: # Interrupt triggered
touch_data = read_touch_data()
print("Touch data: 0x{:04X}".format(touch_data))
if touch_data & (1 << 0): # Check if touch sensor 0 is touched
print("Touch sensor 0 is touched!")
time.sleep(0.01)
```
### Example 3: Multi-Touch Detection with ESP32 (MicroPython)
This example demonstrates how to detect multiple touch sensors using an ESP32 board and the MPR121 module.
```python
import machine
import utime
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
mpr121_addr = 0x5A
def read_touch_data():
data = i2c.readfrom_mem(mpr121_addr, 0x00, 2)
return (data[0] << 8) | data[1]
while True:
touch_data = read_touch_data()
touches = []
for i in range(12):
if touch_data & (1 << i):
touches.append(i)
if touches:
print("Touches:", touches)
utime.sleep_ms(50)
```
Note: These examples are for illustration purposes only and might require modifications to work with your specific setup. Be sure to check the datasheet and documentation for the MPR121 module and your microcontroller board for more information.