Capacitive touch sensor CAP1188

FunduinoLast updated on

Acht Berührungssensoren – vom Testaufbau zum Instrument

Der CAP1188 erkennt Berührungen, ohne dass etwas gedrückt werden muss. Er misst, wie gut ein Körper elektrische Ladung speichert – und darin sind menschliche Haut und alles Leitfähige gut.

Acht Sensoren, acht LEDs, ein I²C-Anschluss. Nach einem kurzen Testaufbau mit der internen LED des Arduino wird daraus ein Instrument: An Büroklammern, Alufolie oder Kupferstreifen geklemmt, spielt jeder Kontakt einen eigenen Ton.

CAP1188 board with eight touch pins and eight LEDs
The CAP1188

You need this

KY-012 Buzzer Modul - aktiv mit Oszillator, 3,3-5 V KY-012 Buzzer Modul - aktiv mit Oszillator, 3,3-5 V 1,39 € *Item no. F23106686About the product — KY-012 Buzzer Modul - aktiv mit Oszillator, 3,3-5 V
KY-006 Buzzer Modul - passiv, 3-5 V, PWM-gesteuert KY-006 Buzzer Modul - passiv, 3-5 V, PWM-gesteuert 1,30 € *Item no. F23106690About the product — KY-006 Buzzer Modul - passiv, 3-5 V, PWM-gesteuert
Lautsprecher 5 W - 8 Ohm, 30 x 70 mm Lautsprecher 5 W - 8 Ohm, 30 x 70 mm 1,84 € *Item no. F23107409About the product — Lautsprecher 5 W - 8 Ohm, 30 x 70 mm
CAP1188 Touchsensor - kapazitiv, 8 Kanäle, I2C, 3,3-5 V CAP1188 Touchsensor - kapazitiv, 8 Kanäle, I2C, 3,3-5 V 11,26 € *Item no. F23108527About the product — CAP1188 Touchsensor - kapazitiv, 8 Kanäle, I2C, 3,3-5 V
Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino 13,90 € *Item no. F23107480About the product — Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino
Krokodilklemmenkabel - 10 Stück, 22 cm, male Steckerstift Krokodilklemmenkabel - 10 Stück, 22 cm, male Steckerstift 3,00 € *Item no. F23107468About the product — Krokodilklemmenkabel - 10 Stück, 22 cm, male Steckerstift

All 6 parts together 32,69 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

What the CAP1188 can do

The CAP1188 has eight capacitive sensors, C1 to C8. They respond to how well an object can store electrical charge - human skin and conductive objects can do this.

When you touch a touch pin, the reading changes and the contact is triggered. Each touch pin has an LED on the board that lights up.

The sensor connects via I²C.

The article says “one LED (L8 to L7).” With eight sensors, there are eight LEDs - L8 to L1.

CAP1188 board with eight touch pins and eight LEDs
The CAP1188

First test: toggle the internal LED

Connect a wire to each of C1 and C2. The free end of the wire is your touch surface - touching it triggers the sensor. The two pins switch the Arduino's built-in LED on and off.

Install the library through the Library Manager: Adafruit CAP1188. In the following dialog, you must also install the additional required library.

You can adjust the sensitivity using register 0x1f: low values respond to proximity, while high values only respond to actual touch.

The sketch turns on at C2 and off at C1; the text above says exactly the opposite.

CAP1188 on Arduino, two wires on pins C1 and C2
Cables on C1 and C2 as touch surfaces
Adafruit CAP1188 library in the library manager
The Adafruit CAP1188 library
Dialog for installing the additional required library
Install the additional library too.
1 / 3
Switch the internal LED via C1 and C2
#include "Adafruit_CAP1188.h"
// Assign a name
Adafruit_CAP1188 cap = Adafruit_CAP1188();
void setup()
{
  Serial.begin(9600);
  delay(500);
  pinMode(LED_BUILTIN, OUTPUT);
  if (!cap.begin())
  {
    // Stop the program if the CAP1188 is not found
    Serial.println("CAP1188 not found");
    while (1);
  }
  Serial.println("CAP1188 found!");
  delay(1000);
  // Set sensitivity
  // 10 = very sensitive -> responds even when approached
  // up to 111, less sensitive -> object must be touched
  // can be adjusted depending on the object
  int Sensitivity = cap.readRegister(0x1f) & 0x0f;
  cap.writeRegister(0x1f, Sensitivity | 111);
}
void loop()
{
  int Touch = cap.touched();
  if (Touch == 0)
  {
    // no touch detected
    return;
  }
  // Check for touches
  for (int i = 0; i < 8; i++)
  {
    if (Touch & (1 << i))
    {
      // start with the highest value
      if (i + 1 == 2) digitalWrite(LED_BUILTIN, HIGH);
      if (i + 1 == 1) digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

Expansion: Music from Everyday Objects

Anything that conducts electricity can be used as a trigger: paper clips, aluminum foil, copper foil, screws, nails - and foods that contain water.

This turns into an instrument: eight touch surfaces produce eight notes on a scale.

Aluminum foil strips as touch pads on the CAP1188
Aluminum foil touch pads
Paper clips as touch surfaces on the CAP1188
Contact surfaces made from paper clips
1 / 2

Assembly and wiring

Connect a cable with an alligator clip to each pin from C8 to C1, then clip it to your object. Connect the speaker to pin 7.

The two mapping tables show which tone each pin plays: C8 is low C, and C1 is high C, one octave above.

The parts list lists an active buzzer first. The sketch uses tone() and needs a passive speaker - an active buzzer only produces its fixed tone.

Speaker for audio output
The speaker
Circuit diagram of the music project with CAP1188 and speaker
The schematic for the music project
Assignment of the copper strips to pins C8 to C1
Which strip belongs to which pin?
Table with pins, notes, and corresponding frequencies
Pins, notes, and frequencies
1 / 4

The music program

The sketch checks all eight sensors on every pass and plays the corresponding note for each triggered pin - from C’ at 262 Hz to C” at 523 Hz.

Play eight tones using the touch pads
#include "Adafruit_CAP1188.h"
#define Speaker 7
#define ToneLength 100
// Assign a name
Adafruit_CAP1188 cap = Adafruit_CAP1188();
void setup()
{
  Serial.begin(9600);
  if (!cap.begin())
  {
    // Stop the program if the CAP1188 is not found
    Serial.println("CAP1188 not found");
    while (1);
  }
  Serial.println("CAP1188 found!");
  delay(1000);
  // Set sensitivity
  // 10 = very sensitive -> reacts even when something is nearby
  // up to 111, less sensitive -> object must be touched
  // can be adjusted depending on the object
  int Sensitivity = cap.readRegister(0x1f) & 0x0f;
  cap.writeRegister(0x1f, Sensitivity | 111);
}
void loop()
{
  int Touch = cap.touched();
  if (Touch == 0)
  {
    // no touch detected
    return;
  }
  // Check for touches
  for (int i = 0; i < 8; i++)
  {
    if (Touch & (1 << i))
    {
      // start with the highest value
      if (i + 1 == 8) tone(Speaker, 262, ToneLength); // C'
      if (i + 1 == 7) tone(Speaker, 294, ToneLength); // D'
      if (i + 1 == 6) tone(Speaker, 330, ToneLength); // E'
      if (i + 1 == 5) tone(Speaker, 349, ToneLength); // F'
      if (i + 1 == 4) tone(Speaker, 392, ToneLength); // G'
      if (i + 1 == 3) tone(Speaker, 440, ToneLength); // A'
      if (i + 1 == 2) tone(Speaker, 494, ToneLength); // H'
      if (i + 1 == 1) tone(Speaker, 523, ToneLength); // C"
    }
  }
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.