Hello folks,
I use MPR121 touch sensors and they work fine with the Arduino.
The MPR121 also has the capability to use some of the electrode pins as gpio's and to drive led's, see https://www.nxp.com/docs/en/application-note/AN3894.pdf.
I tried to use pins as output, but i did not succeed.
The register addresses needed for gpio capability are not defined in Adafruit_MPR121.h, so obviously not many people use this feature.
I wrote an h-file for this:
enum
{
MPR121_CTL0 = 0x73, // Control Register 0
MPR121_CTL1 = 0x74, // Control Register 1
MPR121_DATA = 0x75, // Data Register 1
MPR121_DIR = 0x76, // Direction register
MPR121_EN = 0x77, // Enable register
MPR121_SET = 0x78, // Set register
MPR121_CLR = 0x79, // Clear register
MPR121_TOG = 0x7A, // Toggle register
};
I can write to the SET and TOG registers and I can read back the DATA registers, so from the logic side it is OK.
But I do not see any voltage changes on the pins.
I am not sure how to use the CTL0 and CTL1 registers, but setting everything to 0 should work.
Here is my code:
String title1 = "MPR121 GPIO test";
String title2 = "****************";
#include <Wire.h>
#include "Adafruit_MPR121.h"
#include "mpr1221_gpio.h"
#define IPR_ADDR 0x5A
Adafruit_MPR121 cap = Adafruit_MPR121();
//-------------------------------------------------------------------------
void setup() {
bool FoundMPR121 = false;
byte electrodeConfiguration;
byte count = 0;
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial) { // needed to keep leonardo/micro from starting too fast!
delay(10);
}
Serial.println(title1);
Serial.println(title2);
Serial.println("\n");
do
{
delay (200);
Serial.print(IPR_ADDR + count);
FoundMPR121 = cap.begin(IPR_ADDR + count);
if (!FoundMPR121) Serial.println(" MPR121 not found, check wiring?");
count++;
if (count == 4)
{
count = 0;
}
} while (!FoundMPR121);
Serial.println(" MPR121 found");
cap.writeRegister (MPR121_SOFTRESET, 0x63);
delay (100);
cap.writeRegister (MPR121_SOFTRESET, 0x80);
delay (100);
//cap.setThreshholds(THRESH_TOUCH, THRESH_RELEASE);
cap.writeRegister ( MPR121_ECR, 4); // only x Elektrodes, the Rest is GPIO
cap.writeRegister ( MPR121_CTL0, 0);
cap.writeRegister ( MPR121_CTL1, 0); // all GPIO's as CMOS output
cap.writeRegister ( MPR121_DIR, 0xFF); // all GPIO's as output
cap.writeRegister ( MPR121_EN , 0xFF); // all GPIO's enabled
cap.writeRegister ( MPR121_CLR, 0xFF); // Ausgangszustand ... alle 0
cap.writeRegister ( MPR121_SET, 0xFF); // Ausgangszustand
} // setup
//---------------------------------------------------------------
void loop() {
int dataReg;
cap.writeRegister ( MPR121_TOG, 0xFF); // alle GPIO's werden getogglet
dataReg = cap.readRegister8 (MPR121_DATA);
Serial.println (dataReg);
delay (2000);
}
Any hints?
Thanks in advance!