It's easy to use the MPR121 sensor with Python or CircuitPython and the Adafruit CircuitPython MPR121 module.  This module allows you to easily write Python code that reads capacitive touch from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a MPR121 to your board exactly as shown on the previous pages for Arduino. Here's an example of wiring a Feather M0 to the sensor with I2C:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCL to sensor SCL
  • Board SDA tosensor SDA

Here's an example of the MPR121 shield connected to a Metro M0:

Solder the headers onto the shield, then simply connect it to your Metro M0.

This shield may not work with the Metro M4! It is not designed to work with it and may cause undesired behavior from the M4 microcontroller board. If you want to use this shield with CircuitPython for a project, use the Metro M0!

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi SCL to sensor SCL
  • Pi SDA to sensor SDA

You can also use the MPR121 Pi HAT with a Raspberry Pi:

  • Simply solder the header provided with the HAT onto the HAT, and attach the HAT to the Pi.

CircuitPython Installation of MPR121 Library

You'll need to install the Adafruit CircuitPython MPR121 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware. Carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-Express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_mpr121.mpy
  • adafruit_bus_device

Before continuing, make sure your board's lib folder has the adafruit_mpr121.mpy and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of MPR121 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-mpr121

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the sensor, we'll initialize it and read capacitive touch from the board's Python REPL.

If you're using an I2C connection, run the following code to import the necessary modules and initialize the I2C connection with the sensor:

import time
import board
import busio
import adafruit_mpr121
i2c = busio.I2C(board.SCL, board.SDA)
mpr121 = adafruit_mpr121.MPR121(i2c)

Now you're ready to read capacitive touch from the sensor. Use the following syntax to check a specific pin.

  • mpr121[i].value - Return True if the specified pin is being touched, otherwise returns False.

Use a value 0 to 11 for [i] and it will return a boolean True or False value depending on if the input is currently being touched or not.

For example, to print when pin 0 is touched, run the following code, and then touch pin 0:

while True:
    if mpr121[0].value:
        print("Pin 0 touched!")

If you don't see any messages when you touch the inputs, you might need to ground yourself to the board by touching the GND pin on the board with one finger and then touching the input pads with another finger.  

Also make sure nothing is touching the pins when you first run the code, or else it might confuse the MPR121's touch detection (unmount the board's file system from your operating system, then press the board's reset button to reset the script and run it again with nothing touching the pins). The pins are calibrated on start-up, and will not react properly if you're touching the pins when the board starts up.

To print when any pin is touched, run the following code and then touch any capacitive touch pin:

while True:
    for i in range(12):
        if mpr121[i].value:
            print('Input {} touched!'.format(i))

The example doesn't show its usage, but if you want to check all of the inputs at once you can use touched_pins. This function returns a 12 member tuple of the current state for each of the 12 pins. True is touched and False is not touched. For example, to test if pin 0 and 11 are being touched with one call you could run code like:

# Use touched_pins to get current state of all pins.
touched = mpr121.touched_pins
# Test if 0 and 11 are touched.
if touched[0] and touched[11]:
    print('Input 0 and 11 touched!')

That's all there is to using the MPR121 module with CircuitPython!

Full Example Code

# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
# SPDX-License-Identifier: MIT

# Simple test of the MPR121 capacitive touch sensor library.
# Will print out a message when any of the 12 capacitive touch inputs of the
# board are touched.  Open the serial REPL after running to see the output.
# Author: Tony DiCola
import time
import board
import busio

# Import MPR121 module.
import adafruit_mpr121

# Create I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)

# Create MPR121 object.
mpr121 = adafruit_mpr121.MPR121(i2c)

# Note you can optionally change the address of the device:
# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)

# Loop forever testing each input and printing when they're touched.
while True:
    # Loop through all 12 inputs (0-11).
    for i in range(12):
        # Call is_touched and pass it then number of the input.  If it's touched
        # it will return True, otherwise it will return False.
        if mpr121[i].value:
            print("Input {} touched!".format(i))
    time.sleep(0.25)  # Small delay to keep from spamming output messages.

This guide was first published on Jul 25, 2014. It was last updated on Jul 25, 2014.

This page (Python & CircuitPython) was last updated on May 29, 2023.

Text editor powered by tinymce.