<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problems with BMI270 on M5StampFly (M5StampS3)]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I am trying to write a code for M5StampFly myself but I have run into an issue with an imu unit.<br />
I cannot configure it to work. I have tried many different approaches.<br />
The code below read a status and a chip id. The chip id always returns random values with the same SPI settings and status always returns 0. I tried changing SPI settings, using different libraries, powering stampFly with a battery and then running code, reseting imu before reading and many others.<br />
I would gladly appreciate some help.<br />
Thanks in advance.</p>
<p dir="auto">Code (ignore polish comments):</p>
<pre><code class="language-C">#include &lt;SPI.h&gt;

// PINy pod SPI - zakładam ze poprawnie
#define BMI270_CS   46  
#define BMI270_SCK  44  
#define BMI270_MISO 43  
#define BMI270_MOSI 14  

// Definicje rejestrów BMI270 zakresy z https://github.com/simondlevy/BMI270-SPI/blob/main/src/BMI270.h
#define BMI270_REG_CHIP_ID       0x00  // Rejestr identyfikatora chipu
#define BMI270_REG_CMD           0x7E  // Rejestr poleceń
#define BMI270_REG_ACC_CONF      0x40  // Konfiguracja akcelerometru
#define BMI270_REG_ACC_RANGE     0x41  // Zakres akcelerometru
#define BMI270_REG_GYR_CONF      0x42  // Konfiguracja żyroskopu
#define BMI270_REG_GYR_RANGE     0x43  // Zakres żyroskopu
#define BMI270_REG_DATA          0x0C  // Początek danych akcelerometru i żyroskopu

// Funkcja do zapisu wartości do rejestru BMI270
void writeRegister(uint8_t reg, uint8_t value) {
  SPI.beginTransaction(SPISettings(10000000, LSBFIRST, SPI_MODE0));
  digitalWrite(BMI270_CS, LOW);
  SPI.transfer(reg &amp; 0x7F);  // Bit 7 ustawiony na 0 dla operacji zapisu
  SPI.transfer(value);
  digitalWrite(BMI270_CS, HIGH);
  SPI.endTransaction();
}

// Funkcja do odczytu wartości z rejestru BMI270
uint8_t readRegister(uint8_t reg) {
  SPI.beginTransaction(SPISettings(10000000, LSBFIRST, SPI_MODE0));
  digitalWrite(BMI270_CS, LOW);
  SPI.transfer(reg | 0x80);  // Bit 7 ustawiony na 1 dla operacji odczytu
  uint8_t value = SPI.transfer(0x00);
  digitalWrite(BMI270_CS, HIGH);
  SPI.endTransaction();
  return value;
}

// Funkcja do odczytu wielobajtowych danych z BMI270
void readRegisters(uint8_t startReg, uint8_t* buffer, uint8_t length) {
  SPI.beginTransaction(SPISettings(10000000, LSBFIRST, SPI_MODE0));
  digitalWrite(BMI270_CS, LOW);
  SPI.transfer(startReg | 0x80);  // Bit 7 ustawiony na 1 dla operacji odczytu
  for (uint8_t i = 0; i &lt; length; i++) {
    buffer[i] = SPI.transfer(0x00);
  }
  digitalWrite(BMI270_CS, HIGH);
  SPI.endTransaction();
}

// Funkcja resetująca BMI270
void resetBMI270() {
  writeRegister(BMI270_REG_CMD, 0xB6);  // Reset
  delay(100);  // opoznienie
}

// Funkcja inicjalizująca BMI270
void initializeBMI270() {
  resetBMI270();

  // Konfiguracja akcelerometru
  writeRegister(BMI270_REG_ACC_CONF, 0xA8);  // Próbkowanie 1600 Hz, filtr dolnoprzepustowy
  writeRegister(BMI270_REG_ACC_RANGE, 0x00); // Zakres ±2g

  // Konfiguracja żyroskopu
  writeRegister(BMI270_REG_GYR_CONF, 0xA9);  // Próbkowanie 1600 Hz, filtr dolnoprzepustowy
  writeRegister(BMI270_REG_GYR_RANGE, 0x00); // Zakres ±2000°/s

  delay(50);  // Opoxnienie
}

// Funkcja odczytująca dane z akcelerometru i żyroskopu
void readSensorData(int16_t* accData, int16_t* gyrData) {
  uint8_t data[12];
  readRegisters(BMI270_REG_DATA, data, 12);

  // Przetwarzanie danych akcelerometru (16-bitowe wartości w porządku little-endian)
  accData[0] = (int16_t)((data[1] &lt;&lt; 8) | data[0]);  // Oś X
  accData[1] = (int16_t)((data[3] &lt;&lt; 8) | data[2]);  // Oś Y
  accData[2] = (int16_t)((data[5] &lt;&lt; 8) | data[4]);  // Oś Z

  // Przetwarzanie danych żyroskopu (16-bitowe wartości w porządku little-endian)
  gyrData[0] = (int16_t)((data[7] &lt;&lt; 8) | data[6]);  // Oś X
  gyrData[1] = (int16_t)((data[9] &lt;&lt; 8) | data[8]);  // Oś Y
  gyrData[2] = (int16_t)((data[11] &lt;&lt; 8) | data[10]); // Oś Z
}

//uint8_t chip_id = 0;

void setup() {
  Serial.begin(115200);
  pinMode(BMI270_CS, OUTPUT);
  digitalWrite(BMI270_CS, HIGH);
  SPI.begin(BMI270_SCK, BMI270_MISO, BMI270_MOSI, BMI270_CS);
  delay(100);

  initializeBMI270();

  // Sprawdzenie identyfikatora chipu
  uint8_t chip_id = readRegister(BMI270_REG_CHIP_ID);
  Serial.print("Chip ID: 0x");
  Serial.println(chip_id, HEX);

  uint8_t status = readRegister(0x01);
Serial.print("Status Register: 0x");
Serial.println(status, HEX);
}

void loop() {
  int16_t accData[3];  // Tablica na dane akcelerometru
  int16_t gyrData[3];  // Tablica na dane żyroskopu

  // Serial.print("Chip ID: 0x");
  // Serial.println(chip_id, HEX);

  //readSensorData(accData, gyrData);

  // Akcelerometr
  //Serial.print("Akcelerometr [mg]: X=");
  //Serial.print(accData[0]);
  //Serial.print(" Y=");
  //Serial.print(accData[1]);
  //Serial.print(" Z=");
  //Serial.println(accData[2]);

  // Żyroskop
  //Serial.print("Żyroskop [mdps]: X=");
  //Serial.print(gyrData[0]);
  //Serial.print(" Y=");
  //Serial.print(gyrData[1]);
  //Serial.print(" Z=");
  //Serial.println(gyrData[2]);
  delay(200);
}
</code></pre>
]]></description><link>https://community.m5stack.com/topic/7454/problems-with-bmi270-on-m5stampfly-m5stamps3</link><generator>RSS for Node</generator><lastBuildDate>Wed, 13 May 2026 04:15:35 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/7454.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 30 Mar 2025 09:14:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problems with BMI270 on M5StampFly (M5StampS3) on Sun, 30 Mar 2025 10:03:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/felmue" aria-label="Profile: felmue">@<bdi>felmue</bdi></a> Thanks for answering!<br />
Tried it and now I am getting always chip id 0xFF or 0x0 depended on data order in SPISettings (MSB or LSB first). Tried it with and without battery plugged in but there was no difference.<br />
Any other ideas?</p>
]]></description><link>https://community.m5stack.com/post/28718</link><guid isPermaLink="true">https://community.m5stack.com/post/28718</guid><dc:creator><![CDATA[KamilB]]></dc:creator><pubDate>Sun, 30 Mar 2025 10:03:42 GMT</pubDate></item><item><title><![CDATA[Reply to Problems with BMI270 on M5StampFly (M5StampS3) on Sun, 30 Mar 2025 09:51:54 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/kamilb" aria-label="Profile: KamilB">@<bdi>KamilB</bdi></a></p>
<p dir="auto">I do not have the hardware to test this myself, but I think you might want to pull GPIO12 HIGH to make sure you're talking to BMI270 only. See <a href="https://github.com/m5stack/M5StampFly/blob/main/src/imu.cpp#L43" target="_blank" rel="noopener noreferrer nofollow ugc">here</a>.</p>
<p dir="auto">Thanks<br />
Felix</p>
]]></description><link>https://community.m5stack.com/post/28717</link><guid isPermaLink="true">https://community.m5stack.com/post/28717</guid><dc:creator><![CDATA[felmue]]></dc:creator><pubDate>Sun, 30 Mar 2025 09:51:54 GMT</pubDate></item></channel></rss>