Thursday, April 19, 2012

ADXL345 accelerometer integration with TelosB mote

Here is a very simple NesC code for getting X, Y and Z axis value from ADXL345 digital accelerometer with Telosb mote.

Connection
 
ADXL345 GND -> TELOSB 9 pin
ADXL345 VCC  -> TELOSB 1 pin
ADXL345 CS     -> TELOSB 1 pin
ADXL345 SDA  -> TELOSB 8 pin 
ADXL345 SCL  -> TELOSB 6 pin  

Apart from this connect pull-up resistor(10 KOhm) from SDA &SCL to VCC.

Program

Program has  three main files Accelerometer345AppC.nc, Accelerometer345C.nc and Makefile.

Acclerometer345AppC.nc
configuration Accelerometer345AppC {
}

implementation {
  components MainC, Accelerometer345C as App;
  App.Boot -> MainC;
  
  components LedsC;
  App.Leds -> LedsC;

  components new TimerMilliC() as TimerAccel;
  App.TimerAccel -> TimerAccel;

  components new ADXL345C();
  App.Xaxis -> ADXL345C.X;
  App.Yaxis -> ADXL345C.Y;
  App.Zaxis -> ADXL345C.Z;
  App.AccelControl -> ADXL345C.SplitControl;
}

Accelerometer345C.nc
#include "printf.h"

module Accelerometer345C {
  uses {
    interface Boot;
    interface Leds;
    interface Timer<TMilli> as TimerAccel;
    interface Read<uint16_t> as Xaxis;
    interface Read<uint16_t> as Yaxis;
    interface Read<uint16_t> as Zaxis;
    interface SplitControl as AccelControl;
  }
}

implementation {
  event void Boot.booted() {
    call AccelControl.start();
  }

  event void AccelControl.startDone(error_t err) {
    printf("\n\n-----Accelerometer Started-----\n\n");
    call TimerAccel.startPeriodic(1000);
  }

  event void AccelControl.stopDone(error_t err) {
  }

  event void TimerAccel.fired() {
    call Leds.led0Toggle();
    call Xaxis.read();   
  }

  event void Xaxis.readDone(error_t result, uint16_t data) {
    printf("X [%d]  ",data);
    call Yaxis.read();
  }

  event void Yaxis.readDone(error_t result, uint16_t data) {
    printf("Y [%d]  ",data);
    call Zaxis.read();
  }
  event void Zaxis.readDone(error_t result, uint16_t data) {
    printf("Z [%d]\n\n",data);
    printfflush();
  }
} 

Makefile
COMPONENT=Accelerometer345AppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/adxl345

include $(MAKERULES) 

6 comments:

  1. Hey, where you able to solve the "syntax error before '0x20' " compilation error for ADXL345C.nc.... I am not able to understand what to do in this case...

    ReplyDelete
  2. in ADXL345.C try to replace this line:
    components new Msp430I2C1C() as I2C;

    with this one:
    components new Msp430I2CC() as BusI2C;

    ReplyDelete
  3. hi sir.
    i followed your instruction, but still cannot read data from sensor. the output always be
    X [16413] Y [16413] Z [16413]
    , please help me out.

    ReplyDelete
    Replies
    1. Hi I am getting the same output.
      Could you please share the solution to this problem?

      Delete
  4. Hi every one ,
    I am trying to use ADX345 for implementing fall detection algorithm in WSN for my theses . The problem is the driver package in tinyos is not for Telosb .How you guys modified it for telosb ? Please guide me I am really got stuck here Thanks in advance

    ReplyDelete
  5. After following the instructions, I am only getting the following output.
    X [16413] Y [16413] Z [16413]
    Please help!

    ReplyDelete