Hi, I´m struggling to get this sensor working on SPI and an ESP32, with i2c it works fine.
Hello.
It is hard to offer any useful advice without more details, so I recommend reading the troubleshooting and writing suggestions on our Support page.
We test the SPI interface on every unit, so the issue probably has to do with either your connections or your code. Can you post some pictures of your setup, including close-ups of all the soldered connections along with the simplest program that you expect to work, but does not (e.g. something that just tries to read the WhoAmI register)?
- Patrick
I´m pretty sure its a coding thing or I
m missing something.
this is my wiring:
ESP32 LSM6DSO
SCK (18) - SCL
MISO (23) - SDO
MOSI (19) - SDA
CS (5) - CS
3.3V - VDD
GND - GND
I´ve also connected SCx and SDx with Ground.
This is my last recent code:
#include <SPI.h>
// Pin-Definitionen
#define CS_PIN 5
#define SCK_PIN 18
#define MISO_PIN 19
#define MOSI_PIN 23
// LSM6DSO Register
#define LSM6DSO_WHO_AM_I 0x0F // Sollte 0x6C zurückgeben
#define LSM6DSO_CTRL3_C 0x12 // Control Register 3
#define LSM6DSO_STATUS_REG 0x1E // Statusregister (Daten verfügbar?)
// SPI-Einstellungen (1 MHz, Mode 3)
SPISettings lsm6dsoSettings(1000000, MSBFIRST, SPI_MODE3);
void setup() {
Serial.begin(115200);
while (!Serial); // Warten auf Serial Monitor
Serial.println("\nStarting SPI Connection Test...");
// SPI initialisieren
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);
// Verbindungstest
if (!checkConnection()) {
Serial.println("Kritischer Fehler! Abbruch.");
while(1);
}
// Sensor konfigurieren
initSensor();
Serial.println("System bereit!");
}
void loop() {
// Diagnosedaten alle 2 Sekunden
static uint32_t lastCheck = 0;
if (millis() - lastCheck > 2000) {
lastCheck = millis();
printDiagnostics();
}
// Daten auslesen (Beispiel: Temperatur)
float temp = readTemperature();
Serial.print("Temperatur: "); Serial.print(temp); Serial.println(" °C");
delay(500);
}
// ========== HAUPTFUNKTIONEN ========== //
bool checkConnection() {
Serial.println("\n--- Verbindungscheck ---");
// 1. SPI Bus testen
Serial.print("SPI Kommunikation... ");
SPI.beginTransaction(lsm6dsoSettings);
digitalWrite(CS_PIN, LOW);
uint8_t junk = SPI.transfer(0x55); // Testbyte senden
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
Serial.println("OK (Bus aktiv)");
// 2. WHO_AM_I Register prüfen
Serial.print("Sensor-Erkennung... ");
uint8_t id = readRegister(LSM6DSO_WHO_AM_I);
if (id != 0x6C) {
Serial.print("FEHLER (Erhalten: 0x");
Serial.print(id, HEX);
Serial.println(", Erwartet: 0x6C)");
return false;
}
Serial.println("OK (LSM6DSO erkannt)");
// 3. Register-Schreibtest
Serial.print("Register-Schreibtest... ");
writeRegister(LSM6DSO_CTRL3_C, 0x01); // SOFT_RESET bit setzen
delay(50);
uint8_t ctrl3 = readRegister(LSM6DSO_CTRL3_C);
if ((ctrl3 & 0x01) != 0x00) { // Bit sollte automatisch zurückgesetzt werden
Serial.println("FEHLER (Register schreiben/lesen)");
return false;
}
Serial.println("OK");
return true;
}
void initSensor() {
// Beschleunigungssensor: 104 Hz, ±4g
writeRegister(0x10, 0b01010000);
// Gyroskop: 104 Hz, ±500 dps
writeRegister(0x11, 0b01010000);
// FIFO aktivieren (optional)
writeRegister(0x08, 0x40);
}
void printDiagnostics() {
Serial.println("\n--- Systemdiagnose ---");
// SPI-Signale prüfen
Serial.print("CS Pin: "); Serial.println(digitalRead(CS_PIN));
Serial.print("SCK Pin: "); Serial.println(digitalRead(SCK_PIN));
Serial.print("MISO Pin:"); Serial.println(digitalRead(MISO_PIN));
Serial.print("MOSI Pin:"); Serial.println(digitalRead(MOSI_PIN));
// Sensor-Status
uint8_t status = readRegister(LSM6DSO_STATUS_REG);
Serial.print("Status: 0x"); Serial.println(status, HEX);
Serial.println((status & 0x01) ? "Gyro-Daten bereit" : "Gyro-Daten nicht bereit");
Serial.println((status & 0x02) ? "Accel-Daten bereit" : "Accel-Daten nicht bereit");
}
// ========== HILFSFUNKTIONEN ========== //
float readTemperature() {
int16_t temp = (readRegister(0x21) << 8) | readRegister(0x20);
return (temp / 256.0) + 25.0; // Umrechnung laut Datenblatt
}
uint8_t readRegister(uint8_t reg) {
SPI.beginTransaction(lsm6dsoSettings);
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | 0x80);
uint8_t value = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void writeRegister(uint8_t reg, uint8_t value) {
SPI.beginTransaction(lsm6dsoSettings);
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg & 0x7F);
SPI.transfer(value);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
}
I`ve tried also a lot of other examples, no luck so far.
All of your connections seems okay, though I cannot trace them back to the ESP32 pins.
Can you tell us what you expect to see from your program and post a Serial Monitor output log that shows what the program reports?
- Patrick