I suspect I'm about to make a dumb question but I've been strugling for the past 2 days trying to just make a simple reading of the accelerometer. Just that get those values out of X, Y and Z axis...
I can get the correct responses out of the WHO_AM_I register and the status from the other setup registers. No matter what I do, the readings from registers 0x28~0x2D keep returning zero. I need help to understand what I may be doing wrong...
#include <Arduino.h>
#include <Wire.h>
#define i2c_sda 8
#define i2c_scl 9
#define x_l 0x28
#define x_h 0x29
#define y_l 0x2A
#define y_h 0x2B
#define z_l 0x2C
#define z_h 0x2D
uint8_t addr;
uint8_t outx_l;
uint8_t outx_h;
uint8_t outy_l;
uint8_t outy_h;
uint8_t outz_l;
uint8_t outz_h;
uint16_t X;
uint16_t Y;
uint16_t Z;
uint16_t msb;
uint8_t data;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\nI2C Scanner");
delay(1000);
Serial.println ("I2C scanner. Scanning ...");
Wire.begin(static_cast<int>(i2c_sda), static_cast<int>(i2c_scl), static_cast<uint32_t>(100000));
byte count = 0;
for (byte i = 8; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
addr = i;
delay (1);
}
}
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
delay(1000);
// Turn on the accl // ODR = 200 Hz, High-Performance
Wire.beginTransmission(addr);
Wire.write(0x10);
Wire.write(0x64);
Wire.endTransmission(true);
// FS ±2 g, one-shot using interface
Wire.beginTransmission(addr);
Wire.write(0x14);
Wire.write(0xF0);
Wire.endTransmission(true);
}
void loop() {
//sadw = addr << 1;
//sadr = sadw | 1;
delay(25);
//-------------------------------------------------------------------
Wire.beginTransmission(addr);
Wire.write(x_l);
Wire.endTransmission(false);
Wire.requestFrom(addr, 1);
if (Wire.available()) {
outx_l = Wire.read();
}
Wire.endTransmission(true);
delay(25);
//get MSBs
Wire.beginTransmission(addr);
Wire.write(x_h);
Wire.endTransmission(false);
Wire.requestFrom(addr, 1);
if (Wire.available()) {
outx_h = Wire.read();
Serial.println(outx_h, HEX);
}
Wire.endTransmission(true);
delay(25);
msb = outx_h;
X = (msb << 8) | outx_l;
Serial.println(X, HEX);
delay(1000);
}
======================================================
and on platformio.ini:
[env:esp32-s3-devkitc-1-n16r8v]
platform = espressif32
board = esp32-s3-devkitc-1-n16r8v
framework = arduino
monitor_speed = 115200