Maker.io main logo

Capacitive Touch Slider (CAP1203) Hookup Guide

2019-10-08 | By SparkFun Electronics

License: See Original Project Programmers

Courtesy of SparkFun

Introduction

Do you want to replace a slider or a button on your art project or science experiment with a more interesting interface? The SparkFun Capacitive Touch Slider - CAP1203 (Qwiic) is a Qwiic and easy way to add capacitive touch to your next project. With the board's built in touch pads, you can immediately start playing with the touch capabilities as three unique touch inputs or as a slider. You can also enable a touch input to act as a power button, customize the sensitivity for your own touch pads, and play with the interrupt alert LED. Since the sensor supports I2C, we've added a Qwiic connector for easy integration into the Qwiic environment. We've also added breakout pins for the capacitive touch inputs, so you can connect to your own touch pads.

 

Required Materials

To follow along with the example code used in this tutorial, you will also need the following materials. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.

Heads up! If you are using the RedBoard without a Qwiic connector, we recommend getting the Qwiic Shield for Arduino.

If you need different sizes of Qwiic cables, we offer a kit that contains many sizes but we also carry them individually.

Suggested Reading

If you aren't familiar with the Qwiic system, we recommend reading here for an overview.

1_qwiic

We would also recommend taking a look at the following tutorials if you aren't familiar with them.

  • Switch Basics: A tutorial on electronics' most overlooked and underappreciated component: the switch! Here we explain the difference between momentary and maintained switches and what all those acronyms (NO, NC, SPDT, SPST, ...) stand for.
  • Capacitors: Learn about all things capacitors. How they're made. How they work. How they look. Types of capacitors. Series/parallel capacitors. Capacitor applications.
  • I2C: An introduction to I2C, one of the main embedded communications protocols in use today.
  • How to Work with Jumper Pads and PCB Traces: Handling PCB jumper pads and traces is an essential skill. Learn how to cut a PCB trace, add a solder jumper between pads to reroute connections, and repair a trace with the green wire method if a trace is damaged.
  • RedBoard Qwiic Hookup Guide: This tutorial covers the basic functionality of the RedBoard Qwiic. This tutorial also covers how to get started blinking an LED and using the Qwiic system.

Hardware Overview

Note: If you want to do anything outside of what is covered in this tutorial or the example code, please refer to the CAP1203 datasheet for exact details on the sensor functionality.

Power

You can provide 3.3V through the polarized Qwiic connectors on the board or through the 3V3 labeled pin on the through-hole header. The Qwiic system is meant to use 3.3V, so be sure you are NOT using another voltage when using the Qwiic system. When you have correctly powered the board, the red power LED will turn on.

2_Power_LED

Power LED, Breakout Pin, and Qwiic Connectors

Qwiic Connectors or I2C Pins

There are two Qwiic connectors on the back of the board to easily connect the sensor to I2C. If you prefer the old school method of connecting to I2C, we've also included four breakout pins on the side of the board.

3_Qwiic_Connectors

Qwiic Connectors and I2C Breakout Pins

Interrupt Pin and LED

The interrupt pin is an active low output which is triggered each time a sensor pad is touched. You can connect to this pin if you want to check when an interrupt occurs. On the front side of the board, the green LED in the bottom right corner also signals when an interrupt occurs.

4_Interrupt

Interrupt Breakout Pin

5_Interrupt LED

Interrupt LED

Capacitive Touch Pads and Pins

On the front of the board, there is an arrow shape which contains three separate capacitive touch pads. In order to know which pad is which, orient the board with the arrow pointing in the right direction. We will reference these capacitive touch pads as the left pad, the middle pad, and the right pad throughout the guide and in the code. We also broke out the capacitive touch sensor lines as plated through-holes on the top of the board. You can use these pins to connect to your own capacitive touch pads. The CS1 pin connects to the left pad, the CS2 pin connects to the middle pad, and the CS3 pin connects to the right pad.

6_Touch_Pads

Left, Middle, and Right Touch Pads

7_Capacitive

Capacitive Touch Pad Breakout Pins

Jumpers

There are 3 jumper pads on the back of the board, each labeled with its function. First, on upper left side of the board, there is a two way jumper labeled INT that connects to a 10kΩ pull-up resistor on the interrupt data line. If you cut this jumper, it will disconnect the 10kkΩ pull-up resistor from the interrupt data line. Next, on the lower left side of the board, there is a two way jumper labeled LED that connects to the green interrupt LED. If you cut this jumper, it will disconnect the LED from the interrupt line, effectively disabling the green LED's functionality. Lastly, on the lower half of the board, there is a three way jumper labeled I²C that connects two 2.2kkΩ pull-up resistors to the I2C data lines. If you have multiple devices on your I2C data lines, then you may consider cutting this jumper. Not sure how to cut a jumper? Read here!

8_Jumper

Jumper pads for the INT pin pull-up, LED disable, and I2C pullup

Hardware Assembly

With the Qwiic connector system, assembling the hardware is easy. For this tutorial, we use the SparkFun RedBoard Qwiic, an associated USB Cable, and a Qwiic cable. Plug your Qwiic cable between the RedBoard Qwiic and the Capacitive Touch Slider. Then, connect the microcontroller via the USB Cable to your computer. And...that's it! You're ready to start uploading code. Otherwise, if you're going to be soldering, then connect wires from the power, ground, and I2C data line breakout pins to the microcontroller of your choice. Using the CS1, CS2, and CS3 plated through-hole pins, you can also break out the capacitive touch lines to your own capacitive touch pads.

9_capacitive_touch_hookup

Qwiic Hardware Assembly

Arduino Library

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

We've provided a library to help you easily control and configure your Capacitive Touch Slider. You can download the library by searching 'SparkFun Qwiic Capacitive Touch Slider' in the Arduino library manager. Some of the features include reading when a pad is touched, detecting right and left swipes, enabling a power button, and setting the sensitivity for your own touch pads. You can also manually install the library by clicking the button below to get the library from its GitHub repository. We have provided seven different example sketches to help you get started.

SPARKFUN CAPACITIVE TOUCH SLIDER ARDUINO LIBRARY (ZIP)

Library Functions

The Arduino library is commented, and the functions should be self-explanatory. However, below is a detailed list of the available library functions.

Note: It is recommended that users begin with the examples in the following section before diving head first into the library functions. The library examples demonstrate how to setup the more basic features of the Capacitive Touch Slider without having to dig through the datasheet. Once users have become familiar with the basic setups and have gone through the datasheet thoroughly, it should be easier to follow the library functions below.

Initialization Settings: We use these to set-up and initialize the board.

.begin() - Initialize the sensor, returns true if correctly set-up.

.isConnected() - Checks I2C connection, returns true if correctly connected.

Sensitivity Settings: These functions allow you to change the sensitivity settings for your touch pads. Note, the default sensitivity is set to SENSITIVITY_2X in the .begin() function for the SparkFun Capacitive Touch Slider board. We only recommend changing the sensitivity setting if you are breaking out your own touch pads.

.setSensitivity(sensitivity) - Sets the sensitivity multiplier for the touch pads.

Possible sensitivity argument values:

SENSITIVITY_128X - Most sensitive

SENSITIVITY_64X

SENSITIVITY_32X

SENSITIVITY_16X

SENSITIVITY_8X

SENSITIVITY_4X

SENSITIVITY_2X

SENSITIVITY_1X - Least sensitive

.getSensitivity() - Returns the sensitivity multiplier for the current sensitivity settings, returns as an integer value.

Power Button Settings: These functions allow you to enable or disable the power button on a specific pad. The power button requires a longer touch before registering a touch has been detected on the designated pad. Note, when the power button is enabled, the designated pad will only act as the power button.

.setPowerButtonPad(pad) - Sets a button to act as a power button, returns true if correctly set.

Possible pad argument values:

PAD_LEFT - Left pad and CS1 breakout pin

PAD_MIDDLE - Middle pad and CS2 breakout pin

PAD_RIGHT - Right pad and CS3 breakout pin

.getPowerButtonPad() - Returns which pad is currently set to be the power button, returns as an integer value.

Return values:

1 - Left pad and CS1 breakout pin

2 - Middle pad and CS2 breakout pin

3 - Right pad and CS3 breakout pin

.setPowerButtonTime(time) - Sets the power button touch time, returns true if correctly set.

Possible time argument values:

PWR_TIME_280_MS

PWR_TIME_560_MS

PWR_TIME_1120_MS

PWR_TIME_2240_MS

.getPowerButtonTime() - Returns length of time (ms) power button must indicate a touch, returns as an integer value.

.setPowerButtonEnabled() - Enables power button functionality.

.setPowerButtonDisabled() - Disables power button functionality.

isPowerButtonEnabled() - Returns true if power button functionality is currently enabled.

Interrupt Settings: These functions allow you to control if the interrupt pin is enabled. When enabled, the green interrupt LED will turn on when any pad detects a touch. Note, the interrupt is enabled as default in the .begin() function.

.setInterruptDisabled() - Disables interrupt pin.

.setInterruptEnabled() - Enables interrupt pin.

.isInterruptEnabled() - Returns true if interrupt pin is currently enabled.

Check if a capacitive touch pad or breakout pin has been touched: These functions allow you to check when a capacitive touch pad or breakout pin has been touched.

.isLeftTouched() - Returns true if left pad or CS1 pin detects a touch.

.isMiddleTouched() - Returns true if middle pad or CS2 pin detects a touch.

.isRightTouched() - Returns true if right pad or CS3 pin detects a touch.

.isTouched() - Returns true if any pad or breakout pin detects a touch.

.isPowerButtonTouched() - Returns true if designated power button pad or pin held for allotted time.

Check if a swipe has occurred: The direction of the swipe is relative to the arrow on the board pointing in the right direction. For the pins, a right swipe is first CS1, then CS2, and finally CS3 and a left swipe is first CS3, then CS2, and finally CS1. Note, these functions pull most available resources. For best swipe recognition, we highly recommend not implementing other functionalities when using these two functions.

.isRightSwipePulled() - Returns true if board detects a right swipe.

.isLeftSwipePulled() - Returns true if board detects a left swipe.

Example Code

Note: This section is an example of using the Capacitive Touch Slider - CAP1203 (Qwiic) and the RedBoard Qwiic with the Arduino IDE. It is not intended to be a guide for using I2C devices with the Arduino IDE.

Please use the following links and the Internet for a better understanding of I2C and how it works in the Arduino IDE:

Example 1: Basic Reading

The Example01_BasicReading.ino sketch works with the basic functionality of the Capacitive Touch Slider and streams which pad detects a touch. Open the example to follow along. First, we include the CAP1203 library and initialize an instance of the sensor. Then, in the set-up function, we check to make sure the sensor is correctly connected. Now that the sensor is set-up, we can start checking which pads have been touched in the main loop using the .isLeftTouched(), .isMiddleTouched(), and .isRightTouched() functions. Once you have uploaded the code, you can open your serial terminal to see when a pad has been touched. Make sure the baud rate in your serial terminal is set to 9600 baud, otherwise you won't see the correct output.

10_Example-1

Example 1 Output

Example 2: Detect Any Touch

The setup for Example02_DetectAnyTouch.ino sketch is similar to Example 1, except it streams when any pad detects a touch using the .isTouched() function.

11_Example-2

Example 2 Output

Example 3: Detect Swipe

The setup for Example03_DetectSwipe.ino sketch is similar to Example 1, except it streams when the board detects a swipe using the .isRightSwipePulled() and .isLeftSwipePulled() functions. The direction of the swipe is relative to the arrow on the board pointing in the right direction. Note, these functions pull most available resources. For best swipe recognition, we highly recommend not implementing other functionalities when using these two functions.

12_Example-3

Example 3 Output

Example 4: Power Button

The Example04_PowerButton.ino sketch allows you enable or disable the power button feature on a specific pad. The power button requires a longer touch before registering a touch has been detected on the designated pad. Note, when the power button is enabled, the designated pad will only act as the power button. Configuring and enabling the power button feature requires some additional set-up.

In the sketch, we use the .setPowerButtonPad() and .setPowerButtonTime() functions to set which pad will act as the power button and the length of time the designated pad must indicate a touch. We also implement the .getPowerButtonPad() and .getPowerButtonTime() functions to check which pad and time we set with the previous two functions. Then, we enable the power button using the .setPowerButtonEnabled() function, and we check that the power button has been properly enabled using the .isPowerButtonEnabled() function. Finally, we check when the designated power button pad has been held for the allotted time using the .isPowerButtonTouched() function. The serial port prints Power Button when it registers a power button touch.

13_Example-4

Example 4 Output

Example 5: Detect Current Touch

The Example05_DetectCurrentTouch.ino sketch uses the same functions and set-up as Example 1. However, Example 5 only notifies you about the current touch as opposed to a constant stream of touch data in Example 1. In order to do so, we added a while loop inside of each conditional statement. For example: while (sensor.isLeftTouched() == true).

14_Example-5

Example 5 Output

Example 6: Disable Interrupt

The Example06_DisableInterrupt.ino sketch allows you to programmatically adjust the interrupt settings. When disabled, the green interrupt LED will no longer turn on when a pad is touched. We use the .setInterruptDisabled() function to disable to interrupt and the .isInterruptEnabled() function to check the current interrupt settings. In the main loop, we then implement the .isTouched() function as in Example 2 to check that our board still registers a touch even though the interrupt LED no longer turns on.

15_Example-6

Example 6 Output

Example 7: Set Sensitivity

The Example07_SetSensitivity.ino sketch allows you to programmatically adjust the sensitivity settings for your capacitive touch pads. Note, we only recommend changing the sensitivity settings if you are breaking out your own touch pads. The current sensitivity settings are calibrated for the SparkFun Capacitive Touch Slider on board pads. In our example, we use the .setSensitivity() function to change the sensitivity and the .getSensitivity() to check our current sensitivity settings. In the main loop, we then implement the .isTouched() function as in Example 2 to check when the sensor has been touched with the new sensitivity settings.

16_Example-7

Example 7 Output

Troubleshooting

Don't know if your board is working properly? To check that your board is properly hooked up, start by connecting your Capacitive Touch Slider and uploading the first example sketch to your microcontroller. Then, when you touch one of the pads, as demonstrated in the GIF, the green interrupt LED in the bottom right corner should turn on each time it senses a touch. (Note, if you have not yet uploaded code, the interrupt LED will turn on when you first connect to your microcontroller. The LED will turn off once you have uploaded the Example 1 code.) The red power LED on the back of the board, you can see its reflection on the table in the GIF, will also be on when the board is properly powered.

17_Demo

Resources and Going Further

For more on the Capacitive Touch Slider - CAP1203 (Qwiic) check out the links below.

制造商零件编号 SEN-15344
CAP TOUCH SLIDER - CAP1203 (QWII
SparkFun Electronics
制造商零件编号 DEV-15123
REDBOARD QWIIC ATMEGA328 EVAL BD
SparkFun Electronics
制造商零件编号 U050-006-GY-MAX
CBL USB2.0 A PLUG-MCR B PLUG 6'
Tripp Lite
制造商零件编号 PRT-14426
QWIIC CABLE - 50MM
SparkFun Electronics
制造商零件编号 DEV-13975
REDBOARD ATMEGA328 EVAL BRD
SparkFun Electronics
制造商零件编号 DEV-14352
QWIIC SHIELD FOR ARDUINO
SparkFun Electronics
制造商零件编号 KIT-15081
QWIIC CABLE KIT
SparkFun Electronics
制造商零件编号 PRT-14427
QWIIC CABLE - 100MM
SparkFun Electronics
制造商零件编号 PRT-14429
QWIIC CABLE - 500MM
SparkFun Electronics
制造商零件编号 PRT-14425
QWIIC CABLE - BREADBOARD JUMPER
SparkFun Electronics
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum