Hi everybody,
I know that the subject was already treated but not for the issue I had.
Purpose:
I want to make a level sensor using a capacitor that I made by myself. I used to copper strips put on a plastic glass and depending on the level of water, the capacitance is changing.
Now, I bought a capacitance to digital converter "AD7747" to read what is exactly the value of my capacitor. I also have an Arduino to communicate with my chip.
Issue:
I am not really familiar with I²C interface and I found on Internet a code written by somebody which seems to work. I understand what the code does but in case of problem, I am not sure to find the right solution.
The thing is:
Send my code to the Arduino --> Ok
Put some Serial.print inside --> Some of them work, others not really. Thus, it seems that it does not enter in certain part of the code.
Actually, I found in the code that the variable "readycap=Wire.read();" is equal to -1 (I print it in the terminal) while it must be equal to 0 to enter in the next part of the code.
Then, my issue is: I don't understand why the value is equal to "-1" and how to resolve this problem.
I put the code below so you can read it and maybe help me.
I want also to clarify that I solder my AD7747 on a adapter but it seems that all connections are good and the wiring connections are the following:
VDD: 5V
GND: 0V
SDA: A4 (on arduino)
SCL: A5 (on arduino)
C(in): floating (I just first want to read 0pF)
I do not use the other one.
Please find also the datasheet of my component:
http://www.analog.com/media/en/technical-documentation/data-sheets/AD7747.pdf
In case of you cannot open the "ino" file:
#include <Wire.h> //include the library for i2c communication
//int capreadybit = (bitRead(TWDR,0)); //initialize variable readybit to be two wire data register's lsb
void setup()
{
Wire.begin(); //sets up i2c for operation
Serial.begin(9600); // we will monitor this via serial cable
Wire.beginTransmission(0x48);
Wire.write(0xBF);
Wire.write(0x00);
Wire.endTransmission();
delay(4);
Wire.beginTransmission(0x48); // begins write cycle
Wire.write(0x07); //address pointer for cap setup register
Wire.write(0xA0); //b'10100000' found from datasheet page 16
Wire.endTransmission(); //ends write cycle
delay(4); // Wait for data to clock out? I'm not 100% sure why this delay is here (or why it's 4ms)
Wire.beginTransmission(0x48); //begins transmission again
Wire.write(0x09); //address pointer for capacitive channel excitation register
Wire.write(0x0E); //recommended value from datasheet
Wire.endTransmission();
delay(4);
Wire.beginTransmission(0x48);
Wire.write(0x0A); //address pointer for the configuration register
Wire.write(0x21); //b'00100001' for continuous conversion, arbitrary VTF setting, and mid-range capacitive conversion time
Wire.endTransmission();
Wire.beginTransmission(0x48);
Wire.write(0x0B); //CAP DAC A Register address (Positive pin data)
Wire.write(0x80); //b'10111111' for enable Cap DAC A
Wire.endTransmission();
Serial.println("Hello!"); //test to make sure serial connection is working
}
void loop()
{
Wire.beginTransmission(0x48); //talking to chip
Wire.write(byte(0x00)); //status register address
Wire.endTransmission();
Wire.requestFrom(0x48,1); //request status register data
int readycap;
readycap=Wire.read();
Serial.print(readycap);
if((readycap&0x1)==0){ // ready?
Serial.println("Data Ready");
delay(500);
Wire.beginTransmission(0x48); //arduino asks for data from ad7747
Wire.write(0x01); //set address point to capacitive DAC register 1
Wire.endTransmission(); //pointer is set so now we can read the
Serial.println("Data Incoming");
delay(500);
Wire.requestFrom(0x48,3); //reads data from cap DAC registers 1-3
while(Wire.available()){
unsigned char hi,mid,lo; //1 byte numbers
long capacitance; //will be a 3byte number
float pf; //scaled value of capacitance
hi=Wire.read();
mid=Wire.read();
lo=Wire.read();
capacitance=(hi<<16)+(mid<<8)+lo-0x800000;
pf=(float)capacitance*-1/(float)0x800000*8.192f;
Serial.print(pf, DEC); //prints the capacitance data in decimal through serial port
Serial.print(",");
}
Serial.println();
}
//Serial.println("Loop Done");
//Serial.println(" ");
delay(1000);
}
ad7747_cap_read_test.ino (3.22 KB)