Skip to content

Commit

Permalink
Added Arduino example sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
SFE-Jordan committed Jul 15, 2014
1 parent 5a53007 commit ec72c64
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions firmware/ADXL377_example/ADXL377_example.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/******************************************************************************
ADXL377_example.ino
Simple example for using the ADXL377 accelerometer breakout.
Jordan McConnell @ SparkFun Electronics
3 July 2014
https://github.com/sparkfun/ADXL377_Breakout
This example collects raw accelerometer data from the ADXL377 sensor using
analog reads and then converts it into common units (g's) and prints the
result to the Serial Monitor.
Developed/Tested with:
Arduino Uno
Arduino IDE 1.0.5
This code is beerware.
Distributed as-is; no warranty is given.
******************************************************************************/

// Make sure these two variables are correct for your setup
int scale = 200; // 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
boolean micro_is_5V = true; // Set to true if using a 5V microcontroller such as the Arduino Uno, false if using a 3.3V microcontroller, this affects the interpretation of the sensor data

void setup()
{
// Initialize serial communication at 115200 baud
Serial.begin(115200);
}

// Read, scale, and print accelerometer data
void loop()
{
// Get raw accelerometer data for each axis
int rawX = analogRead(A0);
int rawY = analogRead(A1);
int rawZ = analogRead(A2);

// Scale accelerometer ADC readings into common units
// Scale map depends on if using a 5V or 3.3V microcontroller
float scaledX, scaledY, scaledZ; // Scaled values for each axis
if (micro_is_5V) // Microcontroller runs off 5V
{
scaledX = mapf(rawX, 0, 675, -scale, scale); // 3.3/5 * 1023 =~ 675
scaledY = mapf(rawY, 0, 675, -scale, scale);
scaledZ = mapf(rawZ, 0, 675, -scale, scale);
}
else // Microcontroller runs off 3.3V
{
scaledX = mapf(rawX, 0, 1023, -scale, scale);
scaledY = mapf(rawY, 0, 1023, -scale, scale);
scaledZ = mapf(rawZ, 0, 1023, -scale, scale);
}

// Print out raw X,Y,Z accelerometer readings
Serial.print("X: "); Serial.println(rawX);
Serial.print("Y: "); Serial.println(rawY);
Serial.print("Z: "); Serial.println(rawZ);
Serial.println();

// Print out scaled X,Y,Z accelerometer readings
Serial.print("X: "); Serial.print(scaledX); Serial.println(" g");
Serial.print("Y: "); Serial.print(scaledY); Serial.println(" g");
Serial.print("Z: "); Serial.print(scaledZ); Serial.println(" g");
Serial.println();

delay(2000); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
}

// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

0 comments on commit ec72c64

Please sign in to comment.