annem / adxl362 Goto Github PK
View Code? Open in Web Editor NEWArduino Library for ADXL362 Micropower 3-axis accelerometer
Home Page: http://annem.github.com/ADXL362/
Arduino Library for ADXL362 Micropower 3-axis accelerometer
Home Page: http://annem.github.com/ADXL362/
Hello Sir,
I have a problem using your library with Sparkfun ADXL362 break out. When I run SimpleRead I only get XVALUE. Sample is pasted below... Please help.
Start Demo: Simple Read
XVALUE=0 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=192 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=128 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=252 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=254 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=128 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=0 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=0 YVALUE=0 ZVALUE=0 TEMPERATURE=0
XVALUE=0 YVALUE=0 ZVALUE=0 TEMPERATURE=0
Dear sir/madam,
I have build arduino from Atmega1284P chip with 16MHz clock based on Arduino Uno*Pro. Readings from ADXL362 are kind of strange. Basic reading examples only show readings for axis X no mather how I tilt the sensor. No Y, Z or temp readings, just zeros. And readings for X are only values as 16,8,4,32,192,144,64, nothing else. Register 29 sometimes shows C0, sometimes 80. From that I suspect there could be timing problem with reading values through SPI. Have you tested examples on Atmega1284P?
Thanks, Jan
The switch from 16 bit ints to 32 bit ints on the Due causes the values to be incorrect with the accelerometer. The easy fix was to change all ints to int16_t, which shouldn't lose precision, because the accelerometer outputs 12 bit integers. The following works for me:
/*
Arduino Library for Analog Devices ADXL362 - Micropower 3-axis accelerometer
go to http://www.analog.com/ADXL362 for datasheet
License: CC BY-SA 3.0: Creative Commons Share-alike 3.0. Feel free
to use and abuse this code however you'd like. If you find it useful
please attribute, and SHARE-ALIKE!
Created June 2012
by Anne Mahaffey - hosted on http://annem.github.com/ADXL362
*/
#include <Arduino.h>
#include <SPI.h>
#include <ADXL362.h>
const int16_t slaveSelectPin = 52;
const bool debugSerial = 1;
ADXL362::ADXL362() {
}
//
// begin()
// Initial SPI setup, soft reset of device
//
void ADXL362::begin() {
pinMode(slaveSelectPin, OUTPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE0); //CPHA = CPOL = 0 MODE = 0
delay(1000);
// soft reset
SPIwriteOneRegister(0x1F, 0x52); // Write to SOFT RESET, "R"
delay(10);
Serial.println("Soft Reset\n");
}
//
// beginMeasure()
// turn on Measurement mode - required after reset
//
void ADXL362::beginMeasure() {
byte temp = SPIreadOneRegister(0x2D); // read Reg 2D before modifying for measure mode
if (debugSerial) {Serial.print( "Setting Measeurement Mode - Reg 2D before = "); Serial.print(temp); }
// turn on measurement mode
byte tempwrite = temp | 0x02; // turn on measurement bit in Reg 2D
SPIwriteOneRegister(0x2D, tempwrite); // Write to POWER_CTL_REG, Measurement Mode
delay(10);
if (debugSerial) {
temp = SPIreadOneRegister(0x2D);
Serial.print( ", Reg 2D after = "); Serial.println(temp); Serial.println();}
}
//
// readXData(), readYData(), readZData(), readTemp()
// Read X, Y, Z, and Temp registers
//
int16_t ADXL362::readXData(){
int16_t XDATA = SPIreadTwoRegisters(0x0E);
if (debugSerial) {Serial.print( "XDATA = "); Serial.print(XDATA); }
}
int16_t ADXL362::readYData(){
int16_t YDATA = SPIreadTwoRegisters(0x10);
if (debugSerial) {Serial.print( "\tYDATA = "); Serial.print(YDATA); }
}
int16_t ADXL362::readZData(){
int16_t ZDATA = SPIreadTwoRegisters(0x12);
if (debugSerial) {Serial.print( "\tZDATA = "); Serial.print(ZDATA); }
}
int16_t ADXL362::readTemp(){
int16_t TEMP = SPIreadTwoRegisters(0x14);
if (debugSerial) {Serial.print("\tTEMP = "); Serial.print(TEMP); }
}
void ADXL362::readXYZTData(int16_t XData, int16_t YData, int16_t ZData, int16_t Temperature){
// burst SPI read
// A burst read of all three axis is required to guarantee all measurements correspond to same sample time
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0B); // read instruction
SPI.transfer(0x0E); // Start at XData Reg
XData = SPI.transfer(0x00);
XData = XData + (SPI.transfer(0x00) << 8);
YData = SPI.transfer(0x00);
YData = YData + (SPI.transfer(0x00) << 8);
ZData = SPI.transfer(0x00);
ZData = ZData + (SPI.transfer(0x00) << 8);
Temperature = SPI.transfer(0x00);
Temperature = Temperature + (SPI.transfer(0x00) << 8);
digitalWrite(slaveSelectPin, HIGH);
if (debugSerial) {
Serial.print( ""); Serial.print(XData);
Serial.print( "\t"); Serial.print(YData);
Serial.print( "\t"); Serial.print(ZData);
Serial.print( "\t"); Serial.println(Temperature);
}
}
void ADXL362::setupDCActivityInterrupt(int16_t threshold, byte time){
// Setup motion and time thresholds
SPIwriteTwoRegisters(0x20, threshold);
SPIwriteOneRegister(0x22, time);
// turn on activity interrupt
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x01); // turn on bit 1, ACT_EN
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
if (debugSerial) {
Serial.print("DC Activity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x20));
Serial.print(", Time threshold set to "); Serial.print(SPIreadOneRegister(0x22));
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
}
}
void ADXL362::setupACActivityInterrupt(int16_t threshold, byte time){
// Setup motion and time thresholds
SPIwriteTwoRegisters(0x20, threshold);
SPIwriteOneRegister(0x22, time);
// turn on activity interrupt
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x03); // turn on bit 2 and 1, ACT_AC_DCB, ACT_EN
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
if (debugSerial) {
Serial.print("AC Activity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x20));
Serial.print(", Time Activity set to "); Serial.print(SPIreadOneRegister(0x22));
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
}
}
void ADXL362::setupDCInactivityInterrupt(int16_t threshold, int16_t time){
// Setup motion and time thresholds
SPIwriteTwoRegisters(0x23, threshold);
SPIwriteTwoRegisters(0x25, time);
// turn on inactivity interrupt
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x04); // turn on bit 3, INACT_EN
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
if (debugSerial) {
Serial.print("DC Inactivity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x23));
Serial.print(", Time Inactivity set to "); Serial.print(SPIreadTwoRegisters(0x25));
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
}
}
void ADXL362::setupACInactivityInterrupt(int16_t threshold, int16_t time){
// Setup motion and time thresholds
SPIwriteTwoRegisters(0x23, threshold);
SPIwriteTwoRegisters(0x25, time);
// turn on inactivity interrupt
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x0C); // turn on bit 3 and 4, INACT_AC_DCB, INACT_EN
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
if (debugSerial) {
Serial.print("AC Inactivity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x23));
Serial.print(", Time Inactivity set to "); Serial.print(SPIreadTwoRegisters(0x25));
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
}
}
void ADXL362::checkAllControlRegs(){
//byte filterCntlReg = SPIreadOneRegister(0x2C);
//byte ODR = filterCntlReg & 0x07; Serial.print("ODR = "); Serial.println(ODR, HEX);
//byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); Serial.print("ACT_INACT_CTL_Reg = "); Serial.println(ACT_INACT_CTL_Reg, HEX);
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0B); // read instruction
SPI.transfer(0x20); // Start burst read at Reg 20
Serial.println("Start Burst Read of all Control Regs - Library version 6-24-2012:");
Serial.print("Reg 20 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 21 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 22 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 23 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 24 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 25 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 26 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 27 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 28 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 29 = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 2A = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 2B = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 2C = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 2D = "); Serial.println(SPI.transfer(0x00), HEX);
Serial.print("Reg 2E = "); Serial.println(SPI.transfer(0x00), HEX);
digitalWrite(slaveSelectPin, HIGH);
}
// Basic SPI routines to simplify code
// read and write one register
byte ADXL362::SPIreadOneRegister(byte regAddress){
byte regValue = 0;
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0B); // read instruction
SPI.transfer(regAddress);
regValue = SPI.transfer(0x00);
digitalWrite(slaveSelectPin, HIGH);
return regValue;
}
void ADXL362::SPIwriteOneRegister(byte regAddress, byte regValue){
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0A); // write instruction
SPI.transfer(regAddress);
SPI.transfer(regValue);
digitalWrite(slaveSelectPin, HIGH);
}
int16_t ADXL362::SPIreadTwoRegisters(byte regAddress){
int16_t twoRegValue = 0;
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0B); // read instruction
SPI.transfer(regAddress);
twoRegValue = SPI.transfer(0x00);
twoRegValue = twoRegValue + (SPI.transfer(0x00) << 8);
digitalWrite(slaveSelectPin, HIGH);
return twoRegValue;
}
void ADXL362::SPIwriteTwoRegisters(byte regAddress, int16_t twoRegValue){
byte twoRegValueH = twoRegValue >> 8;
byte twoRegValueL = twoRegValue;
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x0A); // write instruction
SPI.transfer(regAddress);
SPI.transfer(twoRegValueL);
SPI.transfer(twoRegValueH);
digitalWrite(slaveSelectPin, HIGH);
}
/*
Arduino Library for Analog Devices ADXL362 - Micropower 3-axis accelerometer
go to http://www.analog.com/ADXL362 for datasheet
License: CC BY-SA 3.0: Creative Commons Share-alike 3.0. Feel free
to use and abuse this code however you'd like. If you find it useful
please attribute, and SHARE-ALIKE!
Created June 2012
by Anne Mahaffey - hosted on http://annem.github.com/ADXL362
*/
#include "Arduino.h"
#ifndef ADXL362_h
#define ADXL362_h
class ADXL362
{
public:
ADXL362();
//
// Basic Device control and readback functions
//
void begin();
void beginMeasure();
int16_t readXData();
int16_t readYData();
int16_t readZData();
void readXYZTData(int16_t XData, int16_t YData, int16_t ZData, int16_t Temperature);
int16_t readTemp();
//
// Activity/Inactivity interrupt functions
//
void setupDCActivityInterrupt(int16_t threshold, byte time);
void setupDCInactivityInterrupt(int16_t threshold, int16_t time);
void setupACActivityInterrupt(int16_t threshold, byte time);
void setupACInactivityInterrupt(int16_t threshold, int16_t time);
// need to add the following functions
// void mapINT1(
// void mapINT2
// void autoSleep
// void activityInterruptControl
// -Activity, Inactivity, Both
// - Referenced, Absolute
// - Free Fall, Linked Mode, Loop Mode
void checkAllControlRegs();
// Low-level SPI control, to simplify overall coding
byte SPIreadOneRegister(byte regAddress);
void SPIwriteOneRegister(byte regAddress, byte regValue);
int16_t SPIreadTwoRegisters(byte regAddress);
void SPIwriteTwoRegisters(byte regAddress, int16_t twoRegValue);
private:
};
#endif
Hi,
i'm try use adxl with arduino mega, this library can be used with mega or is just for arduino Uno?
because the SPI ports in arduino Mega is different from Uno and Mini.
tnks
Hello, I try to connect my ADXL362 with my ARDUINO Mega 2560 on SPI.
I've connected the ADXL362 trough a Logic Level Shifter, 4-Channel, Bidirectional to the Arduino and when I try the SampleRead code, I can just see the XValue. YValue, ZValue and temperature still stay at "-1".
My code is :
`/*
ADXL362_SimpleRead.ino - Simple XYZ axis reading example
for Analog Devices ADXL362 - Micropower 3-axis accelerometer
go to http://www.analog.com/ADXL362 for datasheet
License: CC BY-SA 3.0: Creative Commons Share-alike 3.0. Feel free
to use and abuse this code however you'd like. If you find it useful
please attribute, and SHARE-ALIKE!
Created June 2012
by Anne Mahaffey - hosted on http://annem.github.com/ADXL362
Modified May 2013
by Jonathan Ruiz de Garibay
Connect SCLK, MISO, MOSI, and CSB of ADXL362 to
SCLK, MISO, MOSI, and DP 10 of Arduino
(check http://arduino.cc/en/Reference/SPI for details)
*/
#include <SPI.h>
#include <ADXL362.h>
ADXL362 xl;
int16_t temp;
int16_t XValue, YValue, ZValue, Temperature;
void setup(){
Serial.begin(9600);
xl.begin(53); // Setup SPI protocol, issue device soft reset
xl.beginMeasure(); // Switch ADXL362 to measure mode
Serial.println("Start Demo: Simple Read");
}
void loop(){
// read all three axis in burst to ensure all measurements correspond to same sample time
xl.readXYZTData(XValue, YValue, ZValue, Temperature);
Serial.print("XVALUE=");
Serial.print(XValue);
Serial.print("\tYVALUE=");
Serial.print(YValue);
Serial.print("\tZVALUE=");
Serial.print(ZValue);
Serial.print("\tTEMPERATURE=");
Serial.println(Temperature);
delay(100); // Arbitrary delay to make serial monitor easier to observe
}
`
I can see on the serial this :
If you can see, this is how I Have connected my ADXL362 :
Can you help me please ??!!
Thanks a lot
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.