Before loading Firmata BLE...

Make sure you have the basic tutorials working, that you can send/receive data via the UART with your wiring. If you can't talk to the module and send/receive data via the basic UART demo, Firmata won't work!

Install Libraries

You'll want to start by installing the BLE Firmata library. Use the Library manager to find Adafruit BLEFirmata and install it

Open Sketch and Configure

Restart the IDE and load the Adafruit_BLE_PinIO->BluefruitLE_nrf51822 sketch

At the top of the sketch is the Firmata configuration section, there's also Bluefruit LE pin configuration in the BluefruitConfig.h tab

Bluefruit LE Config

Start by opening the BluefruitConfig.h tab. By default the sketch is set up for hardware SPI and CS = 8, IRQ = 7 and RST = 4. This works by default for our Bluefruit Feathers.

You'll need to change your pins if you are using UART or different SPI pins.

Check out the generic "Configuration!" details for information on what every pin does. If you're using software serial, be sure to set up flow control and a MODE pin.

Then in the main sketch, if you're not using hardware SPI, uncomment the connection style you want

// 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(BLUEFRUIT_HWSERIAL_NAME, 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);

Firmata Debug Config

Next up in the main sketch there's a few settings you'll have to configure. First up is debug and serial details

// Change this to whatever is the Serial console you want, either Serial or SerialUSB
#define FIRMATADEBUG    Serial
// Pause for Serial console before beginning?
#define WAITFORSERIAL   true
// Print all BLE interactions?
#define VERBOSE_MODE    false

The first setting FIRMATADEBUG is how output is printed. 99% of the time you'll be happy with Serial, but for Arduino Zeros you may need to use SerialUSB for the native port.

WAITFORSERIAL determines whether the sketch waits for the Serial port to be opened before it runs. Set it to true while debugging/testing and open up the serial console to kick off the sketch. Set to false once it's working great

VERBOSE_MODE allows you to see all of the data passing between the BLE module and App.

Available Pins Config

Below the debug config is where you can set up what pins are available for the Pin IO app to twiddle

/************** For Bluefruit Micro or Feather 32u4 Bluefruit ************/
//uint8_t boards_digitaliopins[] = {0,1,2,3,5,6,9,10,11,12,13,A0,A1,A2,A3,A4,A5};

/************** For UNO + nRF58122 SPI & shield ************/
//uint8_t boards_digitaliopins[] = {2, 3, 5, 6, 9, 10, A0, A1, A2, A3, A4, A5}; 

/************** For Bluefruit M0 Bluefruit ************/
//uint8_t boards_digitaliopins[] = {0,1,5,6,9,10,11,12,13,20,21,A0,A1,A2,A3,A4,A5};

You'll need to uncomment one of these lines. Also, they are set up by default for the SPI Bluefruit module, on pins 4, 7, 8. For that reason, the hardware SPI pins and 4,7,8 don't appear. If you are using different pins you can re-add those to the list. If there are any other pins you don't want to show up in the app, remove those pins as well.

For the 32u4 and M0 examples, there's a lot of pins, & perhaps not all of them are necessary! You can have as few as you like.

Below the setup lines you can see the way we tell Firmata which pins do what

#if defined(__AVR_ATmega328P__) 
  // Standard setup for UNO, no need to tweak
  uint8_t boards_analogiopins[] = {A0, A1, A2, A3, A4, A5};  // A0 == digital 14, etc
  uint8_t boards_pwmpins[] = {3, 5, 6, 9, 10, 11};
  uint8_t boards_servopins[] = {9, 10};
  uint8_t boards_i2cpins[] = {SDA, SCL};

Don't mess with these! Change only the digital IO pins array!

Upload and test

Once you're done compile and upload. Open up the serial console. You should see that the sketch was able to initialize the Bluefruit LE module, reset it and print out some details about the BLE firmware. It will now wait for the app to connect

If you dont see anything, check:

  • If you're using an ATSAMD21/M0 chip, do you have FIRMATADEBUG  set to SerialUSB?
  • Do you have WAITFORSERIAL true?
Once you know everything is working, set "#define WAITFORSERIAL true" to false so you don't have to open the serial console to have the Firmata code run!

Once you connect in Pin IO mode you can see a stream of commands that are received and acted upon

This guide was first published on Feb 20, 2015. It was last updated on Mar 08, 2024.

This page (Library and Config) was last updated on Mar 08, 2024.

Text editor powered by tinymce.