2025-03-11 6:55 AM - last edited on 2025-03-11 9:28 AM by Peter BENSCH
Hi,
I designed a special board that includes the LSM6DSO32 sensor. I want to get step count data, for testing I am using ESP32-CAM with LSM6DSO32 via I2C. I have followed the basic routine mentioned in application notes. I can get only garbage value. I will attached sample code for reference. Can you check and help me step by step what to do? First code give step count 0 always. For second code I attached screenshot. Please look into it and tell if anything need to change.
#include <Wire.h>
// LSM6DSO32 I2C address
#define LSM6DSO32_ADDR 0x6B
// Register addresses
#define FUNC_CFG_ACCESS 0x01
#define PAGE_RW 0x17 //0x02
#define PAGE_SEL 0x02 //0x03
#define PAGE_ADDR 0x08 //0x04
#define PAGE_VALUE 0x09 //0x05
#define EMB_FUNC_EN_A 0x04
#define EMB_FUNC_EN_B 0x05
#define EMB_FUNC_INT1 0x0A
#define MD1_CFG 0x5E
#define CTRL1_XL 0x10
#define WHO_AM_I 0x0F
#define STEP_COUNTER_L 0x62
#define STEP_COUNTER_H 0x63
#define EMB_FUNC_SRC 0x64
#define PEDO_CMD_REG 0x83
void setup() {
Serial.begin(115200);
delay(1000);
Wire.begin(13, 14); // SDA = GPIO13, SCL = GPIO14
// Test communication with WHO_AM_I register
byte whoAmI = readRegister(WHO_AM_I);
Serial.print("WHO_AM_I: 0x");
Serial.println(whoAmI, HEX);
/*if (whoAmI != 0x6C) {
Serial.println("Sensor not found!");
while (1); // Stop execution if sensor is not found
}*/
if (whoAmI != 0x6C) {
Serial.println("Sensor is found!");
while (1); // Stop execution if sensor is not found
}
// Initialize the sensor
initSensor();
// Reset the step counter using 0x04
resetStepCounter();
}
void loop() {
// Check step detection status (read raw status register)
byte stepStatus = readRegister(EMB_FUNC_SRC);
Serial.print("Step Detection Status: 0x");
Serial.println(stepStatus, HEX);
if (stepStatus & (1 << 7)) { // Step detected bit is set
Serial.println("Step Detected!");
}
// Read the step count (raw step counter values)
uint16_t stepCount = readStepCounter();
Serial.print("Step Count: ");
Serial.println(stepCount);
// Read and print the raw step counter registers
byte stepLow = readRegister(STEP_COUNTER_L);
byte stepHigh = readRegister(STEP_COUNTER_H);
Serial.print("Raw STEP_COUNTER_L: 0x");
Serial.print(stepLow, HEX);
Serial.print(", STEP_COUNTER_H: 0x");
Serial.println(stepHigh, HEX);
delay(1000); // Delay for 1 second before next read
}
void initSensor() {
Serial.println("Initializing Sensor...");
// Step 1: Enable access to embedded functions registers
writeRegister(FUNC_CFG_ACCESS, 0x80);
// Step 2: Select write operation mode
writeRegister(PAGE_RW, 0x40);
// Step 3: Select page 1
writeRegister(PAGE_SEL, 0x11);
// Step 4: Set the embedded advanced features register (PEDO_CMD_REG)
writeRegister(PAGE_ADDR, 0x83);
// Step 5: Enable false positive rejection block
writeRegister(PAGE_VALUE, 0x04);
// Step 6: Disable write operation mode
writeRegister(PAGE_RW, 0x00);
// Step 7: Enable pedometer
writeRegister(EMB_FUNC_EN_A, 0x08);
// Step 8: Enable pedometer false-positive rejection block and advanced detection feature
writeRegister(EMB_FUNC_EN_B, 0x10);
// Step 9: Route step detection interrupt to INT1 pin (temporarily disable this for now)
// writeRegister(EMB_FUNC_INT1, 0x08);
// Step 10: Disable access to embedded functions registers
writeRegister(FUNC_CFG_ACCESS, 0x00);
// Step 11: Enable routing of the embedded function interrupts
writeRegister(MD1_CFG, 0x02);
// Step 12: Enable accelerometer (26 Hz, ±8g)
writeRegister(CTRL1_XL, 0x28);
delay(2000); // Increase the delay for initialization time
Serial.println("Sensor Initialized!");
}
uint16_t readStepCounter() {
byte stepCountLow = readRegister(STEP_COUNTER_L); // Read low byte
byte stepCountHigh = readRegister(STEP_COUNTER_H); // Read high byte
uint16_t stepCount = (stepCountHigh << | stepCountLow; // Combine into 16-bit unsigned value
return stepCount;
}
void resetStepCounter() {
Serial.println("Resetting Step Counter...");
writeRegister(PEDO_CMD_REG, 0x04); // Reset the step counter (use 0x04 instead of 0x01)
delay(500); // Wait for reset to take effect
// Verify the reset by reading step count
uint16_t stepCount = readStepCounter();
Serial.print("Step Counter After Reset: ");
Serial.println(stepCount);
}
byte readRegister(byte reg) {
Wire.beginTransmission(LSM6DSO32_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(LSM6DSO32_ADDR, 1);
return Wire.available() ? Wire.read() : 0;
}
void writeRegister(byte reg, byte value) {
Wire.beginTransmission(LSM6DSO32_ADDR);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
I would appreciate if you some ways to get step count data, that would be great.
Thanks You
#include <Wire.h>
// LSM6DSO32 I2C address
#define LSM6DSO32_ADDR 0x6B // Default 0x6B
// Register addresses
#define FUNC_CFG_ACCESS 0x01
#define PAGE_RW 0x17
#define PAGE_SEL 0x02
#define PAGE_ADDR 0x08
#define PAGE_VALUE 0x09
#define EMB_FUNC_EN_A 0x04
#define EMB_FUNC_EN_B 0x05
#define EMB_FUNC_INT1 0x0A
#define MD1_CFG 0x5E
#define CTRL1_XL 0x10
#define WHO_AM_I 0x0F
#define STEP_COUNTER_L 0x4B // Corrected step counter low byte register
#define STEP_COUNTER_H 0x4C // Corrected step counter high byte register
#define EMB_FUNC_SRC 0x1A // Corrected step detection status register
#define PEDO_CMD_REG 0x5E // Step counter reset register
void setup() {
Serial.begin(115200);
delay(1000);
Wire.begin(13, 14); // SDA = GPIO13, SCL = GPIO14
// Initialize the sensor
initSensor();
// Verify WHO_AM_I register
byte whoAmI = readRegister(WHO_AM_I);
if (whoAmI == 0x6C) { // WHO_AM_I fixed at 6Ch for LSM6DSO32
Serial.println("Successfully connected to sensor!");
} else {
Serial.print("Failed to connect. WHO_AM_I: 0x");
Serial.println(whoAmI, HEX);
while (1); // Stop execution if sensor is not detected
}
}
void loop() {
// Read step detection status
byte stepStatus = readRegister(EMB_FUNC_SRC);
if (stepStatus & (1 << 7)) { // Correct step detection bit (bit 7)
Serial.println("Step Detected!");
}
// Read step count
int16_t stepCount = readStepCounter();
Serial.print("Step Count: ");
Serial.println(stepCount);
delay(1000); // Read every second
}
void initSensor() {
Serial.println("Initializing Sensor...");
// Enable embedded function configuration
writeRegister(FUNC_CFG_ACCESS, 0x80);
// Select Page 1
writeRegister(PAGE_RW, 0x40);
writeRegister(PAGE_SEL, 0x11);
// Enable advanced detection
writeRegister(PAGE_ADDR, 0x83);
writeRegister(PAGE_VALUE, 0x04); // AD_DET_EN
// Disable page write access
writeRegister(PAGE_RW, 0x00);
// Enable step counter & detection
writeRegister(EMB_FUNC_EN_A, 0x08); // Enable step counter
writeRegister(EMB_FUNC_EN_B, 0x10); // Enable step detection
// Route step detection to INT1
writeRegister(EMB_FUNC_INT1, 0x08);
// Exit embedded functions access
writeRegister(FUNC_CFG_ACCESS, 0x00);
// Enable routing the embedded functions interrupt
writeRegister(MD1_CFG, 0x02);
// Enable accelerometer (26 Hz, ±8g)
writeRegister(CTRL1_XL, 0x28);
delay(1000); // Allow pedometer time to initialize
Serial.println("Sensor Initialized!");
}
int16_t readStepCounter() {
byte stepCountLow = readRegister(STEP_COUNTER_L); // Read low byte
byte stepCountHigh = readRegister(STEP_COUNTER_H); // Read high byte
int16_t stepCount = (stepCountHigh << | stepCountLow;
return stepCount;
}
void resetStepCounter() {
Serial.println("Resetting Step Counter...");
writeRegister(PEDO_CMD_REG, 0x01); // Write reset command
delay(100); // Wait for reset to take effect
// Verify reset
int16_t stepCount = readStepCounter();
Serial.print("Step Counter After Reset: ");
Serial.println(stepCount);
}
byte readRegister(byte reg) {
Wire.beginTransmission(LSM6DSO32_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(LSM6DSO32_ADDR, 1);
return Wire.available() ? Wire.read() : 0;
}
void writeRegister(byte reg, byte value) {
Wire.beginTransmission(LSM6DSO32_ADDR);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}