Advertisement
Guest User

Cap sensor

a guest
Jul 3rd, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //----------------------------------------------------------------------------------------
  2. //| AD7747 Capacitive Sensor |
  3. //| Based on code by MiG found at:http://forum.arduino.cc/index.php/topic,11882.0.html| |
  4. //| |
  5. //| Author: Aidan Williamson (although I didn't do much original work) |
  6. //| Written: 7/3/2014 |
  7. //| ad7747 datasheet:http://www.analog.com/static/imported-files/data_sheets/AD7747.pdf |
  8. //| |
  9. //| |
  10. //| |
  11. //| |
  12. //-----------------------------------------------------------------------------------------
  13. #include <Wire.h> //include the library for i2c communication
  14.  
  15.  
  16. int ready = 78; //ready pin will go low when conversion is complete
  17.  
  18. void setup()
  19. {
  20. pinMode(ready, INPUT); //we will read the ready bit from pin 2
  21. Wire.begin(); //sets up i2c for operation
  22. Serial.begin(9600); // we will monitor this via serial cable
  23. Wire.beginTransmission(0x48); // begins write cycle
  24. Wire.write(0x07); //address pointer for cap setup register
  25. Wire.write(0xA0); //b'10100000' found from datasheet page 16
  26. Wire.endTransmission(); //ends write cycle
  27. delay(4);// Wait for data to clock out? I'm not 100% sure why this delay is here (or why it's 4ms)
  28. Wire.beginTransmission(0x48);//begins transmission again
  29. Wire.write(0x09); //address pointer for capacitive channel excitation register
  30. Wire.write(0x0F); //This will vary depending on your setup. See page 17 of datasheet
  31. Wire.endTransmission();
  32. delay(4);
  33. Wire.beginTransmission(0x48);
  34. Wire.write(0x0A); //address pointer for the configuration register
  35. Wire.write(0xA1); //b'00011001' for continuous conversion, arbitrary VTF setting, and mid-range capacitive conversion time
  36. Wire.endTransmission();
  37. }
  38.  
  39. void loop() //set up for reading and transmitting data
  40. {
  41. int ready = digitalRead(ready); //sets up ready pin as digital input
  42.  
  43. while (ready ==HIGH) //use digital pin 22 as interrupt pin
  44. {
  45. delay(1);//why not
  46. }; //do nothing at all!
  47. Wire.beginTransmission(0x48); //arduino asks for data from ad7747
  48. Wire.write(0x01); //set address point to capacitive DAC register 1
  49. Wire.endTransmission(); //pointer is set so now we can read the data
  50. Wire.requestFrom(0x48,3); //reads data from cap DAC registers 1-3
  51. while(Wire.available())
  52. {
  53. char capacitance = Wire.read();
  54. Serial.print(capacitance, DEC); //prints the capacitance data in decimal through serial port
  55. Serial.print(",");
  56. }
  57. Serial.println();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement