2

Ok so as some readers might have seen, my current project involves using MPU's. The idea is sending data via CAN bus to a controlling MCU to then send to a raspberry pi to perform the calculations required for my MATLAB/ Simulink model.

After a lot of searching i found this great post/ blog on how to set I2C registers.

Setting I2C Registers

With this, i was able to avoid using a library from Github. I do NOT think i am better than the people writing the libraries, just it's difficult to truly understand code/ a project with a downloaded library and it just stuffed at the top of a piece of butchered example code.

Using the above blog, i learnt how to control an MPU6050 myself in a very basic sense. I am now trying to use an MPU9250 for the sake of extra speed over SPI. However i am trying to understand how to set registers using the SPI interface. For example below for I2C you could write;

  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

and then access the registers like below;

Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)

my question, is how do you read and write to registers in a similar way via SPI. I've found lots of info on using SPI, but not configuring sensors etc with it in a nice, "SPI configuration for Dummies" type of thing. Is my googling just terrible?

EDIT - My goal is learn the theory behind making a basic library for a sensor, not to make my own SPI library. For example, make a library that can be used to control an MPU9250 using SPI commands to change registers. The last part is what i am struggling with. Interfacing with a sensor to change register values or read registers with SPI.

The link above is almost a crash course in how to use the wire library to interface with a sensor. How do you use the SPI library to change/ read register values.

EDIT2 - Is this the usual format for communicating with registers using SPI on arduino? Thanks

digitalWrite(SS, LOW);
SPI.transfer(0xF1);  // Read, Sensor 1...
SPI.transfer(0x02);  // ... RESA register
highByteA = SPI.transfer(0x00);  // Read High Byte of RESA
lowByteA = SPI.transfer(0x00);  // Read Low Byte of RESA
highByteB = SPI.transfer(0x00);  // Read High Byte of RESB
lowByteB = SPI.transfer(0x00);  // Read Low Byte of RESB
.
.
.
digitalWrite(SS, HIGH);

thanks

  • The Arduino IDE already has a SPI library, that you can use (like is also already has the Wire library for I2C). There are may tutorials, that explain, how to use it. And there is no general way to "configure sensores". It depends on the sensor. For the MPU it can be found in it's datasheet, though this is not a "... for Dummies" guide. I think you probably won't find such a guide, since the way "... for Dummies" is to use a premade library. But you might also learn much by looking at these premade libraries, that you don't want to use. Otherwise you just chose the hard tour. – chrisl Dec 21 '18 at 21:14
  • This is what i like. Getting different perspectives. I am happy to use the SPI library, what i wanted to learn is almost how to make my own for each sensor. The link i put in explains the theory of communicating with the sensor to change parameters/ registers using I2C. (e.g measurement range or frequency). I will change my question to make it clearer what i am asking – Ross Hanna Dec 21 '18 at 22:13
  • Have you read page 36 of the datasheet (for example here cdn.sparkfun.com/assets/learn_tutorials/5/5/0/MPU9250REV1.0.pdf ) It explanes how it works. The first byte consists of a read/write bit and the address of the register, that you want to access. Next bytes are data (for example that should be written into the specified registers). Have you tried reading some registers that way? – chrisl Dec 22 '18 at 0:15
  • forgive my ignorance. i did see that in the datasheet but my issue was trying to find how to actually write to or read from the register. I haven't figured out code tags in the comments, but i found some code from a post on arduino.cc that i overlooked because the OP hadn't found a working solution. It is at the bottom of my post. is that the format to read to or write from a register for a sensor using SPI? – Ross Hanna Dec 22 '18 at 4:40
  • Do you have a logic analyzer? I was working on some similar things last year and it was infinitely helpful to be able to see what was actually happening on the wire. – RubberDuck Dec 22 '18 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.