Interfacing Adafruit Bluefruit LE SPI Friend with Arduino

2,077 views

Introduction

Are you ready to take your Arduino projects to the next level? If so, the Adafruit Bluefruit LE SPI Friend is the perfect device! This handy little device allows you to easily add Bluetooth Low Energy (BLE) connectivity to your Arduino projects, enabling you to communicate with your projects wirelessly from your smartphone or tablet.

With the Adafruit Bluefruit LE SPI Friend, you can easily send and receive data over Bluetooth, allowing you to create all sorts of interactive and engaging projects. Whether you want to create a remote-controlled robot, a gesture-based controller, or add some wireless functionality to an existing project, the Adafruit Bluefruit LE SPI Friend has you covered.

In this blog post, we’ll take an in-depth look at the Adafruit Bluefruit LE SPI Friend, exploring its features, capabilities, and how to use it with your Arduino projects. So if you’re ready to dive into wireless Arduino projects, let’s get started!

What is Adafruit Bluefruit LE SPI Friend?

Adafruit Bluefruit LE SPI Friend is a Bluetooth Low Energy (BLE) board that allows you to connect your project to your smartphone, computer, or tablet. It can be used to control a variety of electronics, such as LEDs, motors, and sensors, and it also has a built-in UART for serial communication. It’s an easy-to-use and powerful tool for creating all kinds of wireless projects.

Adafruit Bluefruit LE SPI Friend

Hardware Components

You will require the following hardware for Interfacing Adafruit Bluefruit LE SPI Friend with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.ADAFRUIT BLUEFRUIT LE SPI FRIEND1
3.Breadboard1
4.Jumper Wires1

Adafruit Bluefruit LE SPI Friend with Arduino

Here are some steps to help you interface Adafruit Bluefruit LE SPI Friend with Arduino:

Schematic

Make connections according to the circuit diagram given below.

Adafruit Bluefruit LE SPI Friend Arduino Circuit Schematic

Wiring / Connections

ArduinoSensor
5VVCC
GNDGND
D4RST
D7CS
D8IRQ
D11MOSI
D12MISO
D13SCK

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

    /*********************************************************************
 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 <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. 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 and 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);
}

/**************************************************************************/
/*!
    @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 Command Mode 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 UART mode"));
  Serial.println(F("Then Enter characters to send to Bluefruit"));
  Serial.println();

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

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

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

/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
  // Check for user input
  char inputs[BUFSIZE+1];

  if ( getUserInput(inputs, BUFSIZE) )
  {
    // Send characters to Bluefruit
    Serial.print("[Send] ");
    Serial.println(inputs);

    ble.print("AT+BLEUARTTX=");
    ble.println(inputs);

    // check response stastus
    if (! ble.waitForOK() ) {
      Serial.println(F("Failed to send?"));
    }
  }

  // Check for incoming characters from Bluefruit
  ble.println("AT+BLEUARTRX");
  ble.readline();
  if (strcmp(ble.buffer, "OK") == 0) {
    // no data
    return;
  }
  // Some data was found, its in the buffer
  Serial.print(F("[Recv] ")); Serial.println(ble.buffer);
  ble.waitForOK();
}

/**************************************************************************/
/*!
    @brief  Checks for user input (via the Serial Monitor)
*/
/**************************************************************************/
bool getUserInput(char buffer[], uint8_t maxSize)
{
  // timeout in 100 milliseconds
  TimeoutTimer timeout(100);

  memset(buffer, 0, maxSize);
  while( (!Serial.available()) && !timeout.expired() ) { delay(1); }

  if ( timeout.expired() ) return false;

  delay(2);
  uint8_t count=0;
  do
  {
    count += Serial.readBytes(buffer+count, maxSize);
    delay(2);
  } while( (count < maxSize) && (Serial.available()) );

  return true;
}

Let’s Test It

It’s now time to test the circuit! Once you upload the code, the program does the following things:

  • Wait for a serial connection
  • Initialize Bluefruit module
  • Perform factory reset if the flag is set
  • Wait for a BLE connection
  • check the firmware version
  • Change the LED behavior on the module
  • Wait for user input on the serial monitor and send it to the module over BLE.
  • Check if there is any incoming data from the BLE module and print it on the serial monitor if found.

Working Explanation

The above code is an example of using the Adafruit Bluefruit LE module in command mode, which allows the module to be controlled through serial commands and respond with status information.

It starts by defining some constants and a couple of functions:

  • FACTORYRESET_ENABLE: a flag that controls whether the module should be factory reset before being configured.
  • MINIMUM_FIRMWARE_VERSION: the minimum firmware version required for the module to support certain features.
  • MODE_LED_BEHAVIOUR: this is used to change the behavior of the mode LED on the Bluefruit module
  • error(const __FlashStringHelper*err) is a function that takes an error message as input, displays it on the serial monitor, and then enters an infinite loop to prevent the program from continuing.
  • Setup () function is called once when the program starts. It starts by initializing the serial communication and waiting for a serial connection. It then initializes the Bluefruit module; it checks if the module is found, and if not, it will call the error function to stop the program. If the module is found, it performs a factory reset if the FACTORYRESET_ENABLE flag is true.
  • The loop () function is called repeatedly after the setup() function has been called. It waits for user input and sends the input data to the Bluefruit module. Then it checks if there is any incoming data from the module; if there is, it will print it on the serial monitor.
  • getUserInput() function is called to check for any input coming from the serial monitor; it waits for input and returns true if the information is received. Otherwise, it will return false.

Applications

  • Provides wireless communication capabilities to different projects.

Conclusion.

We hope you have found this Interfacing Adafruit Bluefruit LE SPI Friend with Arduino Circuit very useful. If you feel any difficulty making it feel free to ask anything in the comment section.