ADXL 345 Digitaler Beschleunigungssensor

Der ADXL345 (GY-291) ist ein kleiner, dünner, extrem Stromsparender 3-Achsen-Beschleunigungssensor mit einer hohen Auflösung (13-bit) bei Beschleunigungswerten von ± 16 g. Der Messbereich kann auf folgende Bereiche angepasst werden: +-2g, +-4g, +-8g oder +- 16g.


ADXL 345

    ADXL345 Software Interface
  by e-Gizmo Mechatronix Central
  This program uses a special software technique
  to allow direct and safe interfacing of a 3.3V ADXL345
  device (e-Gizmo ADXL345 breakout board) with the
  5V logic interface of the gizDuino/Arduino I/O bus
  Usage terms: 
  Free, as long as you agree to make us
  not liable for any bad things that may happen
  with the use of this code. Please keep our name
  on the credit.

// Pin usage, change assignment if you want to
const byte  spiclk=17;    // connect to ADXL CLK
const byte  spimiso=16;  // connect to ADXL DO
const byte spimosi=15;  // connect to ADXL DI
const byte spics=14;    // connect to ADXL CS
// Don't forget, connect ADXL VDD-GND to gizDuino/Arduino +3.3V-GND

byte  xyz[8];  // raw data storage
int x,y,z;    // x, y, z accelerometer data

byte spiread;

void setup(void){
  Serial.begin(9600);      // serial i/o for test output
  init_adxl();            // initialize ADXL345

void loop(void){

  read_xyz();            // read ADXL345 accelerometer

  // and then send results to serial port
  // view results by using IDE Tools>Serial Monitor
  Serial.print("x = ");
  Serial.print("     y = ");
  Serial.print("     z = ");


    Bit bang SPI function 
  All SPI interface pins of the ADXL345 must be provided
  with pull-up resistors (to 3.3V, 3.3Kto 10K ohm) in order
  to work using this code.e-Gizmo ADXL345 breakout board
  already has these parts on board, hence is ready for use
  without any modifications.
  Principle of operation:
  A 3.3V logic 1 output is effected by configuring
  the driving pin as input, letting the pull up resistor
  take the logic level up to 3.3V only. A logic 0 output
  is generated by configuring the driving pin to output.

void spi_out(byte spidat){
  byte bitnum=8;

    // start spi bit bang
      pinMode(spiclk,OUTPUT);    // SPI CLK =0
      if((spidat & 0x80)!=0)
        pinMode(spimosi,INPUT);  // MOSI = 1 if MSB =1
        pinMode(spimosi,OUTPUT);  // else MOSI = 0
      pinMode(spiclk,INPUT);  // SPI CLK = 1
      // read spi data
      if(digitalRead(spimiso)==HIGH) spiread |= 0x01; // shift in a 1 if MISO is 1

      pinMode(spimosi,INPUT);  // reset MOSI to 1


/*  Initialize ADXL345 */

void  init_adxl(void){
  pinMode(spics,OUTPUT);  // CS=0   
  //Write to register 0x31, DATA FORMAT
  // uncomment your desired range
  spi_out(0x0B); //full resolution, +/- 16g range
  //spi_out(0x0A); //full resolution, +/- 8g range
  //spi_out(0x09); //full resolution, +/- 4g range
 // spi_out(0x08); //full resolution, +/- 2g range
  pinMode(spics,INPUT);  //CS HIGH
    pinMode(spics,OUTPUT);  // CS=0   
  // Write to register 0x2d, POWER_CTL
  //set to measure mode
  pinMode(spics,INPUT);  //CS HIGH

   Read all 3 axis x,y,z

void read_xyz(void){
  int i;
    pinMode(spics,OUTPUT);  // CS=0   
  //Set start address to 0x32
  //D7= 1 for read and D6=1 for sequential read
  // dump xyz content to array
  // merge to convert to 16 bits
  x=((int)xyz[1]<<8) + xyz[0];
  y=((int)xyz[3]<<8) + xyz[2];
  z=((int)xyz[5]<<8) + xyz[4];
  pinMode(spics,INPUT);  //CS HIGH