Capacitive Sensors Interfaced to Arduino

capsense

Capacitive touch sensors are used extensively in consumer electronic devices like iphones, laptop touchpads and buttons, but they can also be used as localized proximity sensors, or turning non-conductive materials like glass into physical interfaces for all kinds of electronics projects. Here are a couple of sensors I’ve been experimenting with lately:

::QT301 (see example) outputs a PWM signal based on capacitive touch/proximity sensing.

QT113 for basic touch sensing applications (acts like an on/off switch). Here’s Tom Igoe’s arduino/QT113 tutorial

::Capacitive Touch Sensor Breakout Board from SparkFun electronics which is based on the AD7746 (see datasheet here for more technical details).

The AD7746 uses I2C (two wire) serial to send and receive information with microcontrollers like the arduino. Since there is little documentation on using this device that is easy to understand I figured I’d donate some sample arduino code (in the name of science!). To communicate over I2C the arduino needs to access the Wire library. The Wire library uses analog pin 4 for sending and receiving data (SDA), and analog pin 5 as the clock (SCL). These pins have internal pull-up resistors on atmega168 and atmega328 (Diecimila, and Duemilanove) so you may ignore these resistors in the datasheet diagrams. If you are using a Boarduino which has no 3.3v power output, you’ll need to use a voltage regulator like the L1085-33 to bump 5v down to the 3.3v the sensor wants to see. Here’s the code- the setup function calibrates the sensor, and the loop function keeps checking its value and spitting it out via Serial.

#include 

#define SLAVEWRT 0x48 // address of device during write cycle
#define SLAVERD  0x48 // address of device during read cycle

int mySensor = 0; // where capacitive sensor data is kept

void setup()
{

  Wire.begin(); // sets up i2c for operation
  Serial.begin(9600); // set up baud rate for serial

  Wire.beginTransmission(SLAVEWRT); // start i2c cycle
  Wire.send(0xBF); // reset the device
  Wire.endTransmission(); // ends i2c cycle

  Wire.beginTransmission(SLAVEWRT); // begin write cycle
  Wire.send(0x0A); // point to config register
  Wire.send(0x01 | (7 << 3)); // calib. mode, slow sample
  Wire.endTransmission(); // ends cycle

  Wire.beginTransmission(SLAVEWRT); // begin write cycle
  Wire.send(0x07); // cap setup reg
  Wire.send(0x80); // cap enabled
  Wire.endTransmission(); // ends cycle

  Wire.beginTransmission(SLAVEWRT); // begin write cycle
  Wire.send(0x09); //  EXC register
  Wire.send(0x08); // EXC source A
  Wire.endTransmission(); // ends cycle

  Wire.beginTransmission(SLAVEWRT); // begin write cycle
  Wire.send(0x0B); // CAPDAC A reg
  Wire.send(0x00 | 39); // capdac a is on, offset about 1.2pF
  Wire.endTransmission(); // ends cycle

  Wire.beginTransmission(SLAVEWRT); // begin write cycle
  Wire.send(0x0A); // configuration register
  Wire.send(0x01 | (7 << 3)); // continuous mode
  Wire.endTransmission(); // ends cycle

}

void loop() // main program begins
{

    Wire.beginTransmission(SLAVERD); // begin read cycle
    Wire.send(0x00); //pointer to first cap data register
    Wire.endTransmission(); // end cycle

    Wire.requestFrom(SLAVERD,4); // reads 4 bytes

    byte data[2];
    int i = 0;
      while(Wire.available())
     {
	char c = Wire.receive();
        data[i] = c;
        i++;
     }

   mySensor = -((data[0]<<16)+(data[1]<<8)+data[2]) / 12;
   Serial.println(mySensor);

  delay(50);
}
Posted in Geekery, Physical Computing, Programming and tagged , , , , , . Bookmark the permalink. RSS feed for this post. Leave a trackback.

8 Responses to Capacitive Sensors Interfaced to Arduino

  1. steve blair says:

    Right on! I’ve been looking at the QT113 for turning backlit acrylic panels into touch sensitive buttons. While searching for more info though, I found this page http://tiny.cc/BdYRa which suggests using the QT100A as a newer replacement for the QT113. So, I’ve ordered some and now I just have to wait anxiously for them to come in the mail before I can start playing with them.

  2. Jess Brouse says:

    Cool, let me know how it goes. Have fun.

  3. Treizeur says:

    Damn ! I have problems to make it work properly (AD7746 and Arduino Duemilanove)… What kind of value does it returns ? I have a value of approximately 420, which goes up near 500 when I put a finger so close to the sensor. If I touch it the value is 0. Is it normal values ?
    There are a lot of things that I don’t really understand (for example, if I change parts of the code nothing happens !?)…
    If you have time to spend with me it would be great help ! (I understand if not, however ! )

  4. Daniel says:

    Lovely well explained code Jess, I thank you kindly and will credit ifi use.
    Having a few problems getting Channel 2 of the sensor activated and set up. Any advice at all!!! have looked at the data sheet and see that what I need to do it is just the hex that I’m stuck with!!!!!
    Thanks again
    Daniel

  5. Jess Brouse says:

    I haven’t used two channels, can’t help you there sorry.

    Treizeur:
    Expect numbers to vary greatly depending on positioning, and if it is sensing through a material like glass, plastic, air etc. Getting sensitivity right is a chronic issue with capacitive sensors as whenever the physical environment they’re in changes, they produce different values. So basically get a sense of the range of numbers in your situation and use a bit of math to make those numbers meaningful (say 0-255 if you want to control a PWM pin with it)

  6. Treizeur says:

    Thanks your answer helps me ! In fact I have no idea how capacitance works, I’m trying to build something that detects pieces of adhesives ribbon onto textile ribbons. I’ve seen existing devices that uses this technology, I hope the AD7746 can accomplish that. I will try to build a kind of fork as I’ve seen to let the ribbon pass through, perhaps the values will be less messy.
    Thanks again.

  7. bunsen says:

    Jess!

    Thank you thank you thank you!!!! You’ve helped me out so much, I’ve been trying to get the AD7746 working for hours now and I tried your code. Of course it worked first time.

    You’re a legend.

  8. bunsen says:

    Just one question though.

    Parts of the code above such as:
    “Wire.send(0×01 | (7 << 3)); // calib. mode, slow sample"
    and
    "Wire.send(0×00 | 39); // capdac a is on, offset about 1.2pF"

    What is the "| (7 << 3)" and the "| 39 " doing?

Leave a Reply

  • Upcoming Workshops and Events

    • Events on April 17, 2014
      • Motors
        Starts: 7:00 pm
        Ends: April 17, 2014 - 10:00 pm
        Description: Register here: https://www.eventbrite.ca/e/motors-workshop-tickets-11025454431

        In the first of two motor workshops, we'll deal with simple motors that don't require complicated circuits to control them: DC motors and AC motors. We'll start by explaining how motors work, how to select the right kind of motor to do the job, and finally, how to mechanically connect the motor to whatever it is you want to move.
        Then we'll get our hands dirty, trying out some different motors and mechanical components like gears, cams and couplings.

        BONUS: COMES WITH EXCITING MOTOR FUNPACK!
        This workshop costs more than our regular offerings because we give you some awesome and useful stuff to take home with you. Your registration fee includes the cost of:
        A DC Hobby Motor
        A Gearbox kit for added torque
        A selection of pulleys and shafts
        A selection of SPDT microswitches
        Batteries included!

        About the instructor:

        Bill Gentles has worked for the past 30 years as a Biomedical Engineer. He has a PhD in Biomedical Engineering from the University of Toronto. He has recently been shifting careers as he has discovered that what he really wanted to be when he grew up was an artist. He previously presented the motors workshop at Interaccess in 2011.

        Refund and Cancellation Policy:

        Refund requests are granted if received 10 business days or more prior to the workshop date. Exceptions to this policy will be clearly stated.
    • Events on April 23, 2014
      • Open Studio
        Starts: 7:00 pm
        Ends: April 23, 2014 - 10:00 pm
        Location: 9 Ossington Ave, Toronto ON
        Description: Hang out with some like-minded artists, technicians, and nerds. Bring projects and share them, or get help with problems.
    • Events on April 24, 2014
      • Motors
        Starts: 7:00 pm
        Ends: April 24, 2014 - 10:00 pm
        Description: Register here: https://www.eventbrite.ca/e/stepper-motors-workshop-tickets-11025869673

        In this, the second of two workshops based on different types of motors, we'll work with stepper motors. Steppers are powerful motors that provide precision control over the rotation speed or exact position of the motor shaft; they also require electronic circuits or microcontrollers to operate them. We'll talk about how to choose the right stepper motor for the job. We'll build a control circuit based on a 555 timer and a special power driver, connect our motor and start playing. We'll also discuss how to use the Arduino microcontroller to control speed and direction of DC motors, servomotors and stepper motors and look at some alternative Arduino-compatible motor controller boards.

        BONUS: COMES WITH EXCITING MOTOR FUNPACK!
        This workshop costs more than our regular offerings because we give you some awesome and useful stuff to take home with you. Your registration fee includes the cost of:
        A stepper motor
        A stepper motor driver circuit, breadboard-ready
        Assorted electronic breadboard components

        About the Instructor:
        Bill Gentles has worked for the past 30 years as a Biomedical Engineer. He has a PhD in Biomedical Engineering from the University of Toronto. He has recently been shifting careers as he has discovered that what he really wanted to be when he grew up was an artist. He previously presented the motors workshop at Interaccess in 2013.

        Refund and Cancellation Policy:

        Refund requests are granted if received 10 business days or more prior to the workshop date. Exceptions to this policy will be clearly stated.
    • Events on April 30, 2014
      • Open Studio
        Starts: 7:00 pm
        Ends: April 30, 2014 - 10:00 pm
        Location: 9 Ossington Ave, Toronto ON
        Description: Hang out with some like-minded artists, technicians, and nerds. Bring projects and share them, or get help with problems.
  • Recent Posts

  • Sign up for InterAccess Mailing List

    Get reminders about upcoming workshops, exhibitions, and events
    * = required field

    powered by MailChimp!
  • Archives

Swedish Greys - a WordPress theme from Nordic Themepark.