Courtesy of SparkFun
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.
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.
If you aren't familiar with the Qwiic system, we recommend reading here for an overview.
We would also recommend taking a look at the following tutorials if you aren't familiar with them.
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.
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.
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.
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.
Interrupt Breakout Pin
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.
Left, Middle, and Right Touch Pads
Capacitive Touch Pad Breakout Pins
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!
Jumper pads for the INT pin pull-up, LED disable, and I2C pullup
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.
Qwiic Hardware Assembly
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.
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_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.
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:
.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.
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.
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.
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.
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.
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).
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.
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.
Example 7 Output
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.
Resources and Going Further
For more on the Capacitive Touch Slider - CAP1203 (Qwiic) check out the links below.