3

I am trying to interface a Arduino Uno with an ASM330LHH using SPI. Whenever I try to read any register, I either get 0xff or 0x00 on the serial monitor

The code I am using:

#include <SPI.h>

#define LOOP_DELAY 1000 //ms
uint8_t data;

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

  /* Refer datasheet for ASM330LHH
   * SPI freq max 10MHz
   * MSB first
   * SPI mode 3
   */
  SPI.begin();
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
  

  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  Serial.println("Setup complete!");

}

void loop() {
  digitalWrite(10, LOW);
  byte masked_addr = 0x0F | 0x80;
  Serial.println(masked_addr, BIN);
  
  SPI.transfer(masked_addr);
  data = SPI.transfer(0x00);
  Serial.println(data, BIN);
  digitalWrite(10, HIGH);
  delay(LOOP_DELAY);
}

The output I get:

10001111
11111111
10001111
11111111
10001111
0
10001111
0

The expected output for the above code is 0x6B. What might have gone wrong?

Thanks in advance

7
  • 1
    add SPI.begin(); – Juraj Nov 9 '20 at 5:31
  • I'm not entirely sure it will matter in what you have currently, but SPI.begin() should happen before .beginTransaction() – timemage Nov 12 '20 at 11:13
  • begin must be called before beginTransfer and 10 MHz is to fast for an 16 MHz arduino. 1 MHz should be OK. It might be, that the library correct the 10 Mhz property to a suitable value. – Peter Paul Kiefer Nov 12 '20 at 11:21
  • Just as an aside, your data rate was probably fine. The Arduino UNO's ATMega328P running 16 MHz, can't run the SPI clock faster than 8 MHz or slower than 125 kHz. The rate will be clamped to this range, which most SPI devices will handle. The slower rate is probably better while you're troubleshooting though. – timemage Nov 12 '20 at 11:44
  • Your ASM330LHH is a 3.3V device. You should probably update the question to have a picture/diagram/schematic of however you're interfacing your 5.0V Arduino UNO to it. – timemage Nov 12 '20 at 11:51

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.