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 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.
    • Events on May 1, 2014
      • Intro to 3D modelling with Rhino
        Starts: 7:00 pm
        Ends: May 1, 2014 - 10:00 pm
        Description: Register here: https://www.eventbrite.ca/e/intro-to-3d-modelling-with-rhino-tickets-11026308987

        Rhino is the ideal 3D modelling software for artists and designers due to its versatility, low cost, and ability to handle a variety of different types of jobs and export to numerous file formats.

        Like lots of other software, Rhino is adept creating 3D renderings with one of its many powerful rendering plugins available. However, this versatile software is also useful for artists creating files for 3D printing, CNC milling, machining and laser cutting. Rhino has also become increasingly popular among designers for its Grasshopper plugin, a parametric modeller that we'll cover in the next workshop.

        In this workshop we'll learn the basic principles of 3D modelling in general, and the Rhino software in particular, by creating our own model. After that, we'll learn how to use the VRay rendering plugin and create a finished CG image.

        MORE INFO:
        What prior experience do I need?
        None! If you've never done 3D modelling before, that's fine. And if you are familiar with another 3d modelling software like SketchUp, then you'll be ahead of the curve learning Rhino, which interfaces a lot differently. And you will probably really like Rhino.
        Do I get to go home with something cool?
        Yes! You'll have made a well-modelled and well-rendered digital image. Hang it on your fridge!
        What do I need to bring?
        Bring your laptop, and have a trial version of Rhino pre-installed. You can download the trial version here: http://www.rhino3d.com/download . Please note that the trial version only allows you to save 25 times before asking you to pony up for the real version, so don't go too crazy before the workshop date. Also... if you're planning on taking next week's workshop on parametric modelling with the Grasshopper plug-in, then be especially stringy with that CTRL-S! If you can't bring a laptop, no worries--just e-mail me beforehand and I'll get you a loaner.
        Is it OK to have a PC or Mac?
        Rhino is native to PCs, so you're all good a PC user. If you're on Mac, you're also in luck... the OSX version of Rhino is still in beta, so it's free, and it won't be much different from the PC version that the instructor will be teaching on. You'll just have to send McNeel your e-mail address to get a download link, and you 'll be part of their "beta testing" team. Woo-hoo! Bad news for Mac users: next week's Grasshopper workshop won't run on Macs, so if you have a choice between Mac and PC, choose PC.

        ABOUT THE INSTRUCTOR:
        Mani Mani (B.Arch. S. + MArch.) is a Toronto based interdisciplinary designer. Mani is the founder and the director of Fishtnk Design Factory- a design and manufacturing studio focused on innovative design processes in architecture, furniture and architectural products.
        Mani's work covers a wide range of disciplines from fashion design to urban planning and has consulted international firms and design organizations. Mani has been an active member of Toronto’s design community since 2007 and he has worked and collaborated with a number of influential local architects.
        Mani's latest research is focuses on responsive architecture and physical computation in built environments.
        Mani is currently a sessional lecturer at Ryerson University, Department of Architectural Sciences and in the past he has taught workshops and been an invited critic at University of Toronto, Waterloo University and the polytechnique University of Timisuara.


        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.
  • 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.