touchio lib for Arduino

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
fredrabelo
 
Posts: 6
Joined: Mon Nov 28, 2022 4:53 am

touchio lib for Arduino

Post by fredrabelo »

Hello everyone!

I bought a feather rp2040, and it's a pretty cool board, but I'm struggling with the compatibility with Arduino.

I'm kind of new to all this, and I must say the documentation for circuitPython is not making it easier to stick with it.

But i have not found a touchio (capacitive touch sensor) library compatible with the earlephilhower core.
Is there one?

It would be awesome to have this library also available for Arduino language!

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: touchio lib for Arduino

Post by mikeysklar »

Which capacitive touch sensor are you using (if any)? The MPR121 has its own library with Arduino examples.

https://learn.adafruit.com/adafruit-mpr ... ial/wiring

There are also capacitive touch boards that don't require a microcontroller.

https://learn.adafruit.com/adafruit-cap ... -breakouts

User avatar
fredrabelo
 
Posts: 6
Joined: Mon Nov 28, 2022 4:53 am

Re: touchio lib for Arduino

Post by fredrabelo »

Hi Mikey,

i am using a Alps Linear Potentiometer that has a "touch" contact.
https://tech.alpsalpine.com/e/products/ ... 0N11M9A0K/

The idea would be to use anything that conducts electricity to sensor the Proximity or touch.
This is working just fine with Circuitpython touchio lib.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: touchio lib for Arduino

Post by mikeysklar »

I see. You have a simple question which quickly spirals into craziness trying to make it work with the RP2040 + earlephilhower core.

Did you give PicoCapSense a try?

https://github.com/Gerriko/PicoCapSense

This discussion is over the top, but might provide some ideas.

https://community.element14.com/product ... -using-pio

User avatar
fredrabelo
 
Posts: 6
Joined: Mon Nov 28, 2022 4:53 am

Re: touchio lib for Arduino

Post by fredrabelo »

Thanks for the tip!

I saw both links before, and was trying to port my code to arduino using the mbed core but it didnt actually tryed to run the librarie with the earlephilhower core, beacause when i compile i get the message:
WARNING: library PicoCapSensing claims to run on mbed_rp2040 architecture(s) and may be incompatible with your current board which runs on rp2040 architecture(s).
i took a little bit to find out that the usb/serial connection only works with i import the <Adafruit_TinyUSB.h> before importing "PicoCapSensing.h".

for the the modified example from PicoCapSense is working! thanks a lot!

Here the code if it helps anyone:

Code: Select all

/*
 Copyright (c) 2022 C Gerrish (https://github.com/Gerriko)
 This example takes a rolling average to smooth out capacitance sampled values.

 It uses a capacitive Sensing library for RP2040 based boards (using the PIO processor).
 Capacitive Sensing is used to detect touch based on the electrical
 capacitance of the human body.

 This library is based on the measuring methods used in the
 Capacitive Sensing Library for 'duino / Wiring (sampling method differs)
 https://github.com/PaulStoffregen/CapacitiveSensor
 Copyright (c) 2009 Paul Bagder
 Updates for other hardare by Paul Stoffregen, 2010-2016

 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 BANNED FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.
 */



 #include <Adafruit_TinyUSB.h>

  #include "PicoCapSensing.h"

// define the pin numbers:
static const int TRIGPIN =  25;
static const int RECPIN =  29;
static const int LEDPIN = 13;

static const uint8_t MOVAVE_CNT = 3;      // Used to calculate a moving ave (needs to be > 0)
static const uint16_t TOUCHSENSETHRESHOLD = 80;

static long totalVal = 0L;
static int totalnow = 0;
static int totalprev = 0;
static int totalsmooth[6];
static uint8_t cntr = 0;
static bool MAcalc = false;

static uint8_t LEDstate = 0;

// This class will provide the PIO memory offset for the capsensing PIO (only need once)
PicoPIO capPicoPIO(pio0);
PicoCapSensing CapSensor(capPicoPIO, TRIGPIN, RECPIN);

void setup() {
  // ------------------------------- setup midi device ----------------------------------------------- //

    // (adafruit usb midi)
      // MIDI librarie standard
      #if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
        // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
        TinyUSB_Device_Init(0);
      #endif

      // wait until device mounted
      while( !TinyUSBDevice.mounted() ) delay(1);
  
  
  
  // put your setup code here, to run once:
  pinMode(LEDPIN, OUTPUT);

  Serial.begin(115200);
  while(!Serial) {;;}

  // Define the pins for

}


void loop() {
  totalnow = CapSensor.getCapSensingSample(2000, 30);
  if (totalnow) {
    if (totalprev) {
      if (cntr < MOVAVE_CNT) {
        if (abs(totalnow - totalprev) < TOUCHSENSETHRESHOLD) {
          if (totalnow > TOUCHSENSETHRESHOLD || totalprev > TOUCHSENSETHRESHOLD) {
            if (totalnow > totalprev) totalsmooth[cntr] = totalnow;
            else  totalsmooth[cntr] = totalprev;
          }
          else totalsmooth[cntr] = abs(totalnow - totalprev);
        }
        else totalsmooth[cntr] = abs(totalnow - totalprev);
        cntr++;
      }
      else {
        cntr = 0;
        if (!MAcalc) MAcalc = true;
      }
      if (MAcalc) {
        totalVal = 0;
        for (uint8_t i = 0; i < MOVAVE_CNT; i++) {
          totalVal += totalsmooth[i];
        }
        totalVal /= MOVAVE_CNT;
        Serial.print("0, ");              // sets the min value on y-axis
        Serial.print(totalVal);           // print smoothed output from capsensor output
        Serial.println(", 500");           // sets the max value on y-axis (although it can shift)

        if (totalVal > TOUCHSENSETHRESHOLD) {
          if (!LEDstate) {
            LEDstate = !LEDstate;
            digitalWrite(LEDPIN, LEDstate);
          }
        }
        else {
          if (LEDstate) {
            LEDstate = !LEDstate;
            digitalWrite(LEDPIN, LEDstate);
          }
        }
      }
    }
    totalprev = totalnow;
  }
  delay(50);                             // arbitrary delay to limit data to serial port

}

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: touchio lib for Arduino

Post by mikeysklar »

Cool. I love it when those long shot suggestions work.

Now the downside of PicoCapSense is that it is limited to a single pin.

User avatar
fredrabelo
 
Posts: 6
Joined: Mon Nov 28, 2022 4:53 am

Re: touchio lib for Arduino

Post by fredrabelo »

It says ongithub that it can do up to 3 sensors, but i didn't test that since I only need one.

Now I have to find a way to set the PWM freq "for dummies" hahahaha
Made a new post for that: viewtopic.php?t=196562
if you know any plz let me know :)

Thanks a lot for the help!!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”