MPR121 workswith touch but not when acting as a liquid level sensor
i trying achieve liquid level indicator mpr 121 , followed https://www.youtube.com/watch?v=pjjmqwqgqdm
it worked when manually touch copper plates when adding water, not detect levels. here config
here arduino code
it worked when manually touch copper plates when adding water, not detect levels. here config
code: [select]
//============ device address
// add pin vcc address 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
// group controls filtering when data > baseline.
mpwrite(mhd_r, 0x01);
mpwrite(nhd_r, 0x01);
mpwrite(ncl_r, 0x00);
mpwrite(fdl_r, 0x00);
// section b
// group controls filtering when data < baseline.
mpwrite(mhd_f, 0x01);
mpwrite(nhd_f, 0x01);
mpwrite(ncl_f, 0xff);
mpwrite(fdl_f, 0x02);
// section c
// group sets touch , release thresholds 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 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 , set run mode
// set ele_cfg 0x00 return standby mode
mpwrite(ele_cfg, 0x8c); // enables 12 electrodes
//afe配置1
// cl
// eleprox
// ele:
// section f
// enable auto config , 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 requested
{
#if defined(arduino) && arduino >= 100
a = wire.read(); // receive byte character
b = wire.read();
#else
a = wire.receive(); // receive byte character
b = wire.receive();
#endif
return (a | (int)(b<<8));
}else
{
return 0;
}
}
here arduino code
code: [select]
#include <mpr121.h>
#include <wire.h>
int irqpin = 2; // digital 2
boolean touchstates[12]; //to keep track of previous touch states
int prev = -1;
int tvar = -1;
void setup() {
serial.begin(19200);
wire.begin();
capatouch.begin(); // calls begin above
delay(500);
serial.println("start");
}
void loop()
{
int var = capatouch.touch(); // 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;
}
Arduino Forum > Using Arduino > Sensors > MPR121 workswith touch but not when acting as a liquid level sensor
arduino
Comments
Post a Comment