Skip to main content

Grove - 12 Key Capacitive I2C Touch Sensor V2 (MPR121)

The Grove - 12 Key Capacitive I2C Touch Sensor V2 (MPR121) is a multichannel proximity capacitive touch sensor. It's a 3-in-1 module with the following features: Capacitance Sensing, Touch Sensing, and Proximity Sensing.

Capacitance Sensing: This module uses a constant DC current capacitance sensing scheme. It can measure capacitances ranging from 10 pF to over 2000 pF with a resolution up to 0.01 pF.

Touch Sensing: Once the electrode capacitance data is acquired, the electrode touch/release status is determined comparing it to the capacitance baseline value.

Proximity Sensing: One new feature of the MPR121 is the near proximity sensing system. This means that all of the system’s electrodes can be summed together to create a single large electrode.

Based on Freescale MPR121, this sensor have 12 completely independent electrodes with build-in autoconfiguration. Thanks to I2C interface, you can detect all the 12 electrodes signals with only one Grove port, and the I2C address is hardware configurable, from 0X5B to 0X5D. This also makes it possible for multiple Grove - 12 Key Capacitive I2C Touch Sensor V2 (MPR121) to be used together for channel expansions in a single system, you can build a touch system which contains max. 36 electrodes.

This sensor is an upgrade version of Grove - I2C Touch Sensor, to meet Matsuzawa.Takashi's(one of our customer) needs, we make the I2C address changeable, and even cheaper than the old version. So if you have any suggestions about all the Groves, please just shoot to us. We will always listen to your voice, it may make another upgrade, even a new grove. Please kindly write your suggestions in the Grove 100+ page.

Version Change

ItemGrove - 12 Key Capacitive I2C Touch Sensor V2Grove - I2C Touch Sensor
Main ChipMPR121MPR121
I2C Addresschangeable(0X5B ~ 0X5D)unmodifiable(0X5A)
Touch Sensor Feelerx
Input Interfacealligator interfaceDIP 2Pin Female Header
Cost PerformanceHighLow
Release TimeSeptember 11-2018October 31-2015

Features

  • Internal 10-bit ADC
  • Integrated independent autocalibration for each electrode input
  • Completely independent electrodes with built-in autoconfiguration
  • I2C interface, with IRQ Interrupt output to advise electrode status changes
  • Hardware configurable I2C address
  • 12 electrodes/capacitance sensing inputs in which 8 are multifunctional for LED driving and GPIO
  • Autoconfiguration of charge current and charge time for each electrode input
  • Separate touch and release trip thresholds for each electrode, providing hysteresis and electrode independence

Specification

ItemValue
Operating voltage3.3V / 5V
Operating temperature-40°C to +85°C
Storage Temperature Range-40°C to +125°C
Capacitances ranging10 pF to over 2000 pF
Resolution0.01 pF
GPIO Source Current per Pin12 mA
GPIO Sink Current per Pin1.2 mA
InterfaceI2C
I2C addrss range0x5B,0x5C,0x5D
Default I2C address0x5B

Applications

  • PC Peripherals
  • MP3 Players
  • Remote Controls
  • Mobile Phones
  • Lighting Controls

Hardware Overview

Pin Map

Pin NumberPin NameFunctionPin multiplexing
8CH0Channel0, Electrode 0, input the capacitance valnue-
9CH1Channel1, Electrode 1, input the capacitance valnue-
10CH2Channel2, Electrode 2, input the capacitance valnue-
11CH3Channel3, Electrode 3, input the capacitance valnue-
12CH4Channel4, Electrode 4, input the capacitance valnueGPIO or LED driver
13CH5Channel5, Electrode 5, input the capacitance valnueGPIO or LED driver
14CH6Channel6, Electrode 6, input the capacitance valnueGPIO or LED driver
15CH7Channel7, Electrode 7, input the capacitance valnueGPIO or LED driver
16CH8Channel8, Electrode 8, input the capacitance valnueGPIO or LED driver
17CH9Channel9, Electrode 9, input the capacitance valnueGPIO or LED driver
18CH10Channel10, Electrode 10, input the capacitance valnueGPIO or LED driver
19CH11Channel11, Electrode 11, input the capacitance valnueGPIO or LED driver
tip

For the CH0 ~ CH11, once the electrode capacitance data is acquired, the electrode touch/release status is determined comparing it to the capacitance baseline value. And you can set the baseline value for each channel separately. The Pin12 ~ Pin19 is multifunctional, which means you can configure them as GPIO or LED driver, for more detail, please refer to the freescale application note AN3894.

Danger

The central pad is connected to the address wire, you can change the I2C address by cutting the wire and re-welding it. For the safety of you and others, please be careful with knife or welding gun you may use.

Schemaitc

Power

The operation voltage of Freescale MPR121 is 1.71V to 3.6V, however, the voltage of Arduino is 3.3V or 5V. In order to make it compaticable with 5V system, we use a voltage conversion chip to provide 3.3V for the Freescale MPR121.

Bi-directional level shifter circuit

This is a typical Bi-directional level shifter circuit to connect two different voltage section of an I2C bus. The I2C bus of this sensor use 3.3V, if the I2C bus of the Arduino use 5V, this circuit will be needed. In the schematic above, Q1 and Q2 are N-Channel MOSFET 2N7002A, which act as a bidirectional switch. In order to better understand this part, you can refer to the AN10441

Platforms Supported

ArduinoRaspberry Pi
caution

The platforms mentioned above as supported is/are an indication of the module's software or theoritical compatibility. We only provide software library or code examples for Arduino platform in most cases. It is not possible to provide software library / demo code for all possible MCU platforms. Hence, users have to write their own software library.

Getting Started

Play With Arduino

In this part, we will show you how to use the Grove - 12 Key Capacitive I2C Touch Sensor V2 (MPR121) as a touch sensor, as for how to configure it as a Capacitance Sensor or Proximity Sensor, please check the Datasheet.

Hardware

Materials required

Seeeduino V4.2Base ShieldI2C Touch Sensor V2
enter image description hereenter image description hereenter image description here
Get One NowGet One NowGet One Now
note

1 Please plug the USB cable gently, otherwise you may damage the port. Please use the USB cable with 4 wires inside, the 2 wires cable can't transfer data. If you are not sure about the wire you have, you can click here to buy

2 Each Grove module comes with a Grove cable when you buy. In case you lose the Grove cable, you can click here to buy.

  • Step 1. Connect the Grove - 12 Key Capacitive I2C Touch Sensor V2 (MPR121) to the I2C port of the Base Shield.

  • Step 2. Plug Grove - Base Shield into Seeeduino.

  • Step 3. Connect Seeeduino to PC via a USB cable.

note

If we don't have Grove Base Shield, We also can directly connect this module to Seeeduino as below.

SeeeduinoGrove-MPR121
5VRed
GNDBlack
SDAWhite
SCLYellow

Software

note

If this is the first time you work with Arduino, we strongly recommend you to see Getting Started with Arduino before the start.

  • Step 1. Download the Grove touch sensor MPR121 Library from Github.

  • Step 2. Refer to How to install library to install library for Arduino.

  • Step 3. Restart the Arduino IDE. Open the example, you can open it in the following three ways:

    1. Open it directly in the Arduino IDE via the path: File --> Examples --> Grove touch sensor MPR121 --> MPR121_demo.

    2. Open it in your computer by click the MPR121_demo.ino which you can find in the xxxx\Arduino\libraries\Grove_touch_sensor_MPR121-master, XXXX is the location you installed the Arduino IDE.

    3. Or, you can just click the icon in upper right corner of the code block to copy the following code into a new sketch in the Arduino IDE.

#include "Seeed_MPR121_driver.h"

Mpr121 mpr121;

u16 touch_status_flag[CHANNEL_NUM]={0};
void setup()
{
s32 ret=0;
Serial.begin(115200);
if(mpr121.begin()<0)
{
Serial.println("Can't detect device!!!!");
}
else
{
Serial.println("mpr121 init OK!");
}
delay(100);
}
void loop()
{
u16 result=0;
u16 filtered_data_buf[CHANNEL_NUM]={0};
u8 baseline_buf[CHANNEL_NUM]={0};

result=mpr121.check_status_register();

mpr121.get_filtered_reg_data(&result,filtered_data_buf);

for(int i=0;i<CHANNEL_NUM;i++)
{
if(result&(1<<i)) /*key i is pressed!!*/
{
if(0==touch_status_flag[i])
{
touch_status_flag[i]=1;
Serial.print("key ");
Serial.print(i);
Serial.println("pressed");
}
}
else
{
if(1==touch_status_flag[i])
{
touch_status_flag[i]=0;
Serial.print("key ");
Serial.print(i);
Serial.println("release");
}
}
}
delay(50);
}
  • Step 4. Upload the demo. If you do not know how to upload the code, please check How to upload code.

  • Step 5. Open the Serial Monitor of Arduino IDE by click Tool-> Serial Monitor. Or tap the ctrl+shift+m key at the same time. Set the baud rate to 115200.

success

If everything goes well, you will get the result. When you touch the CH0 ~ CH11 pads, it will trigger key ?pressed and key ?release

mpr121 inmpr121 init OK!
key 11pressed
key 11release
key 10pressed
key 10release
key 0pressed
key 0release
key 2pressed
key 2release

Play With Raspberry Pi

Hardware

  • Step 1. Things used in this project:
Raspberry piGrove Base Hat for RasPiI2C Touch Sensor V2
enter image description hereenter image description hereenter image description here
Get ONE NowGet ONE NowGet ONE Now
  • Step 2. Plug the Grove Base Hat into Raspberry.
  • Step 3. Connect the Grove - 12 Key Capacitive I2C Touch Sensor V2 (MPR121) to the I2C port of the Base Hat.
  • Step 4. Connect the Raspberry Pi to PC through USB cable.

Software

caution

If you are using Raspberry Pi with Raspberrypi OS >= Bullseye, you have to use this command line only with Python3.

  • Step 1. Follow Setting Software to configure the development environment.
  • Step 2. Download the source file by cloning the grove.py library.
cd ~
git clone https://github.com/Seeed-Studio/grove.py

  • Step 3. Excute below command to run the code.
cd grove.py/grove
python3 grove_12_chan_touch_sensor_MPR121.py

Following is the grove_12_chan_touch_sensor_MPR121.py code.

import time
from grove.i2c import Bus


TOUCH_SENSOR_DEFAULT_ADDR = 0x5b

MODE_CONFIG_REG_ADDR = 0x5e
GLOBAL_PARAM_REG_ADDR_L = 0x5c
TOUCH_STATUS_REG_ADDR_L = 0x00
SET_DEBOUNCE_REG_ADDR = 0x5b

FILTERED_DATA_REG_START_ADDR_L = 0x04
CHANNEL_NUM = 12

STOP_MODE = 0
NORMAL_MODE = 0x3c

class TouchSensorMpr121():
def __init__(self,bus_num = 1,addr = TOUCH_SENSOR_DEFAULT_ADDR):
self.bus = Bus(bus_num)
self.addr = addr
self.threshold = 0
self.touch_flag = [0]*CHANNEL_NUM

def sensor_init(self):
self._set_mode(STOP_MODE)
data = [0x23,0x10]
self._set_global_param(data)
self._set_debounce(0x22)
self._set_mode(NORMAL_MODE)

def set_threshold(self,threshold):
self.threshold = threshold

def wait_for_ready(self):
time.sleep(.2)

def _set_mode(self,mode):
self.bus.write_byte_data(self.addr,MODE_CONFIG_REG_ADDR,mode)

def _set_global_param(self,data):
self.bus.write_i2c_block_data(self.addr,GLOBAL_PARAM_REG_ADDR_L,data)

def _set_debounce(self,data):
self.bus.write_byte_data(self.addr,SET_DEBOUNCE_REG_ADDR,data)

def _check_status_register(self):
data_status = self.bus.read_i2c_block_data(self.addr,TOUCH_STATUS_REG_ADDR_L,2)
return data_status

def get_filtered_touch_data(self,sensor_status):
result_value = []
for i in range(CHANNEL_NUM):
time.sleep(.01)
if(sensor_status & (1<<i)):
channel_data = self.bus.read_i2c_block_data(self.addr,FILTERED_DATA_REG_START_ADDR_L+2*i,2)
result_value.append(channel_data[0] | channel_data[1]<<8 )
else:
result_value.append(0)
return result_value

def listen_sensor_status(self):
data = self._check_status_register()
touch_status = data[0] | (data[1]<<8)
touch_result_value = self.get_filtered_touch_data(touch_status)

for i in range(CHANNEL_NUM):
if(touch_result_value[i] < self.threshold ):
touch_result_value[i] = 0
return touch_result_value

def parse_and_print_result(self,result):
for i in range(CHANNEL_NUM):
if(result[i] != 0):
if(0 == self.touch_flag[i]):
self.touch_flag[i] = 1
print("Channel %d is pressed,value is %d" %(i,result[i]))
else:
if(1 == self.touch_flag[i]):
self.touch_flag[i] = 0
print("Channel %d is released,value is %d" %(i,result[i]))



mpr121 = TouchSensorMpr121()
def main():
mpr121.sensor_init()
mpr121.set_threshold(0x60)
mpr121.wait_for_ready()
while 1:
result = mpr121.listen_sensor_status()
mpr121.parse_and_print_result(result)
time.sleep(.1)

if __name__ == '__main__':
main()
success

If everything goes well, you will get the result. When you touch the CH0 ~ CH11 pads, it will trigger channel # pressed and Channel # released with corresponding pressure values.

>>> %Run grove_12_chan_touch_sensor_MPR121.py
Channel 8 is pressed, value is 308
Channel 8 is released, value is 0
Channel 9 is pressed, value is 170
Channel 9 is released, value is 0
Channel 10 is pressed, value is 340
Channel 8 is pressed, value is 180

Schematic Online Viewer

Resources

Project

This is the introduction Video of this product, simple demos, you can have a try.

Leaf Piano: We made a piano using a touch sensor, as well as leaves for piano keys.

Play with Scratch: How to play a scratch game with a Touch Sensor?

Tech Support & Product Discussion

Thank you for choosing our products! We are here to provide you with different support to ensure that your experience with our products is as smooth as possible. We offer several communication channels to cater to different preferences and needs.

Loading Comments...