Using MPU9250 with SPI
Posted: Sat Aug 25, 2018 1:57 pm
Hello everyone! Total beginner here, so I apologize beforehand if I ask trivial/stupid questions.
For an assignment, I need to use the MPU9250 (https://www.invensense.com/wp-content/u ... 0-v1.6.pdf) to measure accelerations in z direction and use SPI to transfer the data to the pi. I have enabled SPI on my RPI.
I have connected:
RPI | MPU9250
17 -> VCC
19 -> SDA/SDI
21 -> ADO/SDO
23 -> SCLK
24 -> NCS
25 -> GND
So far I have tried using the spidev library based on the example here (http://tightdev.net/SpiDev_Doc.pdf) and wrote:
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 1)
i=0
while i<10
resp = spi.xfer([0x3F])
print(resp)
i +=1
as I understood(please correct me if I am wrong) this should start a transaction with the device and read me the values on the register 0x3F(z acceleration) but it returns something like this:
[15]
[15]
[31]
[15]
[31]
[31]
[31]
[15]
[15]
[31]
every time I run the code it returns a different set of numbers. I have no idea what the value in the brackets mean. I need acceleration values measured in g's, what do I need to do to get that?
And also, (I haven't tried it yet because I couldn't even get any measurements), I need to set the sampling rate to around 4kHz. How can I do that? Is my approach so far completely wrong, am I missing something obvious?
As I said earlier, I am a complete beginner and have almost no background in coding, so I apologize if I am asking silly questions. If you could help me I would appreciate it a lot.
Thanks in advance
For an assignment, I need to use the MPU9250 (https://www.invensense.com/wp-content/u ... 0-v1.6.pdf) to measure accelerations in z direction and use SPI to transfer the data to the pi. I have enabled SPI on my RPI.
I have connected:
RPI | MPU9250
17 -> VCC
19 -> SDA/SDI
21 -> ADO/SDO
23 -> SCLK
24 -> NCS
25 -> GND
So far I have tried using the spidev library based on the example here (http://tightdev.net/SpiDev_Doc.pdf) and wrote:
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 1)
i=0
while i<10
resp = spi.xfer([0x3F])
print(resp)
i +=1
as I understood(please correct me if I am wrong) this should start a transaction with the device and read me the values on the register 0x3F(z acceleration) but it returns something like this:
[15]
[15]
[31]
[15]
[31]
[31]
[31]
[15]
[15]
[31]
every time I run the code it returns a different set of numbers. I have no idea what the value in the brackets mean. I need acceleration values measured in g's, what do I need to do to get that?
And also, (I haven't tried it yet because I couldn't even get any measurements), I need to set the sampling rate to around 4kHz. How can I do that? Is my approach so far completely wrong, am I missing something obvious?
As I said earlier, I am a complete beginner and have almost no background in coding, so I apologize if I am asking silly questions. If you could help me I would appreciate it a lot.
Thanks in advance