Skip to content

Capacitive Touch Slider Wireling Tutorial

Touch can be detected using any of the six individual capacitive touch sensors on the back of this Wireling. With detectability all along the back of the board, sliding motions can be interpreted to add a variety of inputs to your next tiny project!

This Wireling features the ATtiny841, a high performance, low-power AVR 8-bit controller that comes with preloaded firmware to perform measurements through I²C.

Technical Details

TECH SPECS

ATtiny841

  • 8 KB Flash Memory
  • 512 Bytes SRAM and EEPROM
  • Voltage: 1.7V - 5.5V

    TinyDuino Power Requirements

    • Voltage: 3.0V - 5.5V 
    • Current:
      • 300µA (Active Mode)
      • 0.1µA  (Power-down Mode)

    Pins Used

    • A5/SCL - I2C Serial Clock line
    • A4/SDA - I2C Serial Data line

    Dimensions

    • 10mm x 80mm (.394 inches x 3.15 inches)
    • Max Height (from the lower bottom of Wireling to upper top Wireling Connector): 3.70mm (0.15 inches)
    • Weight: 1 gram (.04 ounces)

    Materials

    To interface with any TinyCircuits Arduino board, you will need the Arduino IDE and a Micro USB Cable

    There are multiple processor/adapter combinations that can be used with Wirelings. Use the following table to find the combination that works for you.

    Processor Adapter
    *TinyDuino and USB TinyShield Wireling Adapter TinyShield
    *TinyZero Wireling Adapter TinyShield
    *TinyScreen+ Wireling Adapter TinyShield
    WirelingZero N/A
    *RobotZero N/A
    Arduino Wireling Arduino Shield
    Raspberry Pi Wireling Pi Hat

    * These processors have a 32-pin connector and can have multiple Wireling Adapter TinyShields stacked to increase the number of Wireling ports up to a maximum of 32 total Wireling ports.

    In order to interface with Wirelings, you'll need the appropriate number of Wireling Cables and the Wireling.h Library (You can download this from GitHub as linked, or from the Library Manager in the Arduino IDE).

    You will also need the Capacitive Touch Slider Wireling and the included example Arduino sketch.

    Wireling Library & Example Code
    Port 0 Capacitive Touch Slider ATtiny841 Library

    Hardware Assembly

    Depending on the development system you choose, you will need to put together a TinyDuino stack using the 32-pin tan connectors, or you will just need to plug in your Wireling to Port 0 using a Wireling Cable. (You can change this port in the included Arduino Sketch using the Wireling.selectPort() function)

    NOTE: Be mindful when inserting Wireling Cables - the connector pins inside the 5-pin connectors on Wirelings can be bent when cables are inserted at an angle.


    Software Setup

    For this Wireling, you will need to download the ATtiny841 library. The zip file for this library can be found under the Materials section. To install an Arduino library, check out our Library Installation Help Page. The example program for this board can be found in the examples/ folder of the ATtiny841 Library. Open the example in the Arduino IDE.

    Make the correct Tools selections for your development board. If unsure, you can double check the Help page that mentions the Tools selections needed for any TinyCircuits processor.


    Upload Program

    Upload the program and open the Serial Monitor in full screen to see a graphical display of where you have touched the touch pads on the back of the Wireling. The visual display will only be printed while touching the Wireling.

    Code
    //-------------------------------------------------------------------------------
    //  TinyCircuits Capacitive Touch Slider Basic Example
    //  Last Updated 24 Feb 2020
    //
    //  This example code shows basic usage of the AST1003 Capacitive Touch Slider
    //  Wireling. The board has an ATtiny841 microcontroller loaded with firmware that
    //  implements capacitive touch sensing on six copper pads on the PCB, and
    //  communicates the results over I2C. The CapTouchWireling library interprets
    //  these results and provides a 0 to 100 position value when a finger is
    //  detected on the touchpad. In this example, this value is used to print a
    //  graphic output to the Serial Monitor.
    //
    //  Written by Ben Rose, TinyCircuits http://tinycircuits.com
    //
    //-------------------------------------------------------------------------------
    
    
    #include <Wire.h>
    #include <Wireling.h>
    #include <ATtiny841Lib.h>
    #include <CapTouchWireling.h>
    
    CapTouchWireling capTouch;
    
    
    #if defined (ARDUINO_ARCH_AVR)
    #define SerialMonitorInterface Serial
    #elif defined(ARDUINO_ARCH_SAMD)
    #define SerialMonitorInterface SerialUSB
    #endif
    
    int capTouchPort = 0;
    
    void setup() {
      SerialMonitorInterface.begin(9600);
      Wire.begin();
      Wireling.begin();
      Wireling.selectPort(capTouchPort);
    
      while (!SerialMonitorInterface && millis() < 5000); //This will block until the Serial Monitor is opened on TinyScreen+/TinyZero platform!
      if (capTouch.begin()) {
        SerialMonitorInterface.println("Capacitive touch Wireling not detected!");
        //while (1);
      }
    }
    
    
    
    void loop() {
      //function capTouch.update() must be called to update touch values, but the
      //return value(true or false) assigned to completedTouch does
      //not need to be used. capTouch.duration() is optional as well.
    
      bool completedTouch = capTouch.update();
      if (completedTouch) {
        long touchMillis = capTouch.duration();
        float touchSeconds = touchMillis / 1000.0;
        SerialMonitorInterface.print("Completed Touch: ");
        SerialMonitorInterface.print(touchSeconds);
        SerialMonitorInterface.println("s");
      }
    
      if (capTouch.isTouched()) {
        int pos = capTouch.getPosition();  //getPosition() returns a position value from 0 to 100 across the sensor
        int mag = capTouch.getMagnitude(); //getMagnitude() returns a relative 'pressure' value of about 50 to 500
        for (int i = 0; i < 100; i++) {
          if (abs(i - pos) <= (mag / 100)) {
            SerialMonitorInterface.print('|');
          } else {
            SerialMonitorInterface.print(' ');
          }
        }
        SerialMonitorInterface.print(pos);
        SerialMonitorInterface.print('\t');
        SerialMonitorInterface.print(mag);
        SerialMonitorInterface.print('\t');
        SerialMonitorInterface.println(capTouch.duration());
      }
    
    
      //Alternate usage: read the raw output from each sensor
      /*
        SerialMonitorInterface.print(capTouch.capTouchRead(0));SerialMonitorInterface.print('\t');
        SerialMonitorInterface.print(capTouch.capTouchRead(1));SerialMonitorInterface.print('\t');
        SerialMonitorInterface.print(capTouch.capTouchRead(2));SerialMonitorInterface.print('\t');
        SerialMonitorInterface.print(capTouch.capTouchRead(3));SerialMonitorInterface.print('\t');
        SerialMonitorInterface.print(capTouch.capTouchRead(4));SerialMonitorInterface.print('\t');
        SerialMonitorInterface.print(capTouch.capTouchRead(5));
        SerialMonitorInterface.println();
      */
    }
    

    The values printed after the touch pad tick marks are Position (0-100), relative 'pressure' Magnitude (50-500), and the duration of the touch in milliseconds. Once a touch sequence is complete, the touch time will be printed in seconds.


    Downloads


    Contact Us

    If you have any questions or feedback, feel free to email us or make a post on our forum. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.

    Thanks for making with us!