I am trying to connect the ADXL 345 accelerometer to the particle photon. Ultimately, I want to use ifttt.com to monitor the tilt of the sensor and store the new tilt in a google spreadsheet when it moves. The first step in that process is to read the variables x,y and z using the API. I have been able to create three particle variables and read them through the API. My problem is that they are all 0 no matter how I turn and twist the accelerometer.
I am completely new to this so any advice would be much appreciated!
Here is the code I am using:
// This #include statement was automatically added by the Particle IDE.
#include "ADXL345/ADXL345.h"
// Cabling for i2c using Sparkfun breakout with a Spar Core
// Spark Core <-> Breakout board
// Gnd - GND
// 3.3v - VCC
// 3.3v - CS
// Digital 0 - SDA
// Digital 1 - SCL
#define DEVICE (0x53) // Device address as specified in data sheet
byte _buff[6];
char POWER_CTL = 0x2D; //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
int x;
int y;
int z;
int xacc;
int yacc;
int zacc;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(57000); // start serial for output. Make sure you set your Serial Monitor to the same!
Serial.print("init");
//Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
writeTo(DATA_FORMAT, 0x01);
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
writeTo(POWER_CTL, 0x08);
Particle.variable("xacc", &x, INT);
Particle.variable("yacc", &y, INT);
Particle.variable("zacc", &z, INT);
}
void loop()
{
readAccel();
delay(500); // only read every 0,5 seconds
}
void readAccel() {
uint8_t howManyBytesToRead = 6;
readFrom( DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345
// each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
// thus we are converting both bytes in to one int
x = (((int)_buff[1]) << 8) | _buff[0];
y = (((int)_buff[3]) << 8) | _buff[2];
z = (((int)_buff[5]) << 8) | _buff[4];
Serial.print("x: ");
Serial.print( x );
Serial.print(" y: ");
Serial.print( y );
Serial.print(" z: ");
Serial.println( z );
}
void writeTo(byte address, byte val) {
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
// Reads num bytes starting from address register on device in to _buff array
void readFrom(byte address, int num, byte _buff[]) {
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.requestFrom(DEVICE, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) // device may send less than requested (abnormal)
{
_buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); // end transmission
}
Thank you for your reply @kennethlimcp!
I have connected the breakout board with the photon as described in the first lines of the code (assuming that 3.3v refers to the 3v3 pin and that Digital 0 and 1 refers to pin D0 and D1, respectively). I am using a mini breadboard, so the wiring should be OK. I will check again this afternoon, when I get back home.
Do you see anything wrong with the code? In all my previous uses, I have had to declare the connections to the pins individually. As far as I understand, that is not needed in this case. But maybe I have missed something?
When connected with your computer, try opening a serial monitor. You can use the Arduino IDE if you’ve got that one. The Particle Dev will also work. The Particle CLI is also possible, as well as dedicated software like Putty for example. With each of these, you can open up a serial connection which you can use to see the Serial.print statements. That way, you can verify whether or not the sensor is read correctly and if there’s a problem in the data transmission to the cloud.
Thank you so much for taking the time to help me @Moors7!
I managed to install the Particle CLI yesterday, so I will try this tonight, when I get back home.
I just found your dashboard at http://jordymoors.nl/interface/ which I think is very useful. Unfortunately, both xacc, yacc and zacc turned out to be 0 in the dashboard as well.
I have now checked the wiring (both visually and by measuring the resistance between pins that are supposed to be connected) and everything checked out OK. I have connected the photon to the computer and run a serial monitor in the CLI. All three variables are 0 (both on the serial CLI and on the API).
If anyone could give me a clue to what I am doing wrong I would really appreciate it.
Do I need to use pull up resistances?
Is there a way to see if the breakout board is alive?
Not sure what the differences are between ADXL345 and ADXL362, but you may want to peek at the InternetButton library, which uses the ADXL362. It may suggest the proper steps to initialize and read the accelerometer, if any different from the one you took… (just my 2 cents)
Thank you for your help @peergum! I have looked through the internetbutton libraries and found that most of them use SPI protocol. The code I have found (and modified a bit) for the ADXL345 uses I2C protocol. As far as I understand, the SPI protocol requires pull up resistors and I am not really sure how to connect those.
Maybe I have not initialised the serial correctly. Line 37 in my code reads:
Serial.begin(57000); // start serial for output. Make sure you set your Serial Monitor to the same!
Maybe I have not set the monitor correctly. How do I set it to 57000?
Did you ever get the spark core or photon to talk to the ADXL350? I have tried a ton of different sample code and can’t get any solid data from the 345… I don’t really want to have to buy a different one… I mean this one should work…
Update: Got it working with the library file number 2 sample code… Had to add 4.7k ohm resisters as pull ups on D1 and D0. I also had to tie CS to VCC.