Git Product home page Git Product logo

adxl345_we's Introduction

ADXL345_WE

Arduino Library for the ADXL345 accelerometer.

I have tried to create a library for the ADXL345 which is easy to use for people who don't want to deal with all the registers. Therefore I have added lots example sketches which will enable you to deal even with the more complex features such as the FIFO modes. Howerever I still recommend to have a look into the data sheet to get a deeper understanding.

You can use both I2C and SPI. If you want to find out how to use SPI, look at the ADXL345_SPI_basic_data.ino.

A detailed tutorial is available:

https://wolles-elektronikkiste.de/adxl345-teil-1 (German)

https://wolles-elektronikkiste.de/en/adxl345-the-universal-accelerometer-part-1 (English)

If you are not so much experienced with the ADXL345, I recommend to work through the examples in the following order:

  1. ADXL345_basic_data
  2. ADXL345_SPI_basic_data
  3. ADXL345_calibration
  4. ADXL345_angles_orientation
  5. ADXL345_pitch_roll_corrected_angles
  6. ADXL345_sleep
  7. ADXL345_free_fall_interrupt
  8. ADXL345_data_ready_interrupt
  9. ADXL345_activity_inactivity_interrupt
  10. ADXL345_auto_sleep.ino
  11. ADXL345_single_tap
  12. ADXL345_double_tap
  13. ADXL345_fifo_fifo
  14. ADXL345_fifo_stream
  15. ADXL345_fifo_trigger

To develop this library I have worked with a ADXL345 module. It should also work with the bare ADXL345 IC. For the module I have noticed that the power consumption is much higher than mentioned in the data sheet. I think the issue is the voltage converter on the module. You can reduce the power consumption by choosing 3.3 volts instead of 5 volts. At least this worked with my module.

If you like my library please give it a star. If you don't like it I would be happy to get feedback. And if you find bugs I will try to eliminate them as quickly as possible.

If SPI does not work

My library has implemented SPI 4-Wire. Some modules have SDO connected GND via an 0 ohm resistor. With this resistor only SPI 3-Wire would work. Your options are:

  1. Remove the 0 ohm resistor connected to SDO (R4 on my module)
  2. Replace the 0 ohm resistor connected to SDO by an internal or external pull-down resistor of 4.7 kohm or 10 kohm
  3. Find another library which supports SPI 3-Wire

ADXL345_hack

adxl345_we's People

Contributors

aaronjamt avatar wollewald avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

adxl345_we's Issues

Raw-z never changes

I ran ADXL345_angles_orientation and Raw-z is always 511.0 and it never changes. Raw-x and Raw-y changes if I rotate the sensor.

With Sparkfun library I see all x, y, z to be changing. There z is between 520-1030.

checkInterrupt after MCU sleep

Hey Wolfgang, thank you for sharing this lib! I'm currently playing with it and struggle with the checkInterrupt call on an ESP32 after put to deep sleep:

   log_d("Setting up ADXL345...");

  if (!myAcc.init()) 
  {
    log_d("ADXL345 not connected!");
  }

  if(myAcc.checkInterrupt(myAcc.readAndClearInterrupts(), ADXL345_ACTIVITY))
  {
    log_d("ADXL345 - Motion registered at: %s", myAcc.getActTapStatusAsString());
  }
  else
  {
    log_d("ADXL345 - No Motion registered");
  }

  myAcc.setCorrFactors(-230.0, 225.0, -227.0, 238.0, -280.0, 227.0);

  myAcc.setDataRate(ADXL345_DATA_RATE_100);
  myAcc.setRange(ADXL345_RANGE_2G);

  myAcc.setActivityParameters(ADXL345_AC_MODE, ADXL345_XYZ, 0.0675);

  myAcc.setInterrupt(ADXL345_ACTIVITY, INT_PIN_1);

  myAcc.readAndClearInterrupts();

The strange thing is: When I put if(myAcc.checkInterrupt(myAcc.readAndClearInterrupts(), ADXL345_ACTIVITY)) into my main loop without going to sleep - then moving the box will successfully get the trigger data from the ADXL345 - but when i put it into my setup routine and query the ADXL345 after a wakeup it never seems to get anything from myAcc.checkInterrupt.

The ADXL345 is always powered on and should register movement and save it until the esp wakes up again and pulls the state and resets that interrupt. Am I doing it totally wrong or am I missing something here?

ranges of values

Thanks for the library!

What would be great for the docs to mention the min and max values.
I guess for ADXL345_RANGE_8G it would be -8G/+8G (for example).

The range of the raw values are not quite so obvious.
From the code it looks like 16bit values.

rawVal.x = (static_cast<int16_t>((rawData[1] << 8) | rawData[0])) * 1.0;
corrRawVal.x = rawVal.x * 1.0 - (offsetVal.x / rangeFactor);
gVal.x = corrVal.x * MILLI_G_PER_LSB * rangeFactor * corrFact.x / 1000.0;

So by default it should be which could be calculated backwards per range factor.

rx = (gx * 1000.0 / (MILLI_G_PER_LSB * rangeFactor * corrFactx) + offsetValx) / rangeFactor

Does that make sense?

"Tap mode" is not generating interrupts

Trying common ADXL345 module on Wemos D1, using I2C.
Readings work, all fine.
Trying ADXL345_single_tap.ino - and it does not generate interrupts on any bumps.
I started with basic example, then lowered sensitivity to 1.3g, increased duration, etc - by reading data i see that overloads up to 8g are registered, but INT2 (and INT1, too) do not change it's values.
I attached logic analyzer with trigger on INT1/INT2 pins and it confirms that values do not change.
I tested with another ADXL345 module - the same, interrupt pin value does not change on bumps.

Unrelated minor issues - to get example compiled under platformio + wemos D1 - need to move "tapISR" function to top of code (above setup()) and add ICACHE_RAM_ATTR in front of it.

THRESH_TAP actual values and sensitivity

Trying to tune-up tap sensitivity using setGeneralTapParameters().
Defaults provided in example works (but are very sensitive). Going higher on numbers (3...16) did not provided me with result i need and i investigated datasheet:
The THRESH_TAP register is eight bits and holds the threshold value for tap interrupts. The data format is unsigned, therefore, the magnitude of the tap event is compared with the value in THRESH_TAP for normal tap detection. The scale factor is 62.5 mg/LSB (that is, 0xFF = 16 g)
OK, so we have 1-byte value for THRESH_TAP - max 255/0xFF.
Looking into library:
regVal = static_cast<uint16_t>(round(threshold / 0.0625));
Here we cast value into 2-byte integer (?), so max value can be 4096. Where is second byte going?

Tried to set threshold value to ~4000 in setGeneralTapParameters() - and yes, sensitivity is now extremely low.
But it's usable in very non-linear way. Maybe one byte is dropped?
In this way - isn't better to do cast into uint8_t then, as documentation requires (and change scale)?
Or there is something more going on?

calibrate

How can I calibrate the ADXL345. It seems that the numbers do not want to settle to zero even if the module is taped to a desk

Arduino IDE 2.0.0-rc3 - Error 13 while installing using the library manage

Hello,
I'm trying to install this library using the Arduino IDE v2.0.0-rc3 using the in-built library manager but the installation failed with the following error message:

Downloading [email protected]
Installing [email protected]
Failed to install library: ADXL345_WE:2.1.2.
Error: 13 INTERNAL: Library install failed: moving extracted archive to destination dir: rename C:\Users\Admin\Documents\Arduino\libraries\package-469848640\ADXL345_WE-2.1.2 C:\Users\Admin\Documents\Arduino\libraries\ADXL345_WE: Access is denied.

I tried launching the IDE with admin rights but still getting the same issue.

Thanks

setFifoParameters overwrites FIFO_MODE

When using setFifoParameters after setFifoMode, the mode is overwritten to bypass.

Suggested fix:

void setFifoParameters(adxl345_triggerInt intNumber, uint8_t samples)
{
	regVal = readRegister8(ADXL345_FIFO_CTL);
	regVal &= 0b11000000;
	regVal |= (samples - 1);
	if (intNumber == ADXL345_TRIGGER_INT_2)
	{
		regVal |= 0x20;
	}
	writeRegister(ADXL345_FIFO_CTL, regVal);
}

setInterruptPolarity does not write the value to be set

void ADXL345_WE::setInterruptPolarity(uint8_t pol){
    regVal = readRegister8(ADXL345_DATA_FORMAT);
    if(pol == ADXL345_ACT_HIGH){
        regVal &= ~(0b00100000);
    }
    else if(pol == ADXL345_ACT_LOW){
        regVal |= 0b00100000;
    }
};

should be like this:

void ADXL345_WE::setInterruptPolarity(uint8_t pol){
    regVal = readRegister8(ADXL345_DATA_FORMAT);
    if(pol == ADXL345_ACT_HIGH){
        regVal &= ~(0b00100000);
    }
    else if(pol == ADXL345_ACT_LOW){
        regVal |= 0b00100000;
    }
    writeRegister(ADXL345_DATA_FORMAT, regVal);
};

in init() shouldn't reset the ADXL345_DATA_FORMAT after setRange

Well done and thanks for your well-organized code.

bool ADXL345_WE::init(){
	......
	setRange(ADXL345_RANGE_2G); // in this func, will set the low two bits of ADXL345_DATA_FORMAT
	writeRegister(ADXL345_DATA_FORMAT, 0); // but here, ADXL345_DATA_FORMAT will be reset.
	setFullRes(true);
      ......

Because the ADXL345_RANGE_2G is defined as 0b00, so it doesn't matter here.
But if try to set ADXL345_RANGE_16G, it won't work as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.