2.24 MPR121 Module¶
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.
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.
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.
The function of the module is included in the library MPR121_JM.h.
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.
Setup MPR121 module.
Make interrupt judgment, when the electrode state changes, the function returns a Boolean value 「0」.
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.