• Trill
  • Setting analog in pins for Trill Square with Pi Pico (using Arduino IDE)

Hi folks,

Has anyone here tried the Bela Square hooked up to the Pi Pico, programmed using the Arduino IDE? From looking at the diagram on https://learn.bela.io/using-trill/trill-and-arduino/ the sensor is hooked up to analog pins 4 and 5. The Pi Pico has just 3 accessible analog ins. Is there a way to define the input pins in code? Many thanks.

    kieran the sensor is hooked up to analog pins 4 and 5.

    Nomenclature of Arduino pins is sometimes confusing. Pins may be labelled as "analog" but actually used for some digital signal in a specific application. In our case we are using the I2C bus's SCL and SDA lines, so on your board you'll need to find what pins these signals are available on.

    Is this the board you are using?

    https://www.raspberrypi.com/documentation/microcontrollers/images/pico-pinout.svg

    From what I can see there, you can access the I2C1 and I2C0 SDA and SCL signals on several pins. As the Trill library uses the Wire.h under the hood, you'll have to refer to that to see what pins are used by default and whether that can be changed. This page claims that pins can be changed but it doesn't say which ones are the default ones. We are using Wire.h (and not Wire1.h), so that should mean we are using the I2C0 bus. Besides that, the documentation doesn't state the meaning of the pin number argument to the setSDA() and setSCL() calls ... and this other page doesn't make things much clearer, except one can infer that it's the number of the GPx pin. So for instance to use I2C0_SDA on pin 1 (GP0) and I2C1_SCL on pin 2 ( GP1), you probably need to call within your setup() function:

    setSDA(0);
    setSCL(1);

    before you call .begin() on the Trill object.
    Or it could be that you should call

    Wire.setSDA(0);
    Wire.setSCL(1);

    instead ... or yet something else.

    I don't have a Raspbery Pi Pico, but on the Arduino 2 IDE I followed the instructions here to install the board files that the above "documentation" refers to and it would seem that the second one works (i.e.: it builds without errors).

    E.g.:

    #include <Trill.h>
    #include <Wire.h>
    
    Trill trill;
    void setup() {
      // put your setup code here, to run once:
      Wire.setSDA(0); 
      Wire.setSCL(1);
      trill.begin(Trill::TRILL_BAR);
      ...
    }
    2 years later

    Did this ever get a definitive answer? @kieran did you work it out?

    Now that I tested it, the suggestion I gave above works a charm with the Arduino IDE. Just adde those two lines Wire.setSDA(0); Wire.setSCL(1); to a stock example before calling begin() on the Trill object. Here's an aptly modified square-print example and it prints what it should. Full code:

    /*
     ____  _____ _        _
    | __ )| ____| |      / \
    |  _ \|  _| | |     / _ \
    | |_) | |___| |___ / ___ \
    |____/|_____|_____/_/   \_\
    http://bela.io
    
    \example square-print
    
    Trill Square Print
    ==================
    
    This is an example of how to communicate with the Trill Square
    sensor using the Trill Arduino library.
    
    The sensor is set to Centroid mode and 2D touch location and size
    printed to the serial port.
    
    You can find our Processing library for visualising here:
    https://github.com/BelaPlatform/trill-processing-library/
    The accompanying Processing sketch, `TrillSquare.pde`, listens for
    touch information on the Arduino serial port and displays it in a
    render of a Trill Square.
    
    You may need to update the Processing port number (gPortNumber)
    to match that of your Arduino and set `verbose = false` below in order to
    make the serial data parseable by the Processing sketch.
    */
    
    #include <Trill.h>
    
    Trill trillSensor;
    boolean touchActive = false;
    const boolean verbose = true; // set this to false to communicate to the Processing GUI via UART.
    
    void setup() {
      Serial.begin(115200);
      Wire.setSDA(0);
      Wire.setSCL(1);
      // Initialise serial and touch sensor
      int ret = trillSensor.setup(Trill::TRILL_SQUARE);
      while(ret != 0) {
        Serial.println("failed to initialise trillSensor");
        Serial.print("Error code: ");
        Serial.println(ret);
      }
      if(verbose)
        Serial.println("Success initialising trillSensor");
    }
    
    void loop() {
      // Read 20 times per second
      delay(50);
      trillSensor.read();
    
      if(trillSensor.getNumTouches() > 0 || trillSensor.getNumHorizontalTouches() > 0) {
        if(verbose)
          Serial.print("V[");
        Serial.print(trillSensor.getNumTouches());
        if(verbose) {
          Serial.print("] ");
          Serial.print("H[");
        }
        else
          Serial.print(" ");
        Serial.print(trillSensor.getNumHorizontalTouches());
        if(verbose)
          Serial.print("] ");
        else
          Serial.print(" ");
    
        for(int i = 0; i < trillSensor.getNumTouches(); i++) {
            Serial.print(trillSensor.touchLocation(i));
            Serial.print(" ");
            Serial.print(trillSensor.touchSize(i));
            Serial.print(" ");
        }
        for(int i = 0; i < trillSensor.getNumHorizontalTouches(); i++) {
            Serial.print(trillSensor.touchHorizontalLocation(i));
            Serial.print(" ");
            Serial.print(trillSensor.touchHorizontalSize(i));
            Serial.print(" ");
        }
    
        Serial.println("");
        touchActive = true;
      }
      else if(touchActive) {
        // Print a single line when touch goes off
        if(verbose)
          Serial.println("V[0] H[0]");
        else
          Serial.println("0 0");
        touchActive = false;
      }
    }

    I am using this board:

    set up as explained here: https://randomnerdtutorials.com/programming-raspberry-pi-pico-w-arduino-ide/

    Crikey, that really didn't take very long did it?

    Well the solution was posted three years ago... so it took negative time.