Arduino ADXL_345

Hello Everyone,

I have interfaced Esp8266 with ADXL345 using SPI interface and I am able to interface and read the data properly.

My doubt is Actually in the code I have configured ADXL345 register to ±16g and Full resolution (13 bit) but I am receiving the data only around 8 to 9 bit signed integer data. This code is not able to achieve till 13 bit of data.

Below is the code given with this query.

//https://forum.allaboutcircuits.com/threads/adxl345-arduino-uno-data_ready-interrupt.107207/   code link
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

//ADXL345
#define INTERVAL 10 //10
#define SCLpin   14 //D5//GPIO 14 Serial clock pin
#define SDOpin   12 //D6//GPIO 12 MISO pin
#define SDApin   13 //D7//GPIO 13 MOSI pin
#define SSpin     5 //D1 //15 //D8//GPIO 15 CHip Select D8
//Nodemcu pin 3.3 -----> pin VCC ADXL345
//Nodemcu pin GND -----> pin GND ADXL345

char BW_RATE = 0x2C;   // Data Rate and power mode control
char POWER_CTL=0x2D;   // Power Control Register
char DATA_FORMAT=0x31; // Data Format Register
char DATAX0=0x32;
char DATAX1=0x33;
char DATAY0=0x34;
char DATAY1=0x35;
char DATAZ0=0x36;
char DATAZ1=0x37;

int X_Data, Y_Data, Z_Data;


char str[10];
long lastMsg=0;
byte TO_READ = 6;

WiFiClient espClient;
PubSubClient client(espClient);


void setup_wifi()
{
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void reconnect()
{
  while (!client.connected()) 
  {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
      client.publish("outTopic", "My First MQTT Code");
      client.subscribe("Welcome ESP8266");
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
void setup()
{
  Serial.begin(115200);
  SPI.begin();  
  SPI.beginTransaction(SPISettings(5000000,MSBFIRST,SPI_MODE3));//max 40 MHz
  
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  
  pinMode(SSpin , OUTPUT);
  digitalWrite(SSpin , HIGH);
  
  writeRegister(POWER_CTL, 0x00);// Configured in Stand By Mode
  writeRegister(POWER_CTL, 0x08); // Configured in Measurement Mode 
  writeRegister(DATA_FORMAT, 0x0B); // ±16g 13bit, 4mg/LSB.
  writeRegister(BW_RATE, 0x0F); // Baud Rate Register configuration. 3200 Hz
}

void loop()
{
  if (!client.connected())
  {
    reconnect();
  } 

  long now = millis();
  if (now - lastMsg > INTERVAL)
  {
    lastMsg = now;
    read_345();
    client.publish("ADXL_345",str);
  }
   client.loop();
}
void read_345()
{
// DATAX0 
  readRegister(DATAX0, TO_READ, str); 
  X_Data= ((int)str[1] << 8)  | (int)str[0];
  Y_Data= ((int)str[3] << 8)  | (int)str[2];
  Z_Data= ((int)str[5] << 8)  | (int)str[4];

  sprintf(str,"%d %d %d",X_Data,Y_Data,Z_Data);
  Serial.println(str);
 
}

void writeRegister(char registerAddress, char value) 
{
    digitalWrite(SSpin , LOW);
    SPI.transfer(registerAddress);
    SPI.transfer(value);
    digitalWrite(SSpin , HIGH);
    SPI.endTransaction();
}

void readRegister(char registerAddress, int numBytes, char * str) 
{
    char address = (0x80 | registerAddress);
    if (numBytes > 1)address = address | 0x40;
    digitalWrite(SSpin , LOW);
    SPI.transfer(address);
    
       for (int i = 0; i < numBytes; i++)
       {
            str[i] = SPI.transfer(0x00);
       }
       
    digitalWrite(SSpin , HIGH);
    SPI.endTransaction();
}

My doubt is Actually in the code I have configured ADXL345 register to +-16g and Full resolution (13 bit) but I am receiving the data only around 8 to 9 bit signed integer data.

How do you know? Are the lowest 4 bits always 0?

Hi,

Thank you for your replyNo I have not checked it bit wise, Only with respect to data which I am receiving from the accelerometer on that basis I have asked this question?,

I will try to check it bitwise also.

Thanks
Anurag

Only with respect to data which I am receiving from the accelerometer on that basis I have asked this question?,

The show us that data and tell us also what you expected.

I am new to the field of IoT. currently working on developing air mouse project using adxl345 and nodemcu amica board. I am using your code with changes I am not publishing on the net. But I am getting the output as

0 0 0
0 0 0

what is the problem. Please help me it is urgent.

#include <SPI.h>

//ADXL345
#define INTERVAL 10 //10
#define SCLpin 14 //D5//GPIO 14 Serial clock pin
#define SDOpin 12 //D6//GPIO 12 MISO pin
#define SDApin 13 //D7//GPIO 13 MOSI pin
#define SSpin 5 //D1 //15 //D8//GPIO 15 CHip Select D8
//Nodemcu pin 3.3 -----> pin VCC ADXL345
//Nodemcu pin GND -----> pin GND ADXL345

char BW_RATE = 0x2C; // Data Rate and power mode control
char POWER_CTL=0x2D; // Power Control Register
char DATA_FORMAT=0x31; // Data Format Register
char DATAX0=0x32;
char DATAX1=0x33;
char DATAY0=0x34;
char DATAY1=0x35;
char DATAZ0=0x36;
char DATAZ1=0x37;

int X_Data, Y_Data, Z_Data;

char str[10];
long lastMsg=0;
byte TO_READ = 6;

void setup()
{
Serial.begin(9600);
SPI.begin();
SPI.beginTransaction(SPISettings(5000000,MSBFIRST,SPI_MODE3));//max 40 MHz

pinMode(SSpin , OUTPUT);
digitalWrite(SSpin , HIGH);

writeRegister(POWER_CTL, 0x00);// Configured in Stand By Mode
writeRegister(POWER_CTL, 0x08); // Configured in Measurement Mode
writeRegister(DATA_FORMAT, 0x0B); // ±16g 13bit, 4mg/LSB.
writeRegister(BW_RATE, 0x0F); // Baud Rate Register configuration. 3200 Hz
}

void loop()
{

long now = millis();
if (now - lastMsg > INTERVAL)
{
lastMsg = now;
read_345();

}

}
void read_345()
{
// DATAX0
readRegister(DATAX0, TO_READ, str);
X_Data= ((int)str[1] << 8) | (int)str[0];
Y_Data= ((int)str[3] << 8) | (int)str[2];
Z_Data= ((int)str[5] << 8) | (int)str[4];

sprintf(str,"%d %d %d",X_Data,Y_Data,Z_Data);
Serial.println(str);

}

void writeRegister(char registerAddress, char value)
{
digitalWrite(SSpin , LOW);
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(SSpin , HIGH);
SPI.endTransaction();
}

void readRegister(char registerAddress, int numBytes, char * str)
{
char address = (0x80 | registerAddress);
if (numBytes > 1)address = address | 0x40;
digitalWrite(SSpin , LOW);
SPI.transfer(address);

for (int i = 0; i < numBytes; i++)
{
str = SPI.transfer(0x00);

  • }*

  • digitalWrite(SSpin , HIGH);*

  • SPI.endTransaction();*
    }