How to make my adxl345 to turn on an LED when I twist it upside down

Im trying to make a device that will switch on a LED when I twist my hand, Im trying to simulate a smart watch feature wherein when I raised my hand and look to my smart watch the lcd screen will automatically light up, the only difference with my project is that when I twist my hand the device will turn on.

Currently, Im using an adxl345 but the problem is that when I twist my sensor facing down that y-value returns back to 0 which is the same value when I twist my sensor facing Upward, this means that when my sensor is facing Up the led is on then turns off when I twist my hand, then the LED is turned on again when the sensor is facing down. Is their any way wherein I can only turn on my led when the sensor is facing down.

This is the code Im currently using

#include <SparkFun_ADXL345.h>         // SparkFun ADXL345 Library
ADXL345 adxl = ADXL345(10);           // USE FOR SPI COMMUNICATION, ADXL345(CS_PIN);
void setup() {
  pinMode(2,OUTPUT);
  Serial.begin(9600);               // Start the serial terminal
  adxl.powerOn();                     // Power on the ADXL345
  adxl.setRangeSetting(2);           // Give the range settings
                                      // Accepted values are 2g, 4g, 8g or 16g
                                      // Higher Values = Wider Measurement Range
                                      // Lower Values = Greater Sensitivity
  adxl.setSpiBit(0);                  // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
                                      // Default: Set to 1
                                      // SPI pins on the ATMega328: 11, 12 and 13 as reference in SPI Library 
}
void loop() {
  // Accelerometer Readings
  int x,y,z;   
  adxl.readAccel(&x, &y, &z);         // Read the accelerometer values and store them in variables declared above x,y,z
  // Output Results to Serial
  

  Serial.println(y);
  if(y > 0 && y < 60 ){
    digitalWrite(2,1);
  }else{
    digitalWrite(2,0);
  }

}

Study the y values printed out for different wrist orientations, and make a better choice for the range in the "if" statement.

2 Likes

But some other axis has changed to a DIFFERENT value. You have to check more than one axis if you want to know which way your sensor is facing.

If the z axis is pointing up out of the face of your watch, a Z value near +1G or -1G should indicate that the watch is facing up.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.