Blake AtkinsonBlake Buchanan
Published © GPL3+

Roll Detection Using Accelerometer

Using an accelerometer, we detected angular acceleration. With this data we used a particle photon to notify the user via a Blynk dashboard.

IntermediateShowcase (no instructions)15 hours3,724
Roll Detection Using Accelerometer

Things used in this project

Hardware components

Photon
Particle Photon
Plan to use this to send accelerometer data to another particle so that it can be saved to a micro SSD.
×2
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Blynk
Blynk
Plan to use this to graph data

Story

Read more

Schematics

Photon and Accelerometer

Code

Accelerometer_Photon

C/C++
This code was used for the photon-accelerometer setup. It uses a library for the MPU6050 that was readily available for use. Slight modifications were made to utilize Blynk and check for a condition indicating the accelerometer has "rolled." The if statement that checks the condition of the accelerometer also publishes an event which the second photon is subscribed to.
// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"
//#define BLYNK_PRINT Serial
// This #include statement was automatically added by the Particle IDE.
#include "MPU6050/MPU6050.h"

// Once you import this library into an app on the web based IDE, modify the code to look something like the following.
// This code is a heavily modified version of the MPU6050_raw.ino example found elsewhere in this repo.
// This code has been tested against the MPU-9150 breakout board from Sparkfun.

// This #include statement was automatically added by the Spark IDE.
#include "MPU6050/MPU6050.h"

int ledPin = D7;
//float threshhold = 800;
// MPU variables:
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;

char auth[] = "a9f6f8d56817451fa19aab099d57c16b";

WidgetLED led(V1);
WidgetTerminal term(V2);

int i = 0;

bool ledState = false;
void toggleLed() {
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
}

void setup() {
    pinMode(ledPin, OUTPUT);

    Wire.begin();
    Serial.begin(9600);
    Blynk.begin(auth);
    
    // The following line will wait until you connect to the Spark.io using serial and hit        enter. This gives
    // you enough time to start capturing the data when you are ready instead of just            spewing data to the UART.
    //
    // So, open a serial connection using something like:
    // screen /dev/tty.usbmodem1411 9600
    while(!Serial.available()) SPARK_WLAN_Loop();
    
    //Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    
}

void loop() {
    
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    /*Serial.print("a/g:\t");
    Serial.print("x: "); Serial.print(ax); Serial.print("\t");
    Serial.print("y: "); Serial.print(ay); Serial.print("\t");
    Serial.print("z: "); Serial.print(az); Serial.print("\t");
    Serial.print("Pitch: "); Serial.print(gx); Serial.print("\t");
    Serial.print("Roll: "); Serial.print(gy); Serial.print("\t");
    Serial.print("Yaw: "); Serial.println(gz); */
    
    //toggleLed();
    
    
    if(gy > 0) {
        i++;
        Particle.publish("A_Unique_Event_252103", "On");
        //led.on();
        term.println("Danger");
        term.flush();
        Blynk.virtualWrite(V3, i);
        delay(500);
        
    }
    else {
        Particle.publish("A_Unique_Event_252103", "Off");
        //led.off();
        
    }
   
}

Photon_Subscribed

C/C++
Simply subscribes to events published by the photon-accelerometer configuration and writes a digital high/low depending on the conditions met.
int boardLed = D7;

void setup() {
    
    pinMode(boardLed, OUTPUT);
    Particle.subscribe("A_Unique_Event_252103", myHandler);

}

void loop() {
    

}

void myHandler(const char *event, const char *data) {
    
    if (strcmp(data,"Off")==0) {
    // Turn onboard led off
    digitalWrite(boardLed,LOW);
    delay(100);
    
  }
  else if (strcmp(data,"On")==0) {
    //If triggered by published event, i.e. if the accelerometer's roll exceeds a threshold, turn onboard led on
    digitalWrite(boardLed,HIGH);
    delay(500);
  }
  else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }

}

Credits

Blake Atkinson

Blake Atkinson

1 project • 4 followers
Mechanical Engineering Student at Charlotte with a concentration in Motorsports. Love video games, Formula SAE, and 3D CAD
Blake Buchanan

Blake Buchanan

1 project • 1 follower
Thanks to Arvind Sanjeev.

Comments