Communication Electric Imp (April Board) <-> ADXL345

Hi all,

the title of this topic does not give much information about my doubts, but here they are.

At this point communication between electric and accelerometer adxl345 imp is being done through the i2c89.

I can read values ​​from the accelerometer, however the problem is that the imp is the master, ie, the imp is doing reading adxl345.
What I wanted to do that adxl345 send your data only when detects movement.

Even if the imp is in sleep mode, I wanted to wake him when the adxl345 detect movement. Wanted to make a kind of interruption.
Is this possible?

This is the datasheet of ADXL345: https://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf

If you need I can post the code I have at the moment.

It looks like there are two interrupt pins on that IC. You would hook up the int1 or int2 pins to PIN1 on the imp - and then only read the I2C data line when the interrupt is triggered.

I haven’t worked with that IC, but it looks like Page 12 of the datasheet details how the use the interrupts.

Thanks for the reply.

Yes, the accelerometer has two pin for interrupts, the question is how do I detect this interruption in imp.

I’ll have to have an active waiting? The energy consumption is critical for me.

You want to hook the interrupt pin from the IC up to pin1 of the imp and configure it to be a DIGITAL_IN_WAKEUP.

Then you can write code like this (I wrote this in the forums editor, no promise it will work):

`hardware.pin1.configure(DIGITAL_IN_WAKEUP);

// if we woke up because of a trigger, read the IC
if(hardware.wakereason() == WAKEREASON_PIN1) {
// read the accelerometer with I2C
}

// go to sleep for 12 hours
imp.onidle(function() { server.sleepfor(43200); });`

(edited to have brackets on wakereason)

Hi I have a problem with the ADXL345 since you are using it I want to ask you how do you write the set up into the registers (I am using SPI), because I just noticed that all this time I have been just declaring it as global variables.
`#define ADXL345_DEVICE 0x53;
// Global variables
values <- [0,0,0,0,0,0,0,0,0,0];
x <- 0;
y <- 0;
z <- 0;

//Registers
DATA_FORMAT <-0x31;
DATAX0 <- 0x32; //X-Axis Data 0
DATAX1 <- 0x33; //X-Axis Data 1
DATAY0 <- 0x34; //Y-Axis Data 0
DATAY1 <- 0x35; //Y-Axis Data 1
DATAZ0 <- 0x36; //Z-Axis Data 0
DATAZ1 <- 0x37; //Z-Axis Data 1
//Constants
const ADXL345_DEVICE = 0x53;
const POWER_CTL = 0x2D; // Power control register
//program start
hardware.spi257.configure(SIMPLEX_TX|SIMPLEX_RX|LSB_FIRST|CLOCK_IDLE_HIGH|CLOCK_2ND_EDGE, 500);
hardware.spi189.configure(CLOCK_IDLE_LOW, 500);`
I also have the CS to pin 1, GND <- GND, VCC <- 3V3, SDO <- PIN2, SDA<-PIN7, SCL<-PIN5.

@beardedinventor
Thanks! I will try this way and i give news.

@LeonKuro
I could not get it working through the SPI, I’ll try to read about it in the datasheet and I say something.

Hi again, right now I’m with this problem:

I used the code that @beardedinventor sent and that configuration on pin1 (hardware.pin1.configure(DIGITAL_IN_WAKEUP)) causes an infinite loop.

The pin int1(of the ADXL345) is always sending voltage, the problem may be the configuration of the interrupt accelerometer.

Any sugestion?
Thanks

Hey again! Well I have been investigating all this time and some places it says even if I am using SPI I still need to use the int1 or int 2 or both of the interrupts to make it work, do you think is really necessary? Thanks

Hi @LeonKuro, depends what do you want to do.

If you want read the accelerometer continuously you don’t need any interrupt.
If you want, for example, wake imp when accelerometer detects movement (thats what i want), you need interrupt.

Well I just need to read continuously the data from x, y and z. In this case no need for an interrupt line ah?

Thats it!
You don’t need an interrupt.

Yes I understand, I just need some more help in writing the SPI code cuz, I am not really sure how to access registers and once accessed how to write a value to it to program it, after that I would need to read out the data outputs from x, y, z.

If you could give me the I2C version of the code that will be a big help, just in case I cant get the SPI working; this is how my SPI code is currently:
`#define ADXL345_DEVICE 0x53;
//pin 1 is CS and should be driven high unless transmitting
hardware.pin1.configure(DIGITAL_OUT);
hardware.pin1.write(1); // nCS high by default

//program start
hardware.spi257.configure(CLOCK_IDLE_HIGH|CLOCK_2ND_EDGE, 500);

function adxl345_write(reg, value) {
// See ADXL345 datasheet figure 37
hardware.pin1.write(0); // set nCS low
// R/nW=0 MB=0 so first byte is just the register number
hardware.spi257.write(reg.tochar(0) + value.tochar(0));
hardware.spi257.write(reg.tochar(0x2D) + value.tochar(0x08));
hardware.spi257.write(reg.tochar(0X31) + value.tochar(0x08));
hardware.spi257.write(reg.tochar(0X32));
hardware.spi257.write(reg.tochar(0X33));
hardware.spi257.write(reg.tochar(0X34));
hardware.spi257.write(reg.tochar(0X35));
hardware.spi257.write(reg.tochar(0X36));
hardware.spi257.write(reg.tochar(0X37));
hardware.pin1.write(1); // nCS back high again
}

function adxl345_read(reg) {
hardware.pin1.write(0);
// R/nW=1 MB=0, second byte doesn’t matter
local ret = hardware.spi257.write((reg+0x80).tochar(0x32) + "This is value: ")
hardware.pin1.write(1);
return ret[1];
}`
Thanks
ps. Also I have no idea how to show the data…

Hi @LeonKuro, sorry for delay…

This is the code to make I2C reading:

function accelerometer(){ // i2c89 Interface ADXL345

// Imp Pins
// Pin 8 = SCL
// Pin 9 = SDA

// set the i2c clock speed.
// ADXL345 can handle between 100kHz and 400kHz

hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);

// read i2c
// let’s read 6 bytes (2 bytes for each axis)
local bytes=hardware.i2c89.read(0xA7, “\x32”, 6);

if (typeof(bytes)!= “string”) {
server.log("Erro leitura i2c: "+bytes);
return;
}

local x = bytes[0];
local y = bytes[2];
local z = bytes[4];

server.log("X: "+x);
server.log("Y: "+y);
server.log("Z: "+z);

imp.wakeup(60, accelerometer);
}

accelerometer();

Sorry for the previous post.

`function accelerometer(){
// i2c89 Interface ADXL345

// Imp Pins
// Pin 8 = SCL
// Pin 9 = SDA

// set the i2c clock speed.
// ADXL345 can handle between 100kHz and 400kHz

hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);

// read i2c
// let’s read 6 bytes (2 bytes for each axis)
local bytes=hardware.i2c89.read(0xA7, “\x32”, 6);

if (typeof(bytes)!= “string”) {
server.log("Erro leitura i2c: "+bytes);
return;
}

local x = bytes[0];
local y = bytes[2];
local z = bytes[4];

server.log("X: "+x);
server.log("Y: "+y);
server.log("Z: "+z);

imp.wakeup(60, accelerometer);
}

accelerometer();`

wow awesome is simpler than i tough, if you dont mind i will begin playing with the code. :smiley:

Yes, its very simple!

Of course i don’t mind :smiley: