Skip to content

Instantly share code, notes, and snippets.

@iani
Created May 17, 2022 07:10
Show Gist options
  • Save iani/23c3bce34b64ebbb88f29656314e3ab7 to your computer and use it in GitHub Desktop.
Save iani/23c3bce34b64ebbb88f29656314e3ab7 to your computer and use it in GitHub Desktop.
Arduino uno accelerometer test: Read 6 raw integers from registers and write them to serial port
#include <Wire.h> // Used for I2C
// The SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
#define MMA8452_ADDRESS 0x1D // 0x1D if SA0 is high, 0x1C if low
//Define a few of the registers that we will be accessing on the MMA8452
#define OUT_X_MSB 0x01
#define XYZ_DATA_CFG 0x0E
#define WHO_AM_I 0x0D
#define CTRL_REG1 0x2A
#define GSCALE 2 // Sets full-scale range to +/-2, 4, or 8g. Used to calc real g values.
void setup()
{
Serial.begin(57600);
Serial.println("MMA8452 Basic Example");
Wire.begin(); //Join the bus as a master
initMMA8452(); //Test and intialize the MMA8452
}
void loop()
{
byte rawData[6]; // x/y/z accel register data stored here
readAccelData(accelCount); // Read the x/y/z adc values
readRegisters(OUT_X_MSB, 6, rawData); // Read the six raw data registers into data array
for (int i = 0; i < 6; i++) {
Serial.write(rawData[i])
};
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment