Tom Moxon
Published © GPL3+

Arduino101 Bluetooth Intertial Measurement Unit (IMU)

Use the Arduino101 Bluetooth wireless interface to read the Intertial Measurement Unit (IMU) with your phone or tablet using Blynk !

IntermediateFull instructions provided1 hour1,300
Arduino101 Bluetooth Intertial Measurement Unit (IMU)

Things used in this project

Hardware components

Arduino 101 & Genuino 101
Arduino 101 & Genuino 101
×1
Arduino UNO Click Shield
MikroElektronika Arduino UNO Click Shield
×1
RELAY click
MikroElektronika RELAY click
×1

Software apps and online services

Arduino IDE
Arduino IDE
Blynk
Blynk

Story

Read more

Schematics

Arduino_101_BLE_Relayer Presentation

Presentor's Slides

Code

Arduino_101_BLE_IMU

C/C++
An Arduino101 sketch to send IMU data over a bluetooth connection
/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  Note: This requires CurieBLE library
    from http://librarymanager/all#CurieBLE

  Warning: Bluetooth support is in beta!
 *************************************************************/

// comment the next line to disable Serial Output
//#define SERIAL_DEBUG 

#ifdef SERIAL_DEBUG
#define BLYNK_PRINT Serial
#endif

#include <BlynkSimpleCurieBLE.h>
#include <CurieBLE.h>
#include <CurieIMU.h>
#include <SimpleTimer.h>
#include <TimeLib.h>

int ax, ay, az;           // accelerometer values
int gx, gy, gz;           // gyrometer values
int calibrateOffsets = 1; // determine whether calibration takes place or not

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

BLEPeripheral  blePeripheral;

// A timer instance to avoid flooding the Blynk Server
SimpleTimer timer;

int relay1_pin = 6;
int relay2_pin = 10;
int relay3_pin = 5;
int relay4_pin = 9;
int led_pin    = 13;
int blinkState = 0;

BLYNK_WRITE(V1) {
  int pinData = param.asInt();
  digitalWrite(relay1_pin, pinData);
}

BLYNK_WRITE(V2) {
  int pinData = param.asInt();
  digitalWrite(relay2_pin, pinData);
}

BLYNK_WRITE(V3) {
  int pinData = param.asInt();
  digitalWrite(relay3_pin, pinData);
}

BLYNK_WRITE(V4) {
  int pinData = param.asInt();
  digitalWrite(relay4_pin, pinData);
}

/* we could also do it this way with "Pull"
BLYNK_READ(V5) {
  Blynk.virtualWrite(5, millis() / 1000);
}
*/

void timerService()
{
  // update the IMU data  
  // read raw accel/gyro measurements from device
  CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz);
  // We'll use "PUSH" at 1 sec intervals
  Blynk.virtualWrite(5,ax);  // V5
  Blynk.virtualWrite(6,ay);  // V6
  Blynk.virtualWrite(7,az);  // V7
  Blynk.virtualWrite(8,gx);  // V8
  Blynk.virtualWrite(9,gy);  // V9
  Blynk.virtualWrite(10,gz); // V10  

  #ifdef SERIAL_DEBUG
  // display tab-separated accel/gyro x/y/z values
  Serial.print("a/g:\t");
  Serial.print(ax);
  Serial.print("\t");
  Serial.print(ay);
  Serial.print("\t");
  Serial.print(az);
  Serial.print("\t");
  Serial.print(gx);
  Serial.print("\t");
  Serial.print(gy);
  Serial.print("\t");
  Serial.println(gz);
  #endif
  
// blink LED to indicate activity
  blinkState = !blinkState;
  digitalWrite(led_pin, blinkState);
}

void setup()
{
  #ifdef SERIAL_DEBUG
  // Debug console
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  #endif
  
  // initialize the output (GPIO) pins
  pinMode(led_pin, OUTPUT);
  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);
  pinMode(relay3_pin, OUTPUT);
  pinMode(relay4_pin, OUTPUT);
  digitalWrite(led_pin, 0);    // V0
  digitalWrite(relay1_pin, 0); // V1
  digitalWrite(relay2_pin, 0); // V2
  digitalWrite(relay3_pin, 0); // V3
  digitalWrite(relay4_pin, 0); // V4

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html
  // Update timerService every 1.0 second
  // we do this to avoid flooding the connection 
  // (i.e. the Blynk server) with too much data
  timer.setInterval(1000L, timerService);
  
  // start the Accel/Gyro/IMU
  CurieIMU.begin();
  delay(2000);

  #ifdef SERIAL_DEBUG
  Serial.print("Starting Gyroscope calibration and enabling offset compensation...");
  #endif
  
  CurieIMU.autoCalibrateGyroOffset();
  
  #ifdef SERIAL_DEBUG
  Serial.println(" Done with Gyroscope calibration");
  Serial.print("Starting Acceleration calibration and enabling offset compensation...");
  #endif
  
  CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
  CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
  CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
  
  #ifdef SERIAL_DEBUG
  Serial.println(" Done with Acceleration calibration");
  #endif
  
  // Set the accelerometer range to 250 degrees/second
  CurieIMU.setGyroRange(250);
  // Set the accelerometer range to 2G
  CurieIMU.setAccelerometerRange(2);
  
  blePeripheral.setLocalName("hack01");
  blePeripheral.setDeviceName("hack01");
  blePeripheral.setAppearance(384);

  Blynk.begin(blePeripheral, auth);

  blePeripheral.begin();
  #ifdef SERIAL_DEBUG
  Serial.println("Waiting for BTLE connection...");
  #endif
}

void loop()
{
  // nice and simple...
  Blynk.run();
  blePeripheral.poll();
  timer.run();
}

Credits

Tom Moxon

Tom Moxon

4 projects • 31 followers
Chip Designer, Embedded Hardware and Software Design

Comments

Add projectSign up / Login