Using with Arduino

Its super easy to use this sensor board with an Arduino thanks to the great Adafruit library. Once you've installed the library you can connect the sensor board via I2C or SPI to your Arduino, it will work with any kind or flavor. If you're using a different kind of microcontroller, the library is a good reference to help you port the code over.

Download the library


First up, we'll download the Arduino library from the Arduino library manager.

Open up the Arduino library manager:

Search for the Adafruit CAP1188 library and install it

We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use

Run Test Sketch

After you've restarted, you should be able to load up the File->Examples->Adafruit_CAP1188->cap1188test sketch
Wire up the sensor as shown in the Wiring section, for I2C. Connect the GND pin to ground, VIN pin to 5V and connect SDA to your Arduino's SDA pin, SCL to SCL pin
  • On UNO/Duemilanove/etc, SDA == Analog 4, SCL == Analog 5
  • On Leonardo/Micro, SDA == Digital 2, SCL == Digital 3
  • On Mega/ADK/Due, SDA == Digital 20, SCL == Digital 21
Upload the sketch and open up the serial console at 9600 baud. You should see that the CAP1188 is found (good!) and then you can touch the C1 thru C8 pads with your fingers to see the touch sensor go off. When you touch a pin, you'll also see the matching LED light up.

I2C with a different address

If you're using multiple sensors, or you just want to change the I2C address to something else, you can choose from 5 different options - 0x28, 0x29 (default), 0x2A, 0x2B, 0x2C and 0x2D
The I2C address are selected by connecting a resistor to the AD pin in the lower right: different resistors set a different address. The easiest address to set is 0x28 which is just a wire from AD to the 3Vo pin.
Now look in the test sketch for the lines
if (!cap.begin()) {
Serial.println("CAP1188 not found");
while (1);
}
And change the cap.begin() statement to cap.begin(0x28) to initialize it to use the 0x28 address. Don't forget to do a full power off of the Arduino & sensor, the sensor only detects the i2c address on power up so you can't change it on the fly!

Using with SPI


You can also use SPI if you wish. The library supports both hardware SPI (using the 'hardware SPI' port on your arduino) or software/bit-bang SPI, where you can define the pins. In general, these sensors are not very fast so I2C is a good way to interface them but if you wish, SPI is there for you too! For example, if you want to have more than 5 of these connected to one board, that is possible to do with SPI, but the I2C interface on this chip doesn't support that many shared I2C addresses.

To enable SPI, be sure to wire for SPI as shown in the Wiring section, and do a power reset of your board so that the chip 'wakes up' in SPI mode

If you're using hardware SPI, check the SPI page for what pins you need to use, sometimes they are only on the ICSP header which makes them trickier to use.

To enable the hardware SPI interface, create the Adafruit_CAP1188 object with
Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_CS, CAP1188_RESET);
If you are using software SPI, you can use any pins that are not already used for some purpose. In that case, create the object with
Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_CLK, CAP1188_MISO, CAP1188_MOSI, CAP1188_CS, CAP1188_RESET);

Using the external IRQ Interrupt

Arduino has some basic ability to attach to pin interrupts, here's an example from Nobody123 of connecting the IRQ pin from the CAP1188 to digital pin #3 on an Uno (Interrupt #1) for tracking touches asynchronously
Download: file
/***************************************************
  This is a library for the CAP1188 I2C/SPI 8-chan Capacitive Sensor

  Designed specifically to work with the CAP1188 sensor from Adafruit
  ----> https://www.adafruit.com/products/1602

  These sensors use I2C/SPI to communicate, 2+ pins are required to 
  interface
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries. 
  BSD license, all text above must be included in any redistribution
 ****************************************************/
 
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_CAP1188.h>

// Reset Pin is used for I2C or SPI
#define CAP1188_RESET  4

// CS pin is used for software or hardware SPI
#define CAP1188_CS  10

// These are defined for software SPI, for hardware SPI, check your
// board's SPI pins in the Arduino documentation
#define CAP1188_MOSI  11
#define CAP1188_MISO  12
#define CAP1188_CLK  13

volatile byte interrupt = 0;

// For I2C, connect SDA to your Arduino's SDA pin, SCL to SCL pin
// On UNO/Duemilanove/etc, SDA == Analog 4, SCL == Analog 5
// On Leonardo/Micro, SDA == Digital 2, SCL == Digital 3
// On Mega/ADK/Due, SDA == Digital 20, SCL == Digital 21

// Use I2C, no reset pin!
// Adafruit_CAP1188 cap = Adafruit_CAP1188();

// Or...Use I2C, with reset pin
//Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_RESET);

// Or... Hardware SPI, CS pin & reset pin
Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_CS, CAP1188_RESET);

// Or.. Software SPI: clock, miso, mosi, cs, reset
//Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_CLK, CAP1188_MISO, CAP1188_MOSI, CAP1188_CS, CAP1188_RESET);

void setup() {
  Serial.begin(9600);
  Serial.println("CAP1188 test!");
  pinMode(3,INPUT);
  // Raise SPI slave select (SS) pins
  // Communication begins when you drop the individual select signals to LOW
  digitalWrite(10,HIGH);
 

  // Initialize the sensor, if using i2c you can pass in the i2c address
  // if (!cap.begin(0x28)) {
  if (!cap.begin()) {
    Serial.println("CAP1188 not found");
    while (1);
  }
  Serial.println("CAP1188 found!");
  pinMode(3, INPUT);
  // Turn off multitouch so only one button pressed at a time
  cap.writeRegister(0x2A, 0x80);  // 0x2A default 0x80 use 0x41  — Set multiple touches back to off
  cap.writeRegister(0x41, 0x39);  // 0x41 default 0x39 use 0x41  — Set "speed up" setting back to off
  cap.writeRegister(0x72, 0x00);  // 0x72 default 0x00  — Sets LED links back to off (default)
  cap.writeRegister(0x44, 0x41);  // 0x44 default 0x40 use 0x41  — Set interrupt on press but not release
  cap.writeRegister(0x28, 0x00);  // 0x28 default 0xFF use 0x00  — Turn off interrupt repeat on button hold
  EIFR = 1; // clear flag for interrupt 1
  attachInterrupt(1, routine_Interrupt_CAP1188, FALLING);
}

void loop() {
 
  // Serial.println(digitalRead(3));
  uint8_t touched = cap.touched();

  if (touched == 0) {
    // No touch detected
    // return;
  }
 
  for (uint8_t i=0; i<8; i++) {
    if (touched & (1 << i)) {
      Serial.print("C"); Serial.print(i+1); Serial.print("\t");
    }
  }
  Serial.println();
  delay(50);
  Serial.print("Interrupt: ");  Serial.println(interrupt);
}

void routine_Interrupt_CAP1188()  {
  ++interrupt;
}
This guide was first published on Jan 10, 2014. It was last updated on Jan 10, 2014. This page (Using with Arduino) was last updated on Dec 07, 2019.