Connecting ADXL345 to Nodemcu ESP8266 via SPI communication protocol

Emanue Jimenez
Posts: 2
Joined: Fri Oct 25, 2024 9:33 pm

Connecting ADXL345 to Nodemcu ESP8266 via SPI communication protocol

Postby Emanue Jimenez » Fri Oct 25, 2024 10:07 pm

Hi everyone.

I'm having problems to get the adxl345 working with the esp8266 nodemcu, with i2c there were no problems but through SPI I couldn't get a response from the sensor, or load the code.

I used the following pins

esp8266 ADXL345
3V3 ----------------> VCC
GND ----------------> GND
D8 (GPIO15) -----------> CS
D7 (GPIO13) -----------> SDA (MOSI)
D5 (GPIO14) -----------> SCL (SCK)
D6 (GPIO12) -----------> SDO (MISO)

My program:

Code: Select all

#include <SPI.h>

// Configuración de pines
const int CS_PIN = 15;  // GPIO15 para el Chip select del ADXL345

// Direcciones de registro del ADXL345
#define POWER_CTL 0x2D
#define DATA_FORMAT 0x31
#define BW_RATE 0x2C
#define DATAX0 0x32


void setup() {
  Serial.begin(115200);
  
  // Configuración de SPI en Mode 3
  SPI.begin();
  SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE3));
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH); // Mantener CS alto para deseleccionar
  
  // Configuración del ADXL345
  initADXL345();
}

void loop() {
  // Leer valores de aceleración
  int16_t x, y, z;
  readAcceleration(x, y, z);

  // Mostrar los datos
  Serial.print("X: "); Serial.print(x);
  Serial.print(" | Y: "); Serial.print(y);
  Serial.print(" | Z: "); Serial.println(z);
  
  delay(10);  // 3200 Hz implica un retraso aproximado de 0.3125 ms entre lecturas
}

// Inicialización del ADXL345 en modo de 4 hilos
void initADXL345() {
  // Configurar formato de datos (borrar el bit D6 para el modo de 4 hilos y establecer rango de +/- 16g)
  uint8_t dataFormat = readRegister(DATA_FORMAT);
  dataFormat &= ~(1 << 6); // Borra el bit D6 para el modo de 4 hilos
  dataFormat |= 0x0B;      // Configurar el rango a +/- 16g
  writeRegister(DATA_FORMAT, dataFormat);
  
  // Configurar tasa de datos a 3200 Hz
  writeRegister(BW_RATE, 0x0F);
  
  // Activar el dispositivo en modo de medición
  writeRegister(POWER_CTL, 0x08);
}

// Función para escribir en un registro del ADXL345
void writeRegister(uint8_t reg, uint8_t value) {
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(reg);
  SPI.transfer(value);
  digitalWrite(CS_PIN, HIGH);
}

// Función para leer desde un registro del ADXL345
uint8_t readRegister(uint8_t reg) {
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(reg | 0x80);  // Bit de lectura (R/W=1)
  uint8_t result = SPI.transfer(0x00);
  digitalWrite(CS_PIN, HIGH);
  return result;
}

// Leer aceleración en los tres ejes
void readAcceleration(int16_t &x, int16_t &y, int16_t &z) {
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(DATAX0 | 0xC0);  // Activar modo de lectura múltiple
  x = (SPI.transfer(0x00) | (SPI.transfer(0x00) << 8));
  y = (SPI.transfer(0x00) | (SPI.transfer(0x00) << 8));
  z = (SPI.transfer(0x00) | (SPI.transfer(0x00) << 8));
  digitalWrite(CS_PIN, HIGH);
  delay(5000);
}
I want to clarify that to load the program I must disconnect pin d8, otherwise I get the following error:

A fatal esptool.py error occurred: Failed to connect to ESP8266: Timed out waiting for packet header

When disconnecting pin d8, loading the program and reconnecting d8, all measurements obtained are 0

19:05:22.773 -> X: 0 | Y: 0 | Z: 0
19:05:27.772 -> X: 0 | Y: 0 | Z: 0
19:05:32.789 -> X: 0 | Y: 0 | Z: 0

AcmeUK
Posts: 23
Joined: Wed Dec 06, 2023 5:14 pm

Re: Connecting ADXL345 to Nodemcu ESP8266 via SPI communication protocol

Postby AcmeUK » Wed Oct 30, 2024 8:02 pm

Have you seen this new posting ADXL345 Accelerometer Interfacing with NodeMCU?
However, it uses I2C!
Do you really need SPI?

Who is online

Users browsing this forum: No registered users and 3 guests