Go Down

Topic: MPR121 workswith touch but not when acting as a liquid level sensor (Read 46 times) previous topic - next topic

smartwatch

I was trying to achieve liquid level indicator with mpr 121 and followed https://www.youtube.com/watch?v=PJJmQwqGQDM

it worked when i manually touch the copper plates but when adding water, it does not detect the levels. Here is my config


Code: [Select]
//============ Device Address
// ADD pin is Vcc So address is 0x5B
#define MPR121_ADDR 0x5B


// MPR121 Register Defines
#define E0BV 0x1E
#define E1BV 0x1F
#define E2BV 0x20
#define E3BV 0x21
#define E4BV 0x22
#define E5BV 0x23
#define E6BV 0x24
#define E7BV 0x25
#define E8BV 0x26
#define E9BV 0x27
#define E10BV 0x28
#define E11BV 0x29
#define E12BV 0x2A


#define MHD_R 0x2B
#define NHD_R 0x2C
#define NCL_R 0x2D
#define FDL_R 0x2E
#define MHD_F 0x2F
#define NHD_F 0x30
#define NCL_F 0x31
#define FDL_F 0x32


#define ELE0_T 0x41
#define ELE0_R 0x42
#define ELE1_T 0x43
#define ELE1_R 0x44
#define ELE2_T 0x45
#define ELE2_R 0x46
#define ELE3_T 0x47
#define ELE3_R 0x48
#define ELE4_T 0x49
#define ELE4_R 0x4A
#define ELE5_T 0x4B
#define ELE5_R 0x4C
#define ELE6_T 0x4D
#define ELE6_R 0x4E
#define ELE7_T 0x4F
#define ELE7_R 0x50
#define ELE8_T 0x51
#define ELE8_R 0x52
#define ELE9_T 0x53
#define ELE9_R 0x54
#define ELE10_T 0x55
#define ELE10_R 0x56
#define ELE11_T 0x57
#define ELE11_R 0x58


#define AFE1_CFG 0x5C
#define AFE2_CFG 0x5D
#define ELE_CFG 0x5E


#define GPIO_CTRL0 0x73
#define GPIO_CTRL1 0x74
#define GPIO_DATA 0x75
#define GPIO_DIR 0x76
#define GPIO_EN 0x77
#define GPIO_SET 0x78
#define GPIO_CLEAR 0x79
#define GPIO_TOGGLE 0x7A


#define ATO_CFG0 0x7B
#define ATO_CFGU 0x7D
#define ATO_CFGL 0x7E
#define ATO_CFGT 0x7F
#define RES_CF          0x80

// Global Constants //隔住阻焊层
#define TOU_THRESH 0x06
#define REL_THRESH 0x03
#define START_BV 0x90
// Global Constants //隔住0.5mm
//#define TOU_THRESH 0x06
//#define REL_THRESH 0x03



Code: [Select]
void MPR121::begin(void){
// STOP
mpwrite(ELE_CFG, 0x00);
        mpwrite(RES_CF, 0X63);
        delay(10);
// Section A
// This group controls filtering when data is > baseline.
mpwrite(MHD_R, 0x01);
mpwrite(NHD_R, 0x01);
mpwrite(NCL_R, 0x00);
mpwrite(FDL_R, 0x00);

// Section B
// This group controls filtering when data is < baseline.
mpwrite(MHD_F, 0x01);
mpwrite(NHD_F, 0x01);
mpwrite(NCL_F, 0xFF);
mpwrite(FDL_F, 0x02);

// Section C
// This group sets touch and release thresholds for each electrode
mpwrite(ELE0_T, TOU_THRESH);
mpwrite(ELE0_R, REL_THRESH);
mpwrite(ELE1_T, TOU_THRESH);
mpwrite(ELE1_R, REL_THRESH);
mpwrite(ELE2_T, TOU_THRESH);
mpwrite(ELE2_R, REL_THRESH);
mpwrite(ELE3_T, TOU_THRESH);
mpwrite(ELE3_R, REL_THRESH);
mpwrite(ELE4_T, TOU_THRESH);
mpwrite(ELE4_R, REL_THRESH);
mpwrite(ELE5_T, TOU_THRESH);
mpwrite(ELE5_R, REL_THRESH);
mpwrite(ELE6_T, TOU_THRESH);
mpwrite(ELE6_R, REL_THRESH);
mpwrite(ELE7_T, TOU_THRESH);
mpwrite(ELE7_R, REL_THRESH);
mpwrite(ELE8_T, TOU_THRESH);
mpwrite(ELE8_R, REL_THRESH);
mpwrite(ELE9_T, TOU_THRESH);
mpwrite(ELE9_R, REL_THRESH);
mpwrite(ELE10_T, TOU_THRESH);
mpwrite(ELE10_R, REL_THRESH);
mpwrite(ELE11_T, TOU_THRESH);
mpwrite(ELE11_R, REL_THRESH);

// Section D
// Set the Filter Configuration
// Set ESI2
mpwrite(AFE1_CFG, 0x10);
//AFE配置1 (默认=0x10)
// 一级采样FFI:00-6次
// 充放电电流CDC:100000 - 32uA
mpwrite(AFE2_CFG, 0x04);
//AFE配置2 (默认=0x24)
// 充电时间CDT:010 - 1us
// 二级采样SFI:00 - 4次
// 二级采样间隔ESI:100 - 16ms


// Section E
// Electrode Configuration
// Enable 6 Electrodes and set to run mode
// Set ELE_CFG to 0x00 to return to standby mode
mpwrite(ELE_CFG, 0x8C); // Enables all 12 Electrodes

//AFE配置1
// CL
// ELEPROX
// ELE:
// Section F
// Enable Auto Config and auto Reconfig
/*mpwrite(ATO_CFG0, 0x0B);
mpwrite(ATO_CFGU, 0xC9); // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V   mpwrite(ATO_CFGL, 0x82); // LSL = 0.65*USL = 0x82 @3.3V
mpwrite(ATO_CFGT, 0xB5);*/ // Target = 0.9*USL = 0xB5 @3.3V
}


Code: [Select]
int MPR121::touch(void)
{
   Wire.requestFrom((uint8_t)MPR121_ADDR,(uint8_t)2); 
   uint8_t a,b;
   if(Wire.available()>=2)    // slave may send less than requested
   {
#if defined(ARDUINO) && ARDUINO >= 100
     a = Wire.read(); // receive a byte as character
     b = Wire.read();
#else
     a = Wire.receive(); // receive a byte as character
     b = Wire.receive();
#endif
     return (a | (int)(b<<8));
   }else
   {
     return 0;
   }   
}



Here is the arduino code

Code: [Select]
#include <mpr121.h>
#include <Wire.h>


int irqpin = 2; // Digital 2
boolean touchStates[12]; //to keep track of the previous touch states
int prev = -1;
int tVar = -1;

void setup() {
  Serial.begin(19200);
  Wire.begin();
  CapaTouch.begin(); // this calls the begin above
  delay(500);
  Serial.println("START"); 
}

void loop()
{
  int var = CapaTouch.touch(); // this calls touch defined above
  int num = GetSensorNumber(var);

  if(num != -1 && num != prev)
  {
    Serial.print("Num: ");
    Serial.println(num);
    prev = num;
  } 
}

int GetSensorNumber(int var)
{
   if ((var&0x0800) >0) return 12;
   if ((var&0x0400) >0) return 11;
   if ((var&0x0200) >0) return 10;
   if ((var&0x0100) >0) return 9;
   if ((var&0x0080) >0) return 8;
   if ((var&0x0040) >0) return 7;
   if ((var&0x0020) >0) return 6;
   if ((var&0x0010) >0) return 5;
   if ((var&0x0008) >0) return 4;
   if ((var&0x0004) >0) return 3;
   if ((var&0x0002) >0) return 2;
   if ((var&0x0001) >0) return 1;
  return -1;
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy