SparkFun Forums 

Where electronics enthusiasts find answers.

Questions &/or issues related to products in the Movement & IMU category should be posted in this forum.
By henry321yu
#204022
How do i use SPI communication on this broad?
Introduction:
https://learn.sparkfun.com/tutorials/9d ... troduction
Schematic:
https://cdn.sparkfun.com/assets/learn_t ... ematic.pdf
Datasheet(mpu9250):
https://cdn.sparkfun.com/assets/learn_t ... REV1.0.pdf

I tried to connect it to my arduino broad(nano) to implement multiple slaves like adding accelerometer,etc.
I read imu m0 schematic and mpu9250 datasheet about their spi connection.

imu m0 schematic(mpu9250 part)
Image

mpu9250 datasheet(p.20 SPI operation)
Image

I connected cs to nano's cs(pin10),sd0 to nano's miso,sdi to nano's mosi,sclk to nano's sclk.
Well,NOT work at all haha.
I even can't read the value of WHO_AM_I register.
i think maybe it's besause i didn't supply power,but imu m0 schematic indicate CS is connected to 3.3V,and also Vdd and Vddid.
And SDO had connected to gnd.
Don't have confident to wire it..though i still do it but still no value.

I see many break out pin on it,but i can't find example on internet.
Is it can't use the SPI interface? or there is a solution for it
I'm just a student new to it.

So.. help..

my code:
Code: Select all
#include <SPI.h>

#define ACCEL_XOUT_H  0x3B
#define ACCEL_XOUT_L  0x3C
#define ACCEL_YOUT_H  0x3D
#define ACCEL_YOUT_L  0x3E
#define ACCEL_ZOUT_H  0x3F
#define ACCEL_ZOUT_L  0x40
#define WHO_AM_I      0x75
#define PWR_MGMT_1    0x6B
#define PWR_MGMT_2    0x6C
#define ASTC          0x0CH
#define ST_X_ACCEL    0x0D
#define ST_Y_ACCEL    0x0E
#define ST_Z_ACCEL    0x0F

const int chipSelectPin = 10;
int values[20],t[9];
double x, y, z;

void setup() {
  Serial.begin(115200);

  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));

  pinMode(chipSelectPin, OUTPUT);
  
  writeRegister(PWR_MGMT_2, 0x00);
  writeRegister(PWR_MGMT_1, 0x80);
  
  digitalWrite(chipSelectPin, HIGH);

  delay(100);
}

void loop() {

  values[0] = readRegister(ACCEL_XOUT_H);
  values[1] = readRegister(ACCEL_XOUT_L);
  values[2] = readRegister(ACCEL_YOUT_H);
  values[3] = readRegister(ACCEL_YOUT_L);
  values[4] = readRegister(ACCEL_ZOUT_H);
  values[5] = readRegister(ACCEL_ZOUT_L);

  t[0]=readRegister(WHO_AM_I);
  
  x= (values[0] << 8) + values[1];
  y= (values[2] << 8) + values[3];
  z= (values[4] << 8) + values[5];
  
  Serial.print(x);
  Serial.print(',');
  Serial.print(y);
  Serial.print(',');
  Serial.print(z);
  Serial.print(',');
  Serial.print(t[0],HEX);
  Serial.print('\n');
  delay(5);
}

byte readRegister (byte thisRegister) {
  byte inByte = 0 ;
  digitalWrite(chipSelectPin, LOW);
  SPI.transfer(thisRegister | 0x80);
  inByte = SPI.transfer(0x00);
  digitalWrite(chipSelectPin, HIGH);
  return inByte;
}
void writeRegister (byte thisRegister, byte value) {
  digitalWrite(chipSelectPin, LOW);
  SPI.transfer(thisRegister);
  SPI.transfer(value);
  digitalWrite(chipSelectPin, HIGH);
}
User avatar
By TS-Chris
#204051
Code: Select all
How do i use SPI communication on this broad?
OK, it looks like you're trying to talk to the MPU-9250 directly with an external controller.

You really don't use SPI to talk to the sensor directly since the board already has a microcontroller on it. The firmware in the board already is setup to be a SPI master and isn't watching out for another master on the same bus so you're bound to have bus contention issues by connecting your Nano.

You could probably alter the firmware on the board to allow another master on the SPI bus, or you could even disconnect the onboard SAMD21 from the bus by taking the SPI and CS lines high impedance. Both would probably work but I haven't tested that and can't help with the code modifications you would need to make. Uploading a blank sketch might work.