Has anyone gotten the MPU-9250 to work with SPI?

Status
Not open for further replies.
Have you looked on the Invensense Developer's Forum? I am sure I have seen code to do this there.
 
This has been tested on the teensy 3.0 and 3.1. Should also work for teensy 3.2.

SPI works great for the LSM6DS3 breakout board from Sparkfun (link below). The ADDR jumper must be opened. I also got it to work with the ILI9341 display sharing the same MOSI/CLK/MISO pins.

https://forum.pjrc.com/threads/3468...-on-Teensy-3-2?p=111721&viewfull=1#post111721

I'm moving on to the MPU-9250 and LSM9DS1, both Sparkfun breakouts, because I need the magnetometers.

I just tried the LSM9DS1, and it works (provided that all jumpers are opened per the Sparkfun hookup guide).

However, I'm still having trouble with the MPU-9250. Sparkfun does not mention anything about opening jumpers for SPI. It's configured for I2C. I did try opening and closing "SJ2", but no luck in either case. Which MPU-9250 breakout did you implement?
 
Last edited:
@randomvibe, I'm not sure about the Sparkfun board, however, it seems like it should work with SPI. Refer to the pin description table on this page.

I personally just got the cheapest MPU-9250 from ebay I could find. They're <$2 each when I bought them.
 
I have tried BrianC's SPI library with my MPU-9250 (cheap blue PCB from ebay) and it seems to be working under teensy 3.2 but for some reason I'm getting a failed connection in regards to the magnetometer from the serial monitor when it starts up but for my application I don't exactly need a working magnetometer.

Just make sure you have it wired up correctly, below is how mine is currently wired.

From IMU to Teensy (Pin)

VCC - 3.3V
GND - GND
SCLK - SCK (13)
SDO - DIN (12)
SDI - DOUT (11)
CS - CS (10)
 
FWIW, I got the Sparkfun unit to work on a Due but so far have not gotten my brand new Teensy 3.6 to work.
The SJ2 jumper needs to be moved over, not just opened. I will try to figure out my 3.6 problem as I'm new to the great powers of Teensies:)
 
Ok, just a dumb wiring error on my side, Teensy 3.6 and Sparkfun 9250 in SPI mode spitting out values mighty fast.
The pin-out card that Paul provides is excellent, you just have to actually use it;)
 
Missing Definitions

For anyone who needs help getting the MPU-9250 working with SPI, the library with an example is here.

Hi BrianC, thank you for your sample code!

I know this thread is already a year and a half old, but I've been trying to get SPI to work with my MPU9250 for a while, and have been having a hard time finding any good information on this. I really need to use SPI rather than I2C because I want to read multiple MPUs, but I2C can't distinguish between them because they all have the same hardware address. I'm not sure if your code is written for multiple MPUs, but I'm thinking that if I can at least get it to work, then maybe I can tweak it to work for multiple MPUs.

However, It seems that your code is referencing some things that are not in the attached MPU9250 library. You have a line that says "MPU9250 mpu(SPI_CLOCK, SS_PIN);" but I don't see that defined anywhere in the library, and it's throwing an error...

Also, the code calls this "read_all()" function, but I noticed that read_all() starts off with I2C commands. Is this right? I'm not clear on why an SPI sketch is calling I2C commands. Should I be using a different library than the one attached maybe?

Lastly, I'm not clear about how the pins should be connected. I'm guessing I should have:
- VCC on my Arduino to VCC on my MPU
- GND on my Arduino to GND on my MPU
- SS_PIN on my Arduino to NCS on my MPU
Is that correct? And if so, what about these two?
- INT_PIN on my Arduino to ??? on my MPU
- SCK (serial clock)??? I don't see that in the code anywhere, but I read that it's needed to syncronize devices in SPI.

I really appreciate any help I can get here! Thanks!
 
Hi BrianC, thank you for your sample code!

I know this thread is already a year and a half old, but I've been trying to get SPI to work with my MPU9250 for a while, and have been having a hard time finding any good information on this. I really need to use SPI rather than I2C because I want to read multiple MPUs, but I2C can't distinguish between them because they all have the same hardware address. I'm not sure if your code is written for multiple MPUs, but I'm thinking that if I can at least get it to work, then maybe I can tweak it to work for multiple MPUs.

However, It seems that your code is referencing some things that are not in the attached MPU9250 library. You have a line that says "MPU9250 mpu(SPI_CLOCK, SS_PIN);" but I don't see that defined anywhere in the library, and it's throwing an error...

Also, the code calls this "read_all()" function, but I noticed that read_all() starts off with I2C commands. Is this right? I'm not clear on why an SPI sketch is calling I2C commands. Should I be using a different library than the one attached maybe?

Lastly, I'm not clear about how the pins should be connected. I'm guessing I should have:
- VCC on my Arduino to VCC on my MPU
- GND on my Arduino to GND on my MPU
- SS_PIN on my Arduino to NCS on my MPU
Is that correct? And if so, what about these two?
- INT_PIN on my Arduino to ??? on my MPU
- SCK (serial clock)??? I don't see that in the code anywhere, but I read that it's needed to syncronize devices in SPI.

I really appreciate any help I can get here! Thanks!

I have a MPU-9250 library, which uses I2C or SPI, have a look through the code if you like or just use the library. It supports all Teensy 3.X and LC devices and on I2C and SPI (all SPI buses and alternate pins are supported):
https://github.com/bolderflight/MPU9250

The discussion on PJRC is located here:
https://forum.pjrc.com/threads/37891-MPU-9250-Teensy-Library
 
Error Setting Register

I have a MPU-9250 library, which uses I2C or SPI, have a look through the code if you like or just use the library. It supports all Teensy 3.X and LC devices and on I2C and SPI (all SPI buses and alternate pins are supported):
https://github.com/bolderflight/MPU9250

The discussion on PJRC is located here:
https://forum.pjrc.com/threads/37891-MPU-9250-Teensy-Library

Thank you for your code brtaylor! I've been tweaking it and trying to get it to work, and currently have it running with errors on setting the clock source in the MPU registers.

I hard-coded in custom error codes so they are not just all "-1," which is how I figured out that "begin()" was returning with error code "-2" from that section that sets the clock source: if( !writeRegister(PWR_MGMNT_1, CLOCK_SEL_PLL) ){ return -2; }

I'm not really sure how to wire this though, so it could be a wiring issue...

I *assumed* that "attachInterrupt(2,getIMU,RISING);" means I should connect pin 2 of the Arduino to the "INT" pin on the MPU.
I also *assumed* that "Chip Select pin 10" in the code comments means that I should connect pin 10 on the Arduino to the "NCS" pin on the MPU.
The only other pins I connected were VCC and GND, which seemed obvious.
Did I wire this wrong?

I'm kind of just throwing ideas out there, since I don't really know how this is *supposed* to be done.

Please let me know if you have any thoughts or questions! And I really appreciate what you've offered already!


Note: I had to massively edit the library by removing all of the I2C portions, since I don't use them, and also since this is for Arduino, not Teensey, and it seemed that that library was a Teensey one. I feel like a lot of this code could also be edited down for my purposes, but I'm afraid to remove anything more without knowing what anything does. I knew I didn't need the I2C and "AK8963" stuff though, so I just removed all of that to avoid any dependency errors.
 
I would start with the Basic_SPI example, which doesn't use the interrupt generation on the MPU-9250, it just polls the sensor and gets the current data. But, yes, the attachInterrupt function is attaching a Teensy pin to interrupt when the MPU-9250 generates a pulse, which is set to occur when the MPU-9250 updates its data.

Can you post your updated code and a sketch of the wiring? I'm away from my office until next week, so I won't be able to test anything until then, but something might pop out as being in error.
 
I would start with the Basic_SPI example, which doesn't use the interrupt generation on the MPU-9250, it just polls the sensor and gets the current data. But, yes, the attachInterrupt function is attaching a Teensy pin to interrupt when the MPU-9250 generates a pulse, which is set to occur when the MPU-9250 updates its data.

Can you post your updated code and a sketch of the wiring? I'm away from my office until next week, so I won't be able to test anything until then, but something might pop out as being in error.

No problem!

Here is the trimmed-down MPU9250.cpp: https://gist.github.com/anonymous/8449f56d98ff03c5237c2260e34c1a6c

And here is the trimmed-down MPU9250.h: https://gist.github.com/anonymous/7d8aea9a0834b630a91feded3e1f9bbf

Also, just in case, here is the modified program: https://gist.github.com/anonymous/a50f608c807a8c86fd786420ee19b916

Lastly, I'm attaching a photo to show my pin layout: image1.jpeg

Please let me know if this all looks right to you! Thank you!
 
I'm not an expert but think you are missing a few connections for SPI:
SCK - pin 13 for t3.2
MOSI - pin 11
MISO - pin 12
SS = default is usually pin 10 but you can select any pin.

In your case on the T3.2 to 9250
NCS => SS
MOSI => SDA
MISO => ADO
SCK => SCL

Check this out for SPI on the Teensy: https://www.pjrc.com/teensy/td_libs_SPI.html

Mike
 
I'm not an expert but think you are missing a few connections for SPI:
SCK - pin 13 for t3.2
MOSI - pin 11
MISO - pin 12
SS = default is usually pin 10 but you can select any pin.

In your case on the T3.2 to 9250
NCS => SS
MOSI => SDA
MISO => ADO
SCK => SCL

Check this out for SPI on the Teensy: https://www.pjrc.com/teensy/td_libs_SPI.html

Mike

Thank you Mike! I read that site and it helped me understand a little bit more (well, actually, I knew about MOSI and MISO and CLK and their uses, but my board didn't have any of them, and neither did that sample mention any of them explicitly, so I thought maybe they weren't needed).

Maybe you didn't notice that my device is a Pro Micro (not a Teensy). According to this site (https://laulima.hawaii.edu/wiki/site/ba1120a7-cff6-45d9-8009-442424a1a6db/arduino-pro-micro.html), the MOSI, MISO, and SCK should be pins 16, 15, and 14.

In any case, I've hooked up those pins to the respective pins on the 9250, but I'm still getting the error about the clock register that I had mentioned above. Does anyone have any thoughts on what that could be caused by?
 
It's failing on the first transaction with the MPU-9250, which tells me that the issue is likely one with the basic SPI communication rather than anything specific with setting that register. I am using some Teensy specific SPI code, which let's me pass the SPI bus by reference for easy use of all of the SPI buses available on some of the Teensy devices. I also use digitalWriteFast to raise and lower the CS pin, which is also a Teensy specific bit of code.

My guess is that in writeRegister and readRegisters that all of the _spi-> calls need to be changed to SPI. calls. I would probably try that first and then try changing all of the digitalWriteFast calls to digitalWrite if that still doesn't work.
 
Brian is probably right, his library is great but to convert the SPI portion to generic SPI will be a bit of work. If you want to get something working as a test I am attaching a basic MPU9250 library you can try out and check on changing the read and write registers.
View attachment MPU9250SPIM.zip
 
Status
Not open for further replies.
Back
Top