Hello! Thanks to all for reading. I am using an AD7747 in single-ended mode with a range up to 8.192pf to measure proximity between two parallel plates (or at least i'm trying to.)
Right now the data I'm getting is not changing at all. There are 3 capacitance registers that I am reading and they just aren't changing as I move my plates.
Here is my code,
//----------------------------------------------------------------------------------------
//| AD7747 Capacitive Sensor |
//| Based on code by MiG found at:http://forum.arduino.cc/index.php/topic,11882.0.html| |
//| |
//| Author: Aidan Williamson |
//| Written: 7/3/2014 |
//| ad7747 datasheet:http://www.analog.com/static/imported-files/data_sheets/AD7747.pdf |
//| |
//| |
//| |
//| |
//-----------------------------------------------------------------------------------------
#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.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'10000000' for enable Cap DAC A
Wire.endTransmission();
Serial.println("Hello!"); //test to make sure serial connection is working
}
void readycheck()
{
Wire.beginTransmission(0x48); //talking to chip
Wire.write(byte(0x00)); //status register address
Wire.endTransmission();
Wire.requestFrom(0x48,1); //request status register data
delay(100);
while(capreadybit == 0) //if the ADC has data for us then this bit will be low
{
loop(); //goto the capacitance read program
}
}
void loop() //read capacitance
{
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 data
Wire.requestFrom(0x48,3); //reads data from cap DAC registers 1-3
while(Wire.available())
{
unsigned char hi;
unsigned char mid;
unsigned char 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; //shift high left 16 bits, shift mid left 8 bits, add lo and subtract 0x800000 for zero scale
pf=(float)capacitance/(float)0x800000*8.192f; //8.192 is maximum capacitance reading
Serial.print(pf, DEC); //prints the capacitance data in decimal through serial port
Serial.print(",");
}
Serial.println();
delay(1000);
}
The board I'm using is shown here: http://opensourceecology.org/w/images/1/1d/CapSenseV1Brd.png
I mainly want to know if there are any flaws with my code. This is my first project with arduino and my first in a higher level language. I have experience working in assembly language but not C. So, yeah, if anything stands out please let me know.
Thanks!!!