Problem reading L3GD20H gyroscope in SPI

Hello!

This is my first time posting so I'm not sure how this works, but I've been working on reading the registers for the L3GD20H gyroscope in SPI. I need to make sure my readRegister function is working in order to read the registers of my gyroscope. So, the issue is that the readRegister function is returning 5 bits instead of 8. When I go to read the WHOAMI register it returns 11010 and instead should return 1101 0111 as per page 36 of the datasheet
l3gd20h.pdf (2.4 MB)
Here's my code,

#include <SPI.h>

const int WHOAMI = 0xD7;
const int CTRL1 = 0x20;
const int OUT_X_L = 0x28;
const int OUT_X_H = 0x29;
const int OUT_Y_L = 0x2A;
const int OUT_Y_H = 0x2B;
const int OUT_Z_L = 0x2C;
const int OUT_Z_H = 0x2D;
const int STATUS = 0x27;
const int LOW_ODR = 0x39;

// readData = 0x80;  
// writeData = 0x00;
long gx[16], gy[16], gz[16];      // 16 bit

unsigned long timeLimit = 60000; // 60s

const int GYRO_CS = 10; // labeled CS
const int GYRO_MISO = 12; // labeled SA0  
const int GYRO_MOSI = 11;  // labeled SDA 
const int GYRO_CLK = 13; // labeled SCL


void setup() {
  Serial.begin(9600); // setting the baud rate
  SPI.begin();
  
  pinMode(GYRO_CS, OUTPUT);
  digitalWrite(GYRO_CS, HIGH);

  Serial.println(readRegister(WHOAMI)); // prints 11010, should return 1101 0111
  // writeRegister(CTRL1, 0x0F); // enables normal mode and x,y,z axis


}


void loop() {
}


  
byte readRegister(byte address) {
  byte inByte = 0;
  SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
  digitalWrite(GYRO_CS, LOW); 
  SPI.transfer(address | 0x80);  // 1101 0111 | 1000 0000 = 1101 0111 
  inByte = SPI.transfer(0x00);
  digitalWrite(GYRO_CS, HIGH);  
  SPI.endTransaction(); 
  return inByte;
  
}

void writeRegister(int8_t thisRegister, int8_t val){
  SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
  digitalWrite(GYRO_CS, LOW);
  SPI.transfer(thisRegister << 1);
  SPI.transfer(val);
  digitalWrite(GYRO_CS, HIGH);
  SPI.endTransaction();
}

Any help would be much appreciated! I've been staring at this for a while and I need a different set of eyes.

SPISettings should be done in setup(), if it is needed at all. I can't imagine what happens with the above, or that it is a good idea.

Take a look at SPI examples from other IMU libraries. You might find this Sparkfun guide to be helpful: LSM9DS0 Hookup Guide - SparkFun Learn

Or better, just use the Pololu library. It should work "out of the box": L3G - Arduino Reference

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