Hi everyone,
My very first Adruino project ever, am trying to setup an ADXL377 accelerometer and read in G force readings from the 3 axis.
I am using code found online, and am not receiving any errors upon compiling:
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;
}
I am connected at 5V, per a diagram I also found online(see attached), pretty basic really, +5V, GND and A0, A1 and A2 for the 3 axis.
Readings are as follows, few examples:
X: 511
Y: 512
Z: 512
X: 102.81 g
Y: 103.41 g
Z: 103.41 g
X: 513
Y: 513
Z: 513
X: 104.00 g
Y: 104.00 g
Z: 104.00 g
I'm a bit concerned with the 100g reading at rest?
Has anyone ever seen this type of problem before? Should I get near 0 readings on at least 2 axis?
Also, moving the board around while looking at the serial monitor doesn't seem to have any effect on readings, should it? I tried sampling at 20ms, still no apparent change to readings.
All comments welcome.
Thanks in advance
AC