MPU9250 - SPI -read

Hello,
I’m struggling to get MP9250 work with Arduino via SPI. The skecth is:

#include <SPI.h>
#define SDA_PIN         10          // Configurable, see typical pin layout above
#define SPICLOCK        (4000000u)  

void printCharWithLeading(char c) {
  for (unsigned int mask = 0x80; mask; mask >>= 1) {
    Serial.print(mask&c ? '1':'0');
  }
}

byte SPIwriteRegister(char reg, byte b) {
  Serial.print  ("SPIwriteRegister: ");  
  Serial.print(reg, HEX);  
  Serial.print  (" -  ");  
  printCharWithLeading(b); 
  Serial.println(); 
  SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE3)); // Set the settings to work with SPI bus
  digitalWrite(SDA_PIN, LOW);       // Select slave
  SPI.transfer(reg);                // 
  SPI.transfer(b);                  // Write data 
  SPI.endTransaction();             // Stop using the SPI bus
  return;  
} // End SPIwriteRegister

byte SPIreadRegister(char reg) {
  Serial.print  ("SPIreadRegister: ");  
  Serial.println(reg, HEX);    
  byte value = 0xFF;
  SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE3)); // Set the settings to work with SPI bus
  digitalWrite(SDA_PIN, LOW);       // Select slave
  SPI.transfer(0x80 | reg);         // MSB == 1 is for reading. LSB is not used in address. Datasheet section 8.1.2.3.
  value = SPI.transfer(0);          // Read the value back. Send 0 to stop reading.           
  digitalWrite(SDA_PIN, HIGH);      // Release slave again
  SPI.endTransaction();             // Stop using the SPI bus
  return value;
} // End SPIreadRegister

void initMPU9250 () {
  byte error;
  char c;
  // Set the chipSelectPin as digital output, do not select the slave yet
  Serial.print  ("SDA_PIN: ");  
  Serial.println(SDA_PIN);
  pinMode     (SDA_PIN, OUTPUT);
  // Toggle CS pin to lock in SPI mode
  digitalWrite(SDA_PIN, LOW);
  delay(1);
  digitalWrite(SDA_PIN, HIGH);
  delay(1);    
  

  // write Register PWR_MGMNT_1 (0x6B) / 0xb00000001
  c = SPIwriteRegister(0x6B, 0xb00000001);
  // read Register PWR_MGMNT_1 (0x6B) 
  printCharWithLeading(c);  
  c = SPIreadRegister(0x6B);  
  Serial.print("MPU9250 Power-Management-Register    :   "); 
  printCharWithLeading(c);
  Serial.println();  
}

void setup() {
  Serial.begin(115200);
  Serial.println("start setup");
  SPI.begin();
  initMPU9250();
  Serial.println("end setup");
}

void loop() {

}

The wiing is like this
Arduino MPU9250
CS/SS/SDA D10 NCS
SCK D13 SCL
MOSI D11 SDA
MISO D12 AD0
GND FSYNC
Writing data is o.k. - I checked it by I2C, and the new data is written correctly. But I cannot read data. The returned value is always 0. MISO-Arduino is working fine with another devices.
What am I doing wrong (MCU: Arduino Nano, IDE: 1.8.16)?

After just a quick glance: I notice some fiddling with an SDA pin which you appear to assume works like a chip select, but the MPU9250 (and most breakout boards) have a dedicated nCS pin that you should use for this.

Hm, nCS is conectd to Arduino D10 (SDA). And writing is working without objection. Problem is reading. Ans my multimeter tells me, that the MISO is connected properly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.