├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── githubci.yml ├── Adafruit_ADXL345_U.cpp ├── Adafruit_ADXL345_U.h ├── README.md ├── code-of-conduct.md ├── examples └── sensortest │ └── sensortest.ino ├── library.properties └── license.txt /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit ADXL345 Arduino Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /Adafruit_ADXL345_U.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_ADXL345_U.cpp 4 | @author K.Townsend (Adafruit Industries) 5 | */ 6 | /**************************************************************************/ 7 | #if ARDUINO >= 100 8 | #include "Arduino.h" 9 | #else 10 | #include "WProgram.h" 11 | #endif 12 | 13 | #include 14 | 15 | #include "Adafruit_ADXL345_U.h" 16 | 17 | Adafruit_ADXL345_Unified::~Adafruit_ADXL345_Unified() { 18 | if (i2c_dev) 19 | delete i2c_dev; 20 | if (spi_dev) 21 | delete spi_dev; 22 | } 23 | 24 | /**************************************************************************/ 25 | /*! 26 | @brief Writes one byte to the specified destination register 27 | @param reg The address of the register to write to 28 | @param value The value to set the register to 29 | */ 30 | /**************************************************************************/ 31 | void Adafruit_ADXL345_Unified::writeRegister(uint8_t reg, uint8_t value) { 32 | uint8_t buffer[2] = {reg, value}; 33 | if (i2c_dev) { 34 | i2c_dev->write(buffer, 2); 35 | } else { 36 | spi_dev->write(buffer, 2); 37 | } 38 | } 39 | 40 | /**************************************************************************/ 41 | /*! 42 | @brief Reads one byte from the specified register 43 | @param reg The address of the register to read from 44 | @returns The single byte value of the requested register 45 | */ 46 | /**************************************************************************/ 47 | uint8_t Adafruit_ADXL345_Unified::readRegister(uint8_t reg) { 48 | uint8_t buffer[1] = {i2c_dev ? reg : (uint8_t)(reg | 0x80)}; 49 | if (i2c_dev) { 50 | i2c_dev->write(buffer, 1); 51 | i2c_dev->read(buffer, 1); 52 | } else { 53 | spi_dev->write_then_read(buffer, 1, buffer, 1); 54 | } 55 | return buffer[0]; 56 | } 57 | 58 | /**************************************************************************/ 59 | /*! 60 | @brief Reads two bytes from the specified register 61 | @param reg The address of the register to read from 62 | @return The two bytes read from the sensor starting at the given address 63 | */ 64 | /**************************************************************************/ 65 | int16_t Adafruit_ADXL345_Unified::read16(uint8_t reg) { 66 | uint8_t buffer[2] = {i2c_dev ? reg : (uint8_t)(reg | 0x80 | 0x40), 0}; 67 | if (i2c_dev) { 68 | i2c_dev->write(buffer, 1); 69 | i2c_dev->read(buffer, 2); 70 | } else { 71 | spi_dev->write_then_read(buffer, 1, buffer, 2); 72 | } 73 | return uint16_t(buffer[1]) << 8 | uint16_t(buffer[0]); 74 | } 75 | 76 | /**************************************************************************/ 77 | /*! 78 | @brief Reads the device ID (can be used to check connection) 79 | @return The Device ID of the connected sensor 80 | */ 81 | /**************************************************************************/ 82 | uint8_t Adafruit_ADXL345_Unified::getDeviceID(void) { 83 | // Check device ID register 84 | return readRegister(ADXL345_REG_DEVID); 85 | } 86 | 87 | /**************************************************************************/ 88 | /*! 89 | @brief Gets the most recent X axis value 90 | @return The raw `int16_t` unscaled x-axis acceleration value 91 | */ 92 | /**************************************************************************/ 93 | int16_t Adafruit_ADXL345_Unified::getX(void) { 94 | return read16(ADXL345_REG_DATAX0); 95 | } 96 | 97 | /**************************************************************************/ 98 | /*! 99 | @brief Gets the most recent Y axis value 100 | @return The raw `int16_t` unscaled y-axis acceleration value 101 | */ 102 | /**************************************************************************/ 103 | int16_t Adafruit_ADXL345_Unified::getY(void) { 104 | return read16(ADXL345_REG_DATAY0); 105 | } 106 | 107 | /**************************************************************************/ 108 | /*! 109 | @brief Gets the most recent Z axis value 110 | @return The raw `int16_t` unscaled z-axis acceleration value 111 | */ 112 | /**************************************************************************/ 113 | int16_t Adafruit_ADXL345_Unified::getZ(void) { 114 | return read16(ADXL345_REG_DATAZ0); 115 | } 116 | 117 | /**************************************************************************/ 118 | /*! 119 | @brief Instantiates a new ADXL345 class 120 | @param sensorID A unique ID to use to differentiate the sensor from others 121 | */ 122 | /**************************************************************************/ 123 | Adafruit_ADXL345_Unified::Adafruit_ADXL345_Unified(int32_t sensorID) { 124 | _sensorID = sensorID; 125 | _range = ADXL345_RANGE_2_G; 126 | } 127 | 128 | /**************************************************************************/ 129 | /*! 130 | @brief Instantiates a new ADXL345 class in SPI mode 131 | @param clock The pin number for SCK, the SPI ClocK line 132 | @param miso The pin number for MISO, the SPI Master In Slave Out line 133 | @param mosi The pin number for MOSI, the SPI Master Out Slave In line 134 | @param cs The pin number for CS, the SPI Chip Select line 135 | @param sensorID A unique ID to use to differentiate the sensor from others 136 | */ 137 | /**************************************************************************/ 138 | Adafruit_ADXL345_Unified::Adafruit_ADXL345_Unified(uint8_t clock, uint8_t miso, 139 | uint8_t mosi, uint8_t cs, 140 | int32_t sensorID) { 141 | _sensorID = sensorID; 142 | _range = ADXL345_RANGE_2_G; 143 | spi_dev = new Adafruit_SPIDevice(cs, clock, miso, mosi, 1000000, 144 | SPI_BITORDER_MSBFIRST, SPI_MODE1); 145 | } 146 | 147 | /**************************************************************************/ 148 | /*! 149 | @brief Setups the HW (reads coefficients values, etc.) 150 | @param i2caddr The I2C address to begin communication with 151 | @return true: success false: a sensor with the correct ID was not found 152 | */ 153 | /**************************************************************************/ 154 | bool Adafruit_ADXL345_Unified::begin(uint8_t i2caddr) { 155 | if (spi_dev == NULL) { 156 | if (i2c_dev) 157 | delete i2c_dev; 158 | i2c_dev = new Adafruit_I2CDevice(i2caddr, &Wire); 159 | if (!i2c_dev->begin()) 160 | return false; 161 | } else { 162 | if (!spi_dev->begin()) 163 | return false; 164 | } 165 | 166 | /* Check connection */ 167 | uint8_t deviceid = getDeviceID(); 168 | if (deviceid != 0xE5) { 169 | /* No ADXL345 detected ... return false */ 170 | return false; 171 | } 172 | 173 | // Enable measurements 174 | writeRegister(ADXL345_REG_POWER_CTL, 0x08); 175 | 176 | return true; 177 | } 178 | 179 | /**************************************************************************/ 180 | /*! 181 | @brief Sets the g range for the accelerometer 182 | @param range The new `range_t` to set the accelerometer to 183 | */ 184 | /**************************************************************************/ 185 | void Adafruit_ADXL345_Unified::setRange(range_t range) { 186 | /* Read the data format register to preserve bits */ 187 | uint8_t format = readRegister(ADXL345_REG_DATA_FORMAT); 188 | 189 | /* Update the data rate */ 190 | format &= ~0x0F; 191 | format |= range; 192 | 193 | /* Make sure that the FULL-RES bit is enabled for range scaling */ 194 | format |= 0x08; 195 | 196 | /* Write the register back to the IC */ 197 | writeRegister(ADXL345_REG_DATA_FORMAT, format); 198 | 199 | /* Keep track of the current range (to avoid readbacks) */ 200 | _range = range; 201 | } 202 | 203 | /**************************************************************************/ 204 | /*! 205 | @brief Gets the g range for the accelerometer 206 | @return The current `range_t` value 207 | */ 208 | /**************************************************************************/ 209 | range_t Adafruit_ADXL345_Unified::getRange(void) { 210 | /* Read the data format register to preserve bits */ 211 | return (range_t)(readRegister(ADXL345_REG_DATA_FORMAT) & 0x03); 212 | } 213 | 214 | /**************************************************************************/ 215 | /*! 216 | @brief Sets the data rate for the ADXL345 (controls power consumption) 217 | @param dataRate The `dataRate_t` to set 218 | */ 219 | /**************************************************************************/ 220 | void Adafruit_ADXL345_Unified::setDataRate(dataRate_t dataRate) { 221 | /* Note: The LOW_POWER bits are currently ignored and we always keep 222 | the device in 'normal' mode */ 223 | writeRegister(ADXL345_REG_BW_RATE, dataRate); 224 | } 225 | 226 | /**************************************************************************/ 227 | /*! 228 | @brief Gets the data rate for the ADXL345 (controls power consumption) 229 | @return The current data rate 230 | */ 231 | /**************************************************************************/ 232 | dataRate_t Adafruit_ADXL345_Unified::getDataRate(void) { 233 | return (dataRate_t)(readRegister(ADXL345_REG_BW_RATE) & 0x0F); 234 | } 235 | 236 | /**************************************************************************/ 237 | /*! 238 | @brief Gets the most recent sensor event 239 | @param event Pointer to the event object to fill 240 | @return true: success 241 | */ 242 | /**************************************************************************/ 243 | bool Adafruit_ADXL345_Unified::getEvent(sensors_event_t *event) { 244 | /* Clear the event */ 245 | memset(event, 0, sizeof(sensors_event_t)); 246 | 247 | event->version = sizeof(sensors_event_t); 248 | event->sensor_id = _sensorID; 249 | event->type = SENSOR_TYPE_ACCELEROMETER; 250 | event->timestamp = 0; 251 | event->acceleration.x = 252 | getX() * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD; 253 | event->acceleration.y = 254 | getY() * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD; 255 | event->acceleration.z = 256 | getZ() * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD; 257 | 258 | return true; 259 | } 260 | 261 | /**************************************************************************/ 262 | /*! 263 | */ 264 | /**************************************************************************/ 265 | 266 | /** 267 | * @brief Fill a `sensor_t` struct with information about the sensor 268 | * 269 | * @param sensor Pointer to a `sensor_t` struct to fill 270 | */ 271 | void Adafruit_ADXL345_Unified::getSensor(sensor_t *sensor) { 272 | /* Clear the sensor_t object */ 273 | memset(sensor, 0, sizeof(sensor_t)); 274 | 275 | /* Insert the sensor name in the fixed length char array */ 276 | strncpy(sensor->name, "ADXL345", sizeof(sensor->name) - 1); 277 | sensor->name[sizeof(sensor->name) - 1] = 0; 278 | sensor->version = 1; 279 | sensor->sensor_id = _sensorID; 280 | sensor->type = SENSOR_TYPE_ACCELEROMETER; 281 | sensor->min_delay = 0; 282 | sensor->max_value = -156.9064F; /* -16g = 156.9064 m/s^2 */ 283 | sensor->min_value = 156.9064F; /* 16g = 156.9064 m/s^2 */ 284 | sensor->resolution = 0.03923F; /* 4mg = 0.0392266 m/s^2 */ 285 | } 286 | -------------------------------------------------------------------------------- /Adafruit_ADXL345_U.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_ADXL345_U.h 4 | @author K. Townsend (Adafruit Industries) 5 | 6 | 7 | The ADXL345 is a digital accelerometer with 13-bit resolution, capable 8 | of measuring up to +/-16g. This driver communicate using I2C. 9 | 10 | This is a library for the Adafruit ADXL345 breakout 11 | ----> https://www.adafruit.com/products/1231 12 | 13 | Adafruit invests time and resources providing this open source code, 14 | please support Adafruit and open-source hardware by purchasing 15 | products from Adafruit! 16 | 17 | @section license License 18 | BSD (see license.txt) 19 | 20 | @section HISTORY 21 | 22 | v1.1 - Added Adafruit_Sensor library support 23 | v1.0 - First release 24 | */ 25 | /**************************************************************************/ 26 | 27 | #ifndef Adafruit_ADXL345_h 28 | #define Adafruit_ADXL345_h 29 | 30 | #if ARDUINO >= 100 31 | #include "Arduino.h" 32 | #else 33 | #include "WProgram.h" 34 | #endif 35 | 36 | #include 37 | #include 38 | #include 39 | 40 | /*========================================================================= 41 | I2C ADDRESS/BITS 42 | -----------------------------------------------------------------------*/ 43 | #define ADXL345_DEFAULT_ADDRESS (0x53) ///< Assumes ALT address pin low 44 | /*=========================================================================*/ 45 | 46 | /*========================================================================= 47 | REGISTERS 48 | -----------------------------------------------------------------------*/ 49 | #define ADXL345_REG_DEVID (0x00) ///< Device ID 50 | #define ADXL345_REG_THRESH_TAP (0x1D) ///< Tap threshold 51 | #define ADXL345_REG_OFSX (0x1E) ///< X-axis offset 52 | #define ADXL345_REG_OFSY (0x1F) ///< Y-axis offset 53 | #define ADXL345_REG_OFSZ (0x20) ///< Z-axis offset 54 | #define ADXL345_REG_DUR (0x21) ///< Tap duration 55 | #define ADXL345_REG_LATENT (0x22) ///< Tap latency 56 | #define ADXL345_REG_WINDOW (0x23) ///< Tap window 57 | #define ADXL345_REG_THRESH_ACT (0x24) ///< Activity threshold 58 | #define ADXL345_REG_THRESH_INACT (0x25) ///< Inactivity threshold 59 | #define ADXL345_REG_TIME_INACT (0x26) ///< Inactivity time 60 | #define ADXL345_REG_ACT_INACT_CTL \ 61 | (0x27) ///< Axis enable control for activity and inactivity detection 62 | #define ADXL345_REG_THRESH_FF (0x28) ///< Free-fall threshold 63 | #define ADXL345_REG_TIME_FF (0x29) ///< Free-fall time 64 | #define ADXL345_REG_TAP_AXES (0x2A) ///< Axis control for single/double tap 65 | #define ADXL345_REG_ACT_TAP_STATUS (0x2B) ///< Source for single/double tap 66 | #define ADXL345_REG_BW_RATE (0x2C) ///< Data rate and power mode control 67 | #define ADXL345_REG_POWER_CTL (0x2D) ///< Power-saving features control 68 | #define ADXL345_REG_INT_ENABLE (0x2E) ///< Interrupt enable control 69 | #define ADXL345_REG_INT_MAP (0x2F) ///< Interrupt mapping control 70 | #define ADXL345_REG_INT_SOURCE (0x30) ///< Source of interrupts 71 | #define ADXL345_REG_DATA_FORMAT (0x31) ///< Data format control 72 | #define ADXL345_REG_DATAX0 (0x32) ///< X-axis data 0 73 | #define ADXL345_REG_DATAX1 (0x33) ///< X-axis data 1 74 | #define ADXL345_REG_DATAY0 (0x34) ///< Y-axis data 0 75 | #define ADXL345_REG_DATAY1 (0x35) ///< Y-axis data 1 76 | #define ADXL345_REG_DATAZ0 (0x36) ///< Z-axis data 0 77 | #define ADXL345_REG_DATAZ1 (0x37) ///< Z-axis data 1 78 | #define ADXL345_REG_FIFO_CTL (0x38) ///< FIFO control 79 | #define ADXL345_REG_FIFO_STATUS (0x39) ///< FIFO status 80 | /*=========================================================================*/ 81 | 82 | /*========================================================================= 83 | REGISTERS 84 | -----------------------------------------------------------------------*/ 85 | #define ADXL345_MG2G_MULTIPLIER (0.004) ///< 4mg per lsb 86 | /*=========================================================================*/ 87 | 88 | /** 89 | * @brief Used with register 0x2C (ADXL345_REG_BW_RATE) to set bandwidth 90 | 91 | */ 92 | typedef enum { 93 | ADXL345_DATARATE_3200_HZ = 0b1111, ///< 1600Hz Bandwidth 140�A IDD 94 | ADXL345_DATARATE_1600_HZ = 0b1110, ///< 800Hz Bandwidth 90�A IDD 95 | ADXL345_DATARATE_800_HZ = 0b1101, ///< 400Hz Bandwidth 140�A IDD 96 | ADXL345_DATARATE_400_HZ = 0b1100, ///< 200Hz Bandwidth 140�A IDD 97 | ADXL345_DATARATE_200_HZ = 0b1011, ///< 100Hz Bandwidth 140�A IDD 98 | ADXL345_DATARATE_100_HZ = 0b1010, ///< 50Hz Bandwidth 140�A IDD 99 | ADXL345_DATARATE_50_HZ = 0b1001, ///< 25Hz Bandwidth 90�A IDD 100 | ADXL345_DATARATE_25_HZ = 0b1000, ///< 12.5Hz Bandwidth 60�A IDD 101 | ADXL345_DATARATE_12_5_HZ = 0b0111, ///< 6.25Hz Bandwidth 50�A IDD 102 | ADXL345_DATARATE_6_25HZ = 0b0110, ///< 3.13Hz Bandwidth 45�A IDD 103 | ADXL345_DATARATE_3_13_HZ = 0b0101, ///< 1.56Hz Bandwidth 40�A IDD 104 | ADXL345_DATARATE_1_56_HZ = 0b0100, ///< 0.78Hz Bandwidth 34�A IDD 105 | ADXL345_DATARATE_0_78_HZ = 0b0011, ///< 0.39Hz Bandwidth 23�A IDD 106 | ADXL345_DATARATE_0_39_HZ = 0b0010, ///< 0.20Hz Bandwidth 23�A IDD 107 | ADXL345_DATARATE_0_20_HZ = 0b0001, ///< 0.10Hz Bandwidth 23�A IDD 108 | ADXL345_DATARATE_0_10_HZ = 109 | 0b0000 ///< 0.05Hz Bandwidth 23�A IDD (default value) 110 | } dataRate_t; 111 | 112 | /** 113 | * @brief Used with register 0x31 (ADXL345_REG_DATA_FORMAT) to set g range 114 | * 115 | */ 116 | typedef enum { 117 | ADXL345_RANGE_16_G = 0b11, ///< +/- 16g 118 | ADXL345_RANGE_8_G = 0b10, ///< +/- 8g 119 | ADXL345_RANGE_4_G = 0b01, ///< +/- 4g 120 | ADXL345_RANGE_2_G = 0b00 ///< +/- 2g (default value) 121 | } range_t; 122 | 123 | /** 124 | * @brief Class to interact with the ADXL345 accelerometer 125 | * 126 | */ 127 | class Adafruit_ADXL345_Unified : public Adafruit_Sensor { 128 | public: 129 | Adafruit_ADXL345_Unified(int32_t sensorID = -1); 130 | Adafruit_ADXL345_Unified(uint8_t clock, uint8_t miso, uint8_t mosi, 131 | uint8_t cs, int32_t sensorID = -1); 132 | ~Adafruit_ADXL345_Unified(); 133 | 134 | bool begin(uint8_t addr = ADXL345_DEFAULT_ADDRESS); 135 | void setRange(range_t range); 136 | range_t getRange(void); 137 | void setDataRate(dataRate_t dataRate); 138 | dataRate_t getDataRate(void); 139 | bool getEvent(sensors_event_t *); 140 | void getSensor(sensor_t *); 141 | 142 | uint8_t getDeviceID(void); 143 | void writeRegister(uint8_t reg, uint8_t value); 144 | uint8_t readRegister(uint8_t reg); 145 | int16_t read16(uint8_t reg); 146 | 147 | int16_t getX(void), getY(void), getZ(void); 148 | 149 | private: 150 | Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface 151 | Adafruit_SPIDevice *spi_dev = NULL; ///< Pointer to SPI bus interface 152 | 153 | int32_t _sensorID; 154 | range_t _range; 155 | }; 156 | 157 | #endif // Adafruit_ADXL345_h 158 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adafruit ADXL345 [![Build Status](https://github.com/adafruit/Adafruit_ADXL345/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_ADXL345/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_ADXL345/html/index.html) 2 | 3 | This driver is for the Adafruit ADXL345 Breakout (http://www.adafruit.com/products/1231), and is based on Adafruit's Unified Sensor Library (Adafruit_Sensor). 4 | Tested and works great with the Adafruit ADXL345 Breakout Board 5 | [](https://www.adafruit.com/products/1231) 6 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 7 | 8 | ## About the ADXL345 ## 9 | 10 | The ADXL345 is a digital accelerometer that supports both SPI and I2C mode, with adjustable data rata and 'range' (+/-2/4/8/16g). The Adafruit_ADXL345 driver takes advantage of I2C mode to reduce the total pin count required to use the sensor. 11 | 12 | More information on the ADXL345 can be found in the datasheet: http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf 13 | 14 | 15 | 16 | ## Compatibility 17 | 18 | MCU | Tested Works | Doesn't Work | Not Tested | Notes 19 | ------------------ | :----------: | :----------: | :---------: | ----- 20 | Atmega328 @ 16MHz | X | | | 21 | Atmega328 @ 12MHz | X | | | 22 | Atmega32u4 @ 16MHz | X | | | 23 | Atmega32u4 @ 8MHz | X | | | 24 | ESP8266 | X | | | 25 | Atmega2560 @ 16MHz | X | | | 26 | ATSAM3X8E | X | | | 27 | ATSAM21D | X | | | 28 | ATtiny85 @ 16MHz | | X | | sketch too big 29 | ATtiny85 @ 8MHz | | X | | sketch too big 30 | Intel Curie @ 32MHz | X | | | 31 | STM32F2 | | | X | 32 | 33 | * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini 34 | * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V 35 | * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0 36 | * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro 37 | * ESP8266 : Adafruit Huzzah 38 | * ATmega2560 @ 16MHz : Arduino Mega 39 | * ATSAM3X8E : Arduino Due 40 | * ATSAM21D : Arduino Zero, M0 Pro 41 | * ATtiny85 @ 16MHz : Adafruit Trinket 5V 42 | * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V 43 | 44 | 45 | ## What is the Adafruit Unified Sensor Library? ## 46 | 47 | The Adafruit Unified Sensor Library (https://github.com/adafruit/Adafruit_Sensor) provides a common interface and data type for any supported sensor. It defines some basic information about the sensor (sensor limits, etc.), and returns standard SI units of a specific type and scale for each supported sensor type. 48 | 49 | It provides a simple abstraction layer between your application and the actual sensor HW, allowing you to drop in any comparable sensor with only one or two lines of code to change in your project (essentially the constructor since the functions to read sensor data and get information about the sensor are defined in the base Adafruit_Sensor class). 50 | 51 | This is imporant useful for two reasons: 52 | 53 | 1.) You can use the data right away because it's already converted to SI units that you understand and can compare, rather than meaningless values like 0..1023. 54 | 55 | 2.) Because SI units are standardised in the sensor library, you can also do quick sanity checks working with new sensors, or drop in any comparable sensor if you need better sensitivity or if a lower cost unit becomes available, etc. 56 | 57 | Light sensors will always report units in lux, gyroscopes will always report units in rad/s, etc. ... freeing you up to focus on the data, rather than digging through the datasheet to understand what the sensor's raw numbers really mean. 58 | 59 | ## About this Driver ## 60 | 61 | Adafruit invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from Adafruit! 62 | # Dependencies 63 | * [Adafruit Unified Sensor Driver](https://github.com/adafruit/Adafruit_Sensor) 64 | 65 | # Contributing 66 | Contributions are welcome! Please read our [Code of Conduct](https://github.com/adafruit/Adafruit_ADXL345/blob/master/CODE_OF_CONDUCT.md>) 67 | before contributing to help this project stay welcoming. 68 | 69 | ## Documentation and doxygen 70 | Documentation is produced by doxygen. Contributions should include documentation for any new code added. 71 | 72 | Some examples of how to use doxygen can be found in these guide pages: 73 | 74 | https://learn.adafruit.com/the-well-automated-arduino-library/doxygen 75 | 76 | https://learn.adafruit.com/the-well-automated-arduino-library/doxygen-tips 77 | 78 | Written by Kevin (KTOWN) Townsend for Adafruit Industries. 79 | BSD license, check license.txt for more information 80 | All text above must be included in any redistribution 81 | 82 | To install, use the Arduino Library Manager and search for "Adafruit ADXL345" and install the library. 83 | -------------------------------------------------------------------------------- /code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Adafruit Community Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and leaders pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level or type of 9 | experience, education, socio-economic status, nationality, personal appearance, 10 | race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | We are committed to providing a friendly, safe and welcoming environment for 15 | all. 16 | 17 | Examples of behavior that contributes to creating a positive environment 18 | include: 19 | 20 | * Be kind and courteous to others 21 | * Using welcoming and inclusive language 22 | * Being respectful of differing viewpoints and experiences 23 | * Collaborating with other community members 24 | * Gracefully accepting constructive criticism 25 | * Focusing on what is best for the community 26 | * Showing empathy towards other community members 27 | 28 | Examples of unacceptable behavior by participants include: 29 | 30 | * The use of sexualized language or imagery and sexual attention or advances 31 | * The use of inappropriate images, including in a community member's avatar 32 | * The use of inappropriate language, including in a community member's nickname 33 | * Any spamming, flaming, baiting or other attention-stealing behavior 34 | * Excessive or unwelcome helping; answering outside the scope of the question 35 | asked 36 | * Trolling, insulting/derogatory comments, and personal or political attacks 37 | * Public or private harassment 38 | * Publishing others' private information, such as a physical or electronic 39 | address, without explicit permission 40 | * Other conduct which could reasonably be considered inappropriate 41 | 42 | The goal of the standards and moderation guidelines outlined here is to build 43 | and maintain a respectful community. We ask that you don’t just aim to be 44 | "technically unimpeachable", but rather try to be your best self. 45 | 46 | We value many things beyond technical expertise, including collaboration and 47 | supporting others within our community. Providing a positive experience for 48 | other community members can have a much more significant impact than simply 49 | providing the correct answer. 50 | 51 | ## Our Responsibilities 52 | 53 | Project leaders are responsible for clarifying the standards of acceptable 54 | behavior and are expected to take appropriate and fair corrective action in 55 | response to any instances of unacceptable behavior. 56 | 57 | Project leaders have the right and responsibility to remove, edit, or 58 | reject messages, comments, commits, code, issues, and other contributions 59 | that are not aligned to this Code of Conduct, or to ban temporarily or 60 | permanently any community member for other behaviors that they deem 61 | inappropriate, threatening, offensive, or harmful. 62 | 63 | ## Moderation 64 | 65 | Instances of behaviors that violate the Adafruit Community Code of Conduct 66 | may be reported by any member of the community. Community members are 67 | encouraged to report these situations, including situations they witness 68 | involving other community members. 69 | 70 | You may report in the following ways: 71 | 72 | In any situation, you may send an email to . 73 | 74 | On the Adafruit Discord, you may send an open message from any channel 75 | to all Community Helpers by tagging @community helpers. You may also send an 76 | open message from any channel, or a direct message to @kattni#1507, 77 | @tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or 78 | @Andon#8175. 79 | 80 | Email and direct message reports will be kept confidential. 81 | 82 | In situations on Discord where the issue is particularly egregious, possibly 83 | illegal, requires immediate action, or violates the Discord terms of service, 84 | you should also report the message directly to Discord. 85 | 86 | These are the steps for upholding our community’s standards of conduct. 87 | 88 | 1. Any member of the community may report any situation that violates the 89 | Adafruit Community Code of Conduct. All reports will be reviewed and 90 | investigated. 91 | 2. If the behavior is an egregious violation, the community member who 92 | committed the violation may be banned immediately, without warning. 93 | 3. Otherwise, moderators will first respond to such behavior with a warning. 94 | 4. Moderators follow a soft "three strikes" policy - the community member may 95 | be given another chance, if they are receptive to the warning and change their 96 | behavior. 97 | 5. If the community member is unreceptive or unreasonable when warned by a 98 | moderator, or the warning goes unheeded, they may be banned for a first or 99 | second offense. Repeated offenses will result in the community member being 100 | banned. 101 | 102 | ## Scope 103 | 104 | This Code of Conduct and the enforcement policies listed above apply to all 105 | Adafruit Community venues. This includes but is not limited to any community 106 | spaces (both public and private), the entire Adafruit Discord server, and 107 | Adafruit GitHub repositories. Examples of Adafruit Community spaces include 108 | but are not limited to meet-ups, audio chats on the Adafruit Discord, or 109 | interaction at a conference. 110 | 111 | This Code of Conduct applies both within project spaces and in public spaces 112 | when an individual is representing the project or its community. As a community 113 | member, you are representing our community, and are expected to behave 114 | accordingly. 115 | 116 | ## Attribution 117 | 118 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 119 | version 1.4, available at 120 | , 121 | and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). 122 | 123 | For other projects adopting the Adafruit Community Code of 124 | Conduct, please contact the maintainers of those projects for enforcement. 125 | If you wish to use this code of conduct for your own project, consider 126 | explicitly mentioning your moderation policy or making a copy with your 127 | own moderation policy so as to avoid confusion. 128 | -------------------------------------------------------------------------------- /examples/sensortest/sensortest.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* Assign a unique ID to this sensor at the same time */ 6 | Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); 7 | 8 | void displaySensorDetails(void) 9 | { 10 | sensor_t sensor; 11 | accel.getSensor(&sensor); 12 | Serial.println("------------------------------------"); 13 | Serial.print ("Sensor: "); Serial.println(sensor.name); 14 | Serial.print ("Driver Ver: "); Serial.println(sensor.version); 15 | Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); 16 | Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" m/s^2"); 17 | Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" m/s^2"); 18 | Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" m/s^2"); 19 | Serial.println("------------------------------------"); 20 | Serial.println(""); 21 | delay(500); 22 | } 23 | 24 | void displayDataRate(void) 25 | { 26 | Serial.print ("Data Rate: "); 27 | 28 | switch(accel.getDataRate()) 29 | { 30 | case ADXL345_DATARATE_3200_HZ: 31 | Serial.print ("3200 "); 32 | break; 33 | case ADXL345_DATARATE_1600_HZ: 34 | Serial.print ("1600 "); 35 | break; 36 | case ADXL345_DATARATE_800_HZ: 37 | Serial.print ("800 "); 38 | break; 39 | case ADXL345_DATARATE_400_HZ: 40 | Serial.print ("400 "); 41 | break; 42 | case ADXL345_DATARATE_200_HZ: 43 | Serial.print ("200 "); 44 | break; 45 | case ADXL345_DATARATE_100_HZ: 46 | Serial.print ("100 "); 47 | break; 48 | case ADXL345_DATARATE_50_HZ: 49 | Serial.print ("50 "); 50 | break; 51 | case ADXL345_DATARATE_25_HZ: 52 | Serial.print ("25 "); 53 | break; 54 | case ADXL345_DATARATE_12_5_HZ: 55 | Serial.print ("12.5 "); 56 | break; 57 | case ADXL345_DATARATE_6_25HZ: 58 | Serial.print ("6.25 "); 59 | break; 60 | case ADXL345_DATARATE_3_13_HZ: 61 | Serial.print ("3.13 "); 62 | break; 63 | case ADXL345_DATARATE_1_56_HZ: 64 | Serial.print ("1.56 "); 65 | break; 66 | case ADXL345_DATARATE_0_78_HZ: 67 | Serial.print ("0.78 "); 68 | break; 69 | case ADXL345_DATARATE_0_39_HZ: 70 | Serial.print ("0.39 "); 71 | break; 72 | case ADXL345_DATARATE_0_20_HZ: 73 | Serial.print ("0.20 "); 74 | break; 75 | case ADXL345_DATARATE_0_10_HZ: 76 | Serial.print ("0.10 "); 77 | break; 78 | default: 79 | Serial.print ("???? "); 80 | break; 81 | } 82 | Serial.println(" Hz"); 83 | } 84 | 85 | void displayRange(void) 86 | { 87 | Serial.print ("Range: +/- "); 88 | 89 | switch(accel.getRange()) 90 | { 91 | case ADXL345_RANGE_16_G: 92 | Serial.print ("16 "); 93 | break; 94 | case ADXL345_RANGE_8_G: 95 | Serial.print ("8 "); 96 | break; 97 | case ADXL345_RANGE_4_G: 98 | Serial.print ("4 "); 99 | break; 100 | case ADXL345_RANGE_2_G: 101 | Serial.print ("2 "); 102 | break; 103 | default: 104 | Serial.print ("?? "); 105 | break; 106 | } 107 | Serial.println(" g"); 108 | } 109 | 110 | void setup(void) 111 | { 112 | #ifndef ESP8266 113 | while (!Serial); // for Leonardo/Micro/Zero 114 | #endif 115 | Serial.begin(9600); 116 | Serial.println("Accelerometer Test"); Serial.println(""); 117 | 118 | /* Initialise the sensor */ 119 | if(!accel.begin()) 120 | { 121 | /* There was a problem detecting the ADXL345 ... check your connections */ 122 | Serial.println("Ooops, no ADXL345 detected ... Check your wiring!"); 123 | while(1); 124 | } 125 | 126 | /* Set the range to whatever is appropriate for your project */ 127 | accel.setRange(ADXL345_RANGE_16_G); 128 | // accel.setRange(ADXL345_RANGE_8_G); 129 | // accel.setRange(ADXL345_RANGE_4_G); 130 | // accel.setRange(ADXL345_RANGE_2_G); 131 | 132 | /* Display some basic information on this sensor */ 133 | displaySensorDetails(); 134 | 135 | /* Display additional settings (outside the scope of sensor_t) */ 136 | displayDataRate(); 137 | displayRange(); 138 | Serial.println(""); 139 | } 140 | 141 | void loop(void) 142 | { 143 | /* Get a new sensor event */ 144 | sensors_event_t event; 145 | accel.getEvent(&event); 146 | 147 | /* Display the results (acceleration is measured in m/s^2) */ 148 | Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" "); 149 | Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" "); 150 | Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");Serial.println("m/s^2 "); 151 | delay(500); 152 | } 153 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit ADXL345 2 | version=1.3.4 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Unified driver for the ADXL345 Accelerometer 6 | paragraph=Unified driver for the ADXL345 Accelerometer 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_ADXL345 9 | architectures=* 10 | depends=Adafruit Unified Sensor, Adafruit BusIO 11 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2020 Kevin (KTOWN) Townsend for Adafruit Industries 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holders nor the 14 | names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | --------------------------------------------------------------------------------