ADXL345 Only Detects Tilt with Photon?


I was following the code that was mentioned in here:

But it only seems like the accelerometer is measuring tilt. When I am moving it in one direction and not tilting it in any way, the numbers don’t really change. If I then tilt the accelerometer, the values change.

In the datasheet, it said that it can measuring dynamic acceleration and tilting.

So I was wondering if the adxl only measures tilt?


How do you move the device. Accelerometers will not measure velocity but only acceleration.

In what manner do they change?
Accelerometers will usually always report the gravitational acceleration (g) so when you align any of the three axes vertically you should only read 1g in that one direction while the others should report 0g (when not accelerating horizontally).
This way you can test whether each individual axis does get detected correctly or not.

You are correct, I was outputting the serial output every 500ms, so if I decelerated too quickly, it didn't seem print the output. When I reduced the delay, I began to see values go up during acceleration and down during deceleration.


On a side note, related to the code, I simply added the following toggle function. When I toggle the function I just want the state variable to toggle. For some reason, when I publish the state variable in cloud, it always starts off as 1, even though I have initialized the state variable to start as 0. When I call the particle function to toggle the state variable, it goes to 0, but then it immediately goes back to 1. So I am a little confused about how global variables work here...

int ledToggle(String command);
int led = D7; int state = 0; int val = 0;

void setup() 
{ Particle.function("ledstate", ledToggle);    
  pinMode(led, OUTPUT);
  // Rest of accelerometer code...
void loop()
{  if (state = 1){readAccel();} }

int ledToggle(String command)
{ val = digitalRead(led);
  if (val == 0) {   
    digitalWrite(led, HIGH);   
    state = 0;
    return 0;
  } else {               
    digitalWrite(led, LOW);    
    state = 1;
    return 1;

This is setting the value not merely checking it.
The equality check shoudl be written as state == 1.

If you find yourself forgetting the double equality sign you can flip the statement round like this

  if (1 == state)   // accidental (1 = state) would cause a compile time error for early detection
1 Like

Wow…Sorry for asking about something so simple…Thanks!