2.24 MPR121 Module

Overview

In this lesson, you will learn how to use MPR121. It’s a good option when you want to add a lot of touch switches to your project. The electrode of MPR121 can be extended with a conductor. If you connect a wire to a banana, you can turn the banana into a touch switch, thus realizing projects such as fruit piano.

Fritzing Circuit

In this example, we insert MPR121 into the breadboard. Get the GND of MPR121 connected to GND, 3.3V to 3V3, IRQ to the digital pin 2, SCL to the pin SCL(21), and SDA to the pin SDA(20). There are 12 electrodes for touch sensing. Note: MPR121 is powered by 3.3V, not 5V.

../_images/image201.png

Schematic Diagram

../_images/image202.png

Code

Note

  • You can open the file 2.24_mpr121.ino under the path of sunfounder_vincent_kit_for_arduino\code\2.24_mpr121 directly.

  • Or copy this code into Arduino IDE 1/2.

  • Then Upload the Code to the board.

  • Please make sure you have added the library called MPR121_JM, detailed tutorials refer to Add Libraries.

After uploading the codes to the Mega2560 board, the touch state of pins of MPR121 「1」and「0」will be recorded in a 12 - bit boolean type of array that will be printed on the serial monitor.

Code Analysis

The function of the module is included in the library MPR121_JM.h.

#include <MPR121_JM.h>

Library Functions:

MPR121(int irqpin,uint8_t touThresh,uint8_t relThresh)

Creates a new instance of the MPR121.

  • irqpin: the interrupt request pin.

  • touThresh: Touch threshold,the electrode is recognized as a threshold of the 「Touch」 state.

  • relThresh: Release threshold,the electrode is recognized as a threshold of the 「Release」 state.

The range of the electrode data value is 0~255. For typical touch application, the threshold value can be in range 0x05~0x30 for example. The smaller the value, the more sensitive it is. The setting of the threshold is depended on the actual application.Typically the touch threshold is a little bigger than the release threshold to touch debounce and hysteresis.

void mpr121_setup()

Setup MPR121 module.

bool checkInterrupt()

Make interrupt judgment, when the electrode state changes, the function returns a Boolean value 「0」.

uint16_t readTouchInputs()

The touch state of the electrode produces a Boolean value. The function accumulates the Boolean values generated by all the electrodes in sequence into a 12-bit binary number as the return value. If the first and eleventh electrodes are touched, 「100000000010」 is returned.

Phenomenon Picture

../_images/image203.jpeg