that’s my code
#define BLESERIAL Serial1
//Capacative sensors - AD7746 definitions
#define I2C_ADDRESS 0x48
#define REGISTER_STATUS 0x00
#define REGISTER_CAP_DATA 0x01
#define REGISTER_VT_DATA 0x04
#define REGISTER_CAP_SETUP 0x07
#define REGISTER_VT_SETUP 0x08
#define REGISTER_EXC_SETUP 0x09
#define REGISTER_CONFIGURATION 0x0A
#define REGISTER_CAP_DAC_A 0x0B
#define REGISTER_CAP_DAC_B 0x0B
#define REGISTER_CAP_OFFSET 0x0D
#define REGISTER_CAP_GAIN 0x0F
#define REGISTER_VOLTAGE_GAIN 0x11
#define RESET_ADDRESS 0xBF
#define VALUE_UPPER_BOUND 0x00FFFFFFL
#define VALUE_LOWER_BOUND 0x0L
/*
#define VALUE_UPPER_BOUND 16000000L
#define VALUE_LOWER_BOUND 0xFL
*/
#define MAX_OUT_OF_RANGE_COUNT 3
#define CALIBRATION_INCREASE 1
byte calibration;
byte outOfRangeCount = 0;
unsigned long offset = 0;
byte CAP_DAC_A = 0x4B;
long value =0;
double Capacitance;
double Offset;
//Varous variables
int tempRead = 0;
boolean debug = false;
void setup()
{
delay(1000);
//Initiate Teensy LED
// pinMode(ledPin, OUTPUT);
//Initiate Teensy Serial port on USB
Serial.begin(9600);
//Initiate Teensy Serial 1 port toward Bluetooth module
// BLESERIAL.begin(115200);
//Initiate capacative sensors over I2C
Wire.begin(I2C_ADDRESS );//, I2C_PINS_18_19, I2C_PULLUP_INT, I2C_RATE_400);// I2C setup for Master mode, pins 18/19, internal pullups, 400kHz
Serial.println("Initializing");
Wire.beginTransmission(I2C_ADDRESS); // start i2c cycle
Wire.write(RESET_ADDRESS); // reset the device
Wire.endTransmission(); // ends i2c cycle
delay(1000);
writeRegister(REGISTER_EXC_SETUP,0x23); // 0x23 EXC B, level VDD/2 || 0x 07 EXC A, level VDD/2
writeRegister(REGISTER_CAP_DAC_A,(byte)(0x80 | CAP_DAC_A)); // 0x80 - cap setup reg - cap enabled
writeRegister(REGISTER_CAP_DAC_B,0x80); // Offset
writeRegister(REGISTER_CAP_SETUP,0x80); // Offset
writeRegister(REGISTER_CONFIGURATION, 0xA1);
Serial.println("Getting offset");
offset = ((unsigned long)readInteger(REGISTER_CAP_OFFSET)) << 8;
Serial.print("Factory offset: ");
Serial.println(offset);
delay(100); //wait for calibration
display_Status();
Serial.print("Calibrated offset: ");
offset = ((unsigned long)readInteger(REGISTER_CAP_OFFSET)) << 8;
Serial.println(offset);
display_Status();
//calibrate();
Serial.println("done");
Spark.variable("sensorValue", &value, INT);
}
void loop()
{
//Read capacitance from the sensor
value = readValue();
//Check if debug mode is enabled
//if (debug){
//Convert read value to pF and print on Serial over USB
Capacitance = (((double)(value - 8388608) / 8388608) * 4.096);
Offset = 0.132857143 * CAP_DAC_A;
/* //Adjust calibartion/range of the capacitance sensor if needed
if ((value<=VALUE_LOWER_BOUND) or (value>=VALUE_UPPER_BOUND)) {
outOfRangeCount++;
}
if (outOfRangeCount>MAX_OUT_OF_RANGE_COUNT) {
if (value <= VALUE_LOWER_BOUND) {
calibrate(-CALIBRATION_INCREASE);
}
else {
calibrate(CALIBRATION_INCREASE);
}
outOfRangeCount=0;
}
*/
//Sleep for 100ms so we have sampling arounf 10 Hz
delay(100);
}
//Procedure to toggle LED
/*void tooglePin(){
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}*/
//Write data to a register on capacative sensor connected over I2C
void writeRegister(unsigned char r, unsigned char v)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(r);
Wire.write(v);
Wire.endTransmission();
}
//Read an integer value from capacative sensor connected over I2C
unsigned int readInteger(unsigned char r) {
union {
char data[2];
unsigned int value;
}
byteMappedInt;
byteMappedInt.value = 0;
Wire.beginTransmission(I2C_ADDRESS); // begin read cycle
Wire.write(0); //pointer to first cap data register
Wire.endTransmission(); // end cycle
//after this, the address pointer is set to 0 - since a stop condition has been sent
Wire.requestFrom(I2C_ADDRESS,r+2); // reads 2 bytes plus all bytes before the register
while (!Wire.available()==r+2) {
; //wait
}
for (int i=r+1; i>=0; i--) {
uint8_t c = Wire.read();
if (i<2) {
byteMappedInt.data[i]= c;
}
}
return byteMappedInt.value;
}
//Read a long value from capacative sensor connected over I2C
unsigned long readLong(unsigned char r) {
union {
char data[4];
unsigned long value;
}
byteMappedLong;
byteMappedLong.value = 0L;
Wire.beginTransmission(I2C_ADDRESS); // begin read cycle
Wire.write(0); //pointer to first data register
Wire.endTransmission(); // end cycle
//the data pointer is reset anyway - so read from 0 on
Wire.requestFrom(I2C_ADDRESS,r+4); // reads 2 bytes plus all bytes before the register
while (!Wire.available()==r+4) {
; //wait
}
for (int i=r+3; i>=0; i--) {
uint8_t c = Wire.read();
if (i<4) {
byteMappedLong.data[i]= c;
}
}
return byteMappedLong.value;
}
//Adjust range of the capacitave sensor in desired direction
void calibrate (byte direction) {
writeRegister(REGISTER_CAP_SETUP, 0);
writeRegister(REGISTER_CONFIGURATION, 0);
/*
calibration += direction;
//assure that calibration is in 7 bit range
calibration &=0x7f;
*/
if(direction<0){
if (CAP_DAC_A == 0){
CAP_DAC_A = 0x7f;
}
else{
CAP_DAC_A--;
}
}
else{
if (CAP_DAC_A == 0x7f){
CAP_DAC_A = 0;
}
CAP_DAC_A++;
}
writeRegister(REGISTER_CAP_DAC_A, (byte)(0x80 | CAP_DAC_A));
writeRegister(REGISTER_CAP_SETUP, 0x80);
writeRegister(REGISTER_CONFIGURATION, 0xA1);
}
//Initial adjustment of range of the capacitave sensor
/*void calibrate() {
calibration = 0;
Serial.println("Calibrating CapDAC A");
long value = readValue();
while (value>VALUE_UPPER_BOUND && calibration < 128) {
writeRegister(REGISTER_CAP_SETUP, 0);
writeRegister(REGISTER_CONFIGURATION, 0);
calibration++;
writeRegister(REGISTER_CAP_DAC_A, _BV(7) | calibration);
writeRegister(REGISTER_CAP_SETUP, 0x80);
writeRegister(REGISTER_CONFIGURATION, 0xA1);
value = readValue();
}
Serial.println("done");
}
*/
//Read a value of measured capacitance from capacative sensor connected over I2C
long readValue() {
long ret = 0;
uint8_t data[3];
char status = 0;
//wait until a conversion is done
// while (!(status & (_BV(0) | _BV(2)))) {
//wait for the next conversion
status= readRegister(REGISTER_STATUS);
// }
unsigned long value = readLong(REGISTER_CAP_DATA);
value >>=8;
//we have read one byte to much, now we have to get rid of it
ret = value;
return ret;
}
//Read a value from a register from capacative sensor connected over I2C
unsigned char readRegister(unsigned char r)
{
unsigned char v;
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(r); // register to read
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 1); // read a byte
while(Wire.available()==0) {
// waiting
}
v = Wire.read();
return v;
}
//Read multiple value from multiple registers from capacative sensor connected over I2C
void readRegisters(unsigned char r, unsigned int numberOfBytes, unsigned char buffer[])
{
unsigned char v;
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(r); // register to read
Wire.endTransmission();
Wire.requestFrom((uint8_t)I2C_ADDRESS, numberOfBytes); // read a byte
char i = 0;
while (i<numberOfBytes) {
while(!Wire.available()) {
// waiting
}
buffer[i] = Wire.read();
i++;
}
}
//Display/print status register of the capacative sensor to USB serial
void display_Status() {
unsigned char data[18];
//Read the status register
readRegisters(0,18,data);
//Print data on serial port
Serial.println("\nAD7746 Registers:");
Serial.print("Status (0x0): ");
Serial.println(data[0],BIN);
Serial.print("Cap Data (0x1-0x3): ");
Serial.print(data[1],BIN);
Serial.print(".");
Serial.print(data[2],BIN);
Serial.print(".");
Serial.println(data[3],BIN);
Serial.print("VT Data (0x4-0x6): ");
Serial.print(data[4],BIN);
Serial.print(".");
Serial.print(data[5],BIN);
Serial.print(".");
Serial.println(data[6],BIN);
Serial.print("Cap Setup (0x7): ");
Serial.println(data[7],BIN);
Serial.print("VT Setup (0x8): ");
Serial.println(data[8],BIN);
Serial.print("EXC Setup (0x9): ");
Serial.println(data[9],BIN);
Serial.print("Configuration (0xa): ");
Serial.println(data[10],BIN);
Serial.print("Cap Dac A (0xb): ");
Serial.println(data[11],BIN);
Serial.print("Cap Dac B (0xc): ");
Serial.println(data[12],BIN);
Serial.print("Cap Offset (0xd-0xe): ");
Serial.print(data[13],BIN);
Serial.print(".");
Serial.println(data[14],BIN);
Serial.print("Cap Gain (0xf-0x10): ");
Serial.print(data[15],BIN);
Serial.print(".");
Serial.println(data[16],BIN);
Serial.print("Volt Gain (0x11-0x12): ");
Serial.print(data[17],BIN);
Serial.print(".");
Serial.println(data[18],BIN);
}