1

I have the following code to use in a Microbit connecting to an MPU 6050 (on a gy521 board)

basic.showIcon(IconNames.Heart)

SSD1306oled.useBuffer();
const MPU = 0x68;
pins.i2cWriteNumber(MPU, 0x6B, NumberFormat.UInt8LE, true)
pins.i2cWriteNumber(MPU, 0x00, NumberFormat.UInt8LE, false)
basic.pause(10)
pins.i2cWriteNumber(MPU, 0x1B, NumberFormat.UInt8LE, true)
pins.i2cWriteNumber(MPU, 0x00, NumberFormat.UInt8LE, false)
basic.pause(10)
pins.i2cWriteNumber(MPU, 0x1C, NumberFormat.UInt8LE, true)
pins.i2cWriteNumber(MPU, 0x00, NumberFormat.UInt8LE, false)
basic.pause(10)
pins.i2cWriteNumber(MPU, 0x75, NumberFormat.UInt8LE, true)
let who = pins.i2cReadNumber(MPU, NumberFormat.UInt8LE, false)
SSD1306oled.drawTextAt("who: " + who, 8, 0)
SSD1306oled.drawBuffer()
basic.pause(2000)


basic.forever(function () {
    pins.i2cWriteNumber(MPU, 0x41, NumberFormat.UInt8LE, true)
    basic.pause(100)
    let num1 = pins.i2cReadNumber(MPU, NumberFormat.UInt8LE, true)
    let num2 = pins.i2cReadNumber(MPU, NumberFormat.UInt8LE, false)
    SSD1306oled.drawTextAt(1 + "= " + num1, 0, 1)
    SSD1306oled.drawTextAt(2 + "= " + num2, 0, 2)
    SSD1306oled.drawBuffer();
    basic.pause(500);
})

It can read the "who am I" register (0x75) just fine, but shows zeros for accelerometer, gyro, and even temperature. I've gotten to this point by trying to adapt Arduino code to microbit typescript. I had thought the problem might be there is no "Wire.requestFrom" in microbit typescript, but then I can read "who am I" just fine, so I think it must be something else. I thought maybe I had a defective board, so I bought another - same results.

I have perused the register map (https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf) but nothing is jumping out at me.

I tried someone else's library, but it appears un-maintained / maybe broken (his "initialize" function, which is referenced in his examples, no longer exists... copying it and enabling "initialize" doesn't help).

I am pretty much at a loss. Any ideas?

(BTW, the SSD1306oled functions just write to an LED screen - might as well write to serial output or whatever for debugging)

Aerik
  • 2,307
  • 1
  • 27
  • 39
  • This is really hard to answer without knowing how it's wired, but it's possible you aren't sending the sensor a clock signal – Spinnaker Jan 31 '19 at 21:42
  • I've connect VCC, GND, SDA, and SCL. I would think that if it's connected incorrectly, it would not be able to read the "who am I" register...? I think my next step is to buy a cheap arduino and make sure the board works. – Aerik Feb 02 '19 at 03:32
  • I also purchased an Arduino Uno and connected the MPU. It works there (I can read the temperature, accelerometer, and gyro values using the Arduino), so my MPU isn't defective and my wiring connections are fine. – Aerik Feb 06 '19 at 20:02
  • And I tried a different Microbit, just to cover all the variables. Same result; can read "who am I" but not temp, accel, or gyro values. – Aerik Feb 06 '19 at 20:08
  • Your pause of 100milli seconds is probably too long between `pins.i2cWriteNumber(MPU, 0x41...` and `pins.i2cReadNumber(MPU ...)`. The device has probably sent the data your program just isn't ready. – phil Feb 08 '19 at 03:15
  • Also register 0x41 only returns 1 byte, the high byte of temperature you also need to read register 0x42 for the low byte. And then make an integer from the two bytes. I don't know the best way to do this in java script – phil Feb 08 '19 at 03:37

0 Answers0