Acessing the MAG-3110 on the M5STACK FIRE



  • Gentlefolk,

    Greetings. I am trying to access the MAG-3110 magnetometer on the M5STACK FIRE but have been unsuccessful so far. However, I am able to access the MPU-6050 without any discernible issues.

    I have a Windows environment and am using MicroPython. I've tried a number of MicroPython firmware and the latest one I'm using is m5cloud-psram-20180516-v0.4.0.bin. I am also using M5Burner for Windows to flash the M5STACK FIRE.

    I've tried to reverse-engineer a number of Python modules in order to see if I can access the magnetometer via REPL. Here are some of the results I have obtained:

    from machine import I2C

    i2c = I2C(scl=Pin(22), sda=Pin(21), speed=400000)

    i2c.scan()
    [14, 104, 117] # addr 104 is the MPU-6050 and 14 should be the MAG-3110

    struct.unpack("B", i2c.readfrom_mem(14, 7, 1)) # check who_am_i register on device with addr 14
    (196,) # is this the correct response (0xC4) according to the spec sheet

    i2c.writeto_mem(14, 16, 1) # set operation mode, RW register according to the spec sheet (CTRL_REG1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: object with buffer protocol required

    i2c.writeto_mem(14 17, 1) # try the other register, RW register according to the spec sheet (CTRL_REG1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: object with buffer protocol required

    Note that the same things happen when I try to access address 117 although I get a different response WHO_AM_I (i.e., 0x80 instead of 0xC4). If I try to read the data registers, the contents do not change even if I aggressively move the M5STACK FIRE. I am, however, able to get accelerometer and gyroscope readings.

    Has anyone else encountered this or have successfully accessed the magnetometer on the M5STACK FIRE?

    Best regards.



  • Hi there you can try this code.

    from m5stack import lcd, buttonA
    from mpu9250 import MPU9250
    from time import sleep_ms
    from machine import I2C

    i2c = I2C(sda = 21, scl = 22)
    imu = MPU9250(i2c)

    lcd.clear()
    lcd.font(lcd.FONT_Small)
    lcd.setTextColor(lcd.WHITE, lcd.BLACK)

    while not buttonA.isPressed():
    mag = imu.magnetic
    print("MAG: {:8.3f} {:8.3f} {:8.3f}".format(mag[0], mag[1], mag[2]))
    print('')
    lcd.print("MAG:{:+7.2f} {:+7.2f} {:+7.2f}".format(mag[0], mag[1], mag[2]), lcd.CENTER, 60)
    sleep_ms(20)

    lcd.print('Exit.', 0, 100)

    Remember that there are magnets in the base of the m5 core, so maybe best to remove it if you don't want any interference in the magnetometer reading



  • Hey Lukas,

    This won't work. The mpu9250 module expects to find an MPU6500 accelerometer so even instantiating an mpu9250 object will fail during init(). Even if I remove the part of the code that checks for the MPU6500, instantiation will still fail when it attempts to initialize the magnetometer by writing to one of it's registers.