Contents

Interfacing MPR121 Touch 3×4 Keypad module with Arduino

MPR121 Touch Keypad Features

Switches are one of the most widely used components in electronics. A keypad is a set of keys or buttons arranged in a block. This capacitor touch keypad includes 12 keys and MPR121 IC on the back of the PCB.

Other features:

  • Operating voltage: 3.3V
  • Communication protocol: I2C
  • Operating temperature range: -40°C to +85°C

You can download the datasheet of the this module here.

MPR121 Touch Keypad Pinout

This Module has 5 pins:

  • VCC: Module power supply – 3.3V
  • GND: Ground
  • SCL: I2C Clock
  • SDA: I2C Data
  • IRQ: Interrupt output pin

You can see the pinout of this module in the image below.

Required Material

Hardware component

Arduino UNO R3 × 1
3x4 MPR121 Capacitive Touch Keypad Module × 1
Male to Female jumper wire × 1

Software Apps

Arduino IDE

Interfacing MPR121 Touch Keypad with Arduino

Step 1: Circuit

The following circuit show how you should connect Arduino to MPR121 sensor. Connect wires accordingly.

Step 2: Library

Go to Library manager and search for MPR121, install the following library.

Tip

If you need more help with installing a library on Arduino, read this tutorial: How to Install an Arduino Library

Step 3: Code

Upload the following code to Arduino.

  /*
  3X4-MPR121-Capacitive-Touch-Keypad-Module
  Modified on 10 Jan 2021
  by Amir Mohammad Shojaee @ Electropeak
  
Home
based on Adafruit Library Example */ #include <Wire.h> #include "Adafruit_MPR121.h" #ifndef _BV #define _BV(bit) (1 << (bit)) #endif // You can have up to 4 on one i2c bus but one is enough for testing! Adafruit_MPR121 cap = Adafruit_MPR121(); // Keeps track of the last pins touched // so we know when buttons are 'released' uint16_t lasttouched = 0; uint16_t currtouched = 0; void setup() { Serial.begin(9600); while (!Serial) { // needed to keep leonardo/micro from starting too fast! delay(10); } Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); // Default address is 0x5A, if tied to 3.3V its 0x5B // If tied to SDA its 0x5C and if SCL then 0x5D if (!cap.begin(0x5A)) { Serial.println("MPR121 not found, check wiring?"); while (1); } Serial.println("MPR121 found!"); } void loop() { // Get the currently touched pads currtouched = cap.touched(); for (uint8_t i=0; i<12; i++) { // it if *is* touched and *wasnt* touched before, alert! if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) { Serial.print(i); Serial.print(" touched"); } // if it *was* touched and now *isnt*, alert! if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) { Serial.println(" released"); } } // reset our state lasttouched = currtouched; // comment out this line for detailed data from the sensor! return; // debugging info, what Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX); Serial.print("Filt: "); for (uint8_t i=0; i<12; i++) { Serial.print(cap.filteredData(i)); Serial.print("\t"); } Serial.println(); Serial.print("Base: "); for (uint8_t i=0; i<12; i++) { Serial.print(cap.baselineData(i)); Serial.print("\t"); } Serial.println(); // put a delay so it isn't overwhelming delay(100); }

In this code two required libraries are included at the beginning. Then the I2C address is entered.

As each key is hit, note that the binary number for the key appears on Serial Monitor with this phrase: “Touched”. As each key is released the phrase “Released” appears.

The output is as follows. As you can see, the number 0 to 11 are touched in order.

Liked What You See?​
Get Updates And Learn From The Best​

Comments (2)

  • Robin Reply

    that is not working with arduino uno r4 WiFi.
    I absolutely have no problem eith the Arduino nano.
    Someone ANY idea why this is?
    Tried two of the boards with different wiring… always working on nano, never onu r4…

    August 12, 2023 at 12:05 pm
    • Mohammad Damirchi Reply

      Hi Robin
      Run I2CScanner from Arduino examples to check the address of the sensor and make sure the wiring is correct.

      August 13, 2023 at 9:32 am

Leave a Reply

Your email address will not be published. Required fields are marked *