Trinket M0 integration with Bluefruit LE UART Friend

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Hello!

I am a propmaker currently designing electronics packages to go into props. This will mainly be used for controlling Neopixels. I am struggling to integrate the BLE UART Friend into my current setups, specifically in the coding area. I got the wiring all figured out but I am having lots of trouble configuring the BLE device to the Trinket M0. I have gone through the guides that Adafruit provides but I don't think it is simple enough for my limited coding experience. All I need is to be able to press a button on the app and it perform whatever preset LED code I have. Once again, my issues lie in connectivity, configuration, and coding of the bluetooth serial aspects themselves. I am very new to coding and don't have much depth of knowledge on this subject. In a perfect world, having code that works that I can just input my led code into would be great but any knowledge would be helpful.

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by adafruit_support_carter »

Take a look at the controller example:
https://learn.adafruit.com/introducing- ... controller
Try just running it as is first and see if you can get the button press messages from the control pad to show up:
https://learn.adafruit.com/introducing- ... le-2177253

Then try modifying the sketch to change the behavior to what you want. Here are the lines in the sketch that handle button presses:
https://github.com/adafruit/Adafruit_Bl ... #L197-L207

Can just replace the Serial.print lines with calls to a user function you've written. For example, put whatever this code is:
press a button on the app and it perform whatever preset LED code I have
in something called runLEDpreset() and then change code to:

Code: Select all

    if (pressed) {
      runLEDpreset();
    } else {
      // add code here if you want something to happen when released
    }

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Thank you so much for the response! I am currently going through your recommendations and trying them out. By chance, do you know how I should configure the bluetooth module with my Trinket M0 as far as the config files included in the sketches? (which lines to comment/uncomment, which pins on the board to which pins on the microcontroller). Also, I am currently getting an error when running any sketch that it couldn't factory reset while trying to as written in the sketch. Any idea why that is?

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by adafruit_support_carter »

Using hardware serial should work. For that, connect:

Trinket M0 #3 Serial1 RX to BLE UART TX
Trinket M0 #4 Serial1 TX to BLE UART RX

Pinout ref:
https://learn.adafruit.com/adafruit-tri ... no/pinouts

Then update the sketch to use the hardware serial setup by uncommenting:

Code: Select all

// Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);
and commenting out the SPI one used by default:

Code: Select all

Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
With that, don't think there are any updates needed to the BluefruitConfig.h file.

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Here's an update, I have my Trinket M0 connected to the BLE UART Friend as you described. Currently when I upload the code, it runs and then says it can't find the Bluefruit device. I have no idea what is going on. The main controller code and config code is posted below.

Controller Main Code:

Code: Select all

/*********************************************************************
 This is an example for our nRF51822 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

#include "BluefruitConfig.h"

#if SOFTWARE_SERIAL_AVAILABLE
  #include <SoftwareSerial.h>
#endif

/*=========================================================================
    APPLICATION SETTINGS

    FACTORYRESET_ENABLE       Perform a factory reset when running this sketch
   
                              Enabling this will put your Bluefruit LE module
                              in a 'known good' state and clear any config
                              data set in previous sketches or projects, so
                              running this at least once is a good idea.
   
                              When deploying your project, however, you will
                              want to disable factory reset by setting this
                              value to 0.  If you are making changes to your
                              Bluefruit LE device via AT commands, and those
                              changes aren't persisting across resets, this
                              is the reason why.  Factory reset will erase
                              the non-volatile memory where config data is
                              stored, setting it back to factory default
                              values.
       
                              Some sketches that require you to bond to a
                              central device (HID mouse, keyboard, etc.)
                              won't work at all with this feature enabled
                              since the factory reset will clear all of the
                              bonding data stored on the chip, meaning the
                              central device won't be able to reconnect.
    MINIMUM_FIRMWARE_VERSION  Minimum firmware version to have some new features
    MODE_LED_BEHAVIOUR        LED activity, valid options are
                              "DISABLE" or "MODE" or "BLEUART" or
                              "HWUART"  or "SPI"  or "MANUAL"
    -----------------------------------------------------------------------*/
    #define FACTORYRESET_ENABLE         1
    #define MINIMUM_FIRMWARE_VERSION    "0.6.6"
    #define MODE_LED_BEHAVIOUR          "MODE"
/*=========================================================================*/

// Create the bluefruit object, either software serial...uncomment these lines

//SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);

//Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
//                      BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);


/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
 Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);

/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
//                             BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
//                             BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);


// A small helper
void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}

// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);

// the packet buffer
extern uint8_t packetbuffer[];


/**************************************************************************/
/*!
    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
  while (!Serial);  // required for Flora & Micro
  delay(500);

  Serial.begin(115200);
  Serial.println(F("Adafruit Bluefruit App Controller Example"));
  Serial.println(F("-----------------------------------------"));

  /* Initialise the module */
  Serial.print(F("Initialising the Bluefruit LE module: "));

  if ( !ble.begin(VERBOSE_MODE) )
  {
    error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  }
  Serial.println( F("OK!") );

  if ( FACTORYRESET_ENABLE )
  {
    /* Perform a factory reset to make sure everything is in a known state */
    Serial.println(F("Performing a factory reset: "));
    if ( ! ble.factoryReset() ){
      error(F("Couldn't factory reset"));
    }
  }


  /* Disable command echo from Bluefruit */
  ble.echo(false);

  Serial.println("Requesting Bluefruit info:");
  /* Print Bluefruit information */
  ble.info();

  Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
  Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
  Serial.println();

  ble.verbose(false);  // debug info is a little annoying after this point!

  /* Wait for connection */
  while (! ble.isConnected()) {
      delay(500);
  }

  Serial.println(F("******************************"));

  // LED Activity command is only supported from 0.6.6
  if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
  {
    // Change Mode LED Activity
    Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
    ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
  }

  // Set Bluefruit to DATA mode
  Serial.println( F("Switching to DATA mode!") );
  ble.setMode(BLUEFRUIT_MODE_DATA);

  Serial.println(F("******************************"));

}

/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
  /* Wait for new data to arrive */
  uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
  if (len == 0) return;

  /* Got a packet! */
  // printHex(packetbuffer, len);

  // Color
  if (packetbuffer[1] == 'C') {
    uint8_t red = packetbuffer[2];
    uint8_t green = packetbuffer[3];
    uint8_t blue = packetbuffer[4];
    Serial.print ("RGB #");
    if (red < 0x10) Serial.print("0");
    Serial.print(red, HEX);
    if (green < 0x10) Serial.print("0");
    Serial.print(green, HEX);
    if (blue < 0x10) Serial.print("0");
    Serial.println(blue, HEX);
  }

  // Buttons
  if (packetbuffer[1] == 'B') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';
    Serial.print ("Button "); Serial.print(buttnum);
    if (pressed) {
      Serial.println(" pressed");
    } else {
      Serial.println(" released");
    }
  }

  // GPS Location
  if (packetbuffer[1] == 'L') {
    float lat, lon, alt;
    lat = parsefloat(packetbuffer+2);
    lon = parsefloat(packetbuffer+6);
    alt = parsefloat(packetbuffer+10);
    Serial.print("GPS Location\t");
    Serial.print("Lat: "); Serial.print(lat, 4); // 4 digits of precision!
    Serial.print('\t');
    Serial.print("Lon: "); Serial.print(lon, 4); // 4 digits of precision!
    Serial.print('\t');
    Serial.print(alt, 4); Serial.println(" meters");
  }

  // Accelerometer
  if (packetbuffer[1] == 'A') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Accel\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Magnetometer
  if (packetbuffer[1] == 'M') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Mag\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Gyroscope
  if (packetbuffer[1] == 'G') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Gyro\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Quaternions
  if (packetbuffer[1] == 'Q') {
    float x, y, z, w;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    w = parsefloat(packetbuffer+14);
    Serial.print("Quat\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.print('\t');
    Serial.print(w); Serial.println();
  }
}
Config Code:

Code: Select all

// COMMON SETTINGS
// ----------------------------------------------------------------------------------------------
// These settings are used in both SW UART, HW UART and SPI mode
// ----------------------------------------------------------------------------------------------
#define BUFSIZE                        128   // Size of the read buffer for incoming data
#define VERBOSE_MODE                   true  // If set to 'true' enables debug output
#define BLE_READPACKET_TIMEOUT         500   // Timeout in ms waiting to read a response


// SOFTWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins that will be used for 'SW' serial.
// You should use this option if you are connecting the UART Friend to an UNO
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SWUART_RXD_PIN       9    // Required for software serial!
#define BLUEFRUIT_SWUART_TXD_PIN       10   // Required for software serial!
#define BLUEFRUIT_UART_CTS_PIN         11   // Required for software serial!
#define BLUEFRUIT_UART_RTS_PIN         -1   // Optional, set to -1 if unused


// HARDWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the HW serial port you are using. Uncomment
// this line if you are connecting the BLE to Leonardo/Micro or Flora
// ----------------------------------------------------------------------------------------------
#ifdef Serial1    // this makes it not complain on compilation if there's no Serial1
  #define BLUEFRUIT_HWSERIAL_NAME      Serial1
#endif


// SHARED UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following sets the optional Mode pin, its recommended but not required
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_UART_MODE_PIN        12    // Set to -1 if unused


// SHARED SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for HW and SW SPI communication.
// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when
// using HW SPI.  This should be used with nRF51822 based Bluefruit LE modules
// that use SPI (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_CS               8
#define BLUEFRUIT_SPI_IRQ              7
#define BLUEFRUIT_SPI_RST              4    // Optional but recommended, set to -1 if unused

// SOFTWARE SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for SW SPI communication.
// This should be used with nRF51822 based Bluefruit LE modules that use SPI
// (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_SCK              13
#define BLUEFRUIT_SPI_MISO             12
#define BLUEFRUIT_SPI_MOSI             11

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Also, it isn't automatically recognizing my board as a Trinket M0, instead it detects it as a pIRkey.

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by adafruit_support_carter »

Please post a photo of your setup showing how everything is connected.

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Here is the image
Attachments
IMG_7662.jpg
IMG_7662.jpg (615.47 KiB) Viewed 257 times

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by adafruit_support_carter »

Thanks for the photo. That looks good.

Sorry - looks like I forgot about one other required pin. Try also connecting the BLE UARTs CTS pin to GND.

See general info on this in pinout page here:
https://learn.adafruit.com/introducing- ... ns-2346387
This pin is pulled high by default and must be set to ground in order to enable data transfer out! If you do not need hardware flow control, tie this pin to ground
I hooked one of these up to a Trinket M0 to verify the setup would work. Same connections you are using plus the addition of CTS to GND. I also had to explicitly specify Serial1 for some reason. So my line in the sketch that creates the ble object looks like this:

Code: Select all

Adafruit_BluefruitLE_UART ble(Serial1, -1);
With that, the sketch runs as expected, showing this output in the Serial Monitor:

Code: Select all

Adafruit Bluefruit App Controller Example
-----------------------------------------
Initialising the Bluefruit LE module: ATZ

<- OK
OK!
Performing a factory reset: 
AT+FACTORYRESET

<- OK
ATE=0

<- ATE=0
OK
Requesting Bluefruit info:
----------------
BLEFRIEND32
nRF51822 QFACA00
B71CB879C90C6818
0.6.2
0.6.2
Apr 30 2015
S110 8.0.0, 0.2
----------------
Please use Adafruit Bluefruit LE app to connect in Controller mode
Then activate/use the sensors, color picker, game controller, etc!

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Alright sweet! That worked, thank you so much!

I am not seeing any of the data or button presses get sent over to the serial monitor in the Arduino IDE. I should be seeing the sent information, correct?

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by adafruit_support_carter »

Correct. Here's more serial output. Connected with the BLE app, opened the Control Pad interface of the app, and pressed some random buttons (see bottom of serial output):

Code: Select all

Adafruit Bluefruit App Controller Example
-----------------------------------------
Initialising the Bluefruit LE module: ATZ

<- OK
OK!
Performing a factory reset: 
AT+FACTORYRESET

<- OK
ATE=0

<- ATE=0
OK
Requesting Bluefruit info:
----------------
BLEFRIEND32
nRF51822 QFACA00
B71CB879C90C6818
0.8.1
0.8.1
Apr 10 2019
S110 8.0.0, 0.2
----------------
Please use Adafruit Bluefruit LE app to connect in Controller mode
Then activate/use the sensors, color picker, game controller, etc!

******************************
Change LED activity to MODE
Switching to DATA mode!
******************************
Button 7 pressed
Button 7 released
Button 1 pressed
Button 1 released
Button 4 pressed
Button 4 released
Button 6 pressed
Button 6 released

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Yeah, I am not seeing any button presses or anything for some reason

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by adafruit_support_carter »

Did the app seem to find the device and connect to it OK?

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

Nevermind, I changed my UART_MODE pin to -1 instead of 12 and that seemed to fix the issue.

User avatar
APSIProps
 
Posts: 83
Joined: Mon Oct 10, 2022 1:52 pm

Re: Trinket M0 integration with Bluefruit LE UART Friend

Post by APSIProps »

How would I go about changing the code so I can assign different NeoPixel patterns and code to different button presses in the Bluefruit iOS app?

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

Return to “Wireless: WiFi and Bluetooth”