I am attempting to use an MMA8452 accelerometer (SparkFun Triple Axis Accelerometer Breakout - MMA8452Q - SEN-12756 - SparkFun Electronics) to measure G-force experienced. I would like it to have a green light when the g force is below a certain value(in this case 0.8g) and read when it is at or above 0.8g.
I have wired up the breakout board with 2 330 Ohm resistors between the scl and sda lines. and the board is supplied with 3.3V from the uno.
I am an absolute nube when it comes to navigating this website's forums to find examples of how to do this.
So far i have pieced a couple of example codes together to get this:
#include <Wire.h> // Must include Wire library for I2C
#include "SparkFun_MMA8452Q.h" // Click here to get the library: http://librarymanager/All#SparkFun_MMA8452Q
MMA8452Q accel; // create instance of the MMA8452 class
const int green=3;
const int red=5;
const string
void setup() {
Serial.begin(9600);
Serial.println("MMA8452Q Basic Reading Code!");
Wire.begin();
accel.init(); // Default init: +/-2g and 800Hz ODR
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
if (accel.begin() == false) {
Serial.println("Not Connected. Please check connections and read the hookup guide.");
while (1);
}
}
void loop() {
if (accel.available()) { // Wait for new data from accelerometer
// Acceleration of x, y, and z directions in g units
accel.read();
x = accel.cx;
y = accel.cy;
z = accel.cz;
Serial.print(x);
Serial.print(y);
Serial.print(z);
if ((accel.cx)>0.8){
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
}
else if ((accel.cy)>0.8){
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
}
else if ((accel.cz)>0.8){
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
}
else {
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
}
}
I have been playing with this for a couple days now. Honestly just need some direction at this point. I dont really know what the hell im doing here.
Code for accelerometer to activate LED. - Programming Questions - Arduino Forum just found this thread, now I just need to adapt it to mma8452 and my G limits. I have a sd card shield so I am going to attempt to incorporate that.