ADXL337 and ADXL377 Accelerometer Hookup Guide

Pages
Contributors: JordanDee
Favorited Favorite 11

Example Code

Now that your accelerometer breakout is electrically connected to your Arduino, it's time to dive into the code. The full example sketch can be found in the github repository for either the ADXL337 or the ADXL377. The code is the same with the exception of the value for the scale variable.

The first two lines of code in the sketch setup configuration variables.

language:c
int scale = 3;
boolean micro_is_5V = true; 

The variable scale is set to the full scale of the accelerometer measured in g forces. It is set to 3 for the ADXL337 and set to 200 for the ADXL377, since the sensors measure a ±3g and ±200g range respectively. The variable micro_is_5V is set to true if using a 5V microcontroller such as the Arduino Uno, or false if using a 3.3V microcontroller. We want to know this because it affects the interpretation of the sensor data we will read later on.

Next we use the setup() function in initialize serial communication so that we can later print sensor data to the Serial Monitor.

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

Using the loop() function, we collect the sensor data, scale it to the appropriate units measured in g forces, and print both the raw and scaled data to the Serial Monitor. First, let's look at how we read the sensor data.

language:c
void loop()
{
  // Get raw accelerometer data for each axis
  int rawX = analogRead(A0);
  int rawY = analogRead(A1);
  int rawZ = analogRead(A2);

We use the analog inputs A0, A1, and A2 on the Arduino and a few analog reads to get a number between 0 and 1023 that corresponds to the voltage on those pins. Those voltages reflect the latest acceleration measurement from the sensor. As an example, if the ADXL337 is measuring 3.3V on the X pin, this means it is measuring +3g's on the X axis. This in turn is measured by the microcontroller. If you're using a 3.3V microcontroller, the analog read will return 1023 and store that value in the variable rawX. If you're using a 5V microcontroller, the analog read will return 675 and store that value in that same variable instead. That's why it's important to set the variable micro_is_5V correctly so we know how to interpret these raw values.

Knowing the microcontroller's voltage, will allow us to scale these integers into readable units measured in g forces correctly. Here's how we scale these values into more meaningful units.

language:c
 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
  }
  else // microcontroller runs off 3.3V
  {
    scaledX = mapf(rawX, 0, 1023, -scale, scale);
  }

We first declare the scaled variables as floats, since we want decimal places. We then check whether the microcontroller is running off of 5V or 3.3V with the boolean micro_is_5V. Based on that we scale the raw integer value of x, rawX, into a decimal value measured in g forces, called scaledX using a mapping function. We also do this for the Y and Z axis however I left those out above since they follow the exact same process. Remember that the 675 came from the fact that a 5V Arduino will measure 3.3V as 675, while a 3.3V Arduino will measure 3.3V as 1023.

The mapf() function exists in the sketch and works exactly the same as Arduino standard map() function, which you can reference here. The reason I didn't use the standard map was because it deals with integers only, and, for our purposes, we need decimal places. Thus, I essentially rewrote the exact same function using floats instead.

After scaling, we print both the raw and scaled data to the Serial Monitor. You probably only care to view the scaled data unless your debugging, however I left both there so you can compare. Here's how to print the raw and scaled data for each axis:

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

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

This allows you to see the data in both forms. Afterward, we use a delay before making extra sensor reads.

language:c
delay(2000);

In the example sketch, we pause for 2 seconds (2000 milliseconds) since we are simply printing to the Serial Monitor for viewing and learning purposes. In your actual project, you can read the sensor at 500Hz at most, which means you want a minimum of 2 milliseconds in between sensor reads.

Then it's back to the beginning of loop(). Hope this helps you collect and analyze accelerometer data in your own project.