Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for LSM303AGR (micro:bit v1.5) #125

Closed
alankrantas opened this issue Feb 4, 2020 · 3 comments
Closed

Request for LSM303AGR (micro:bit v1.5) #125

alankrantas opened this issue Feb 4, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@alankrantas
Copy link
Contributor

The current micro:bit version 1.5 uses a single LSM303AGR accelerometer/magnetometer chip. Might be a close relative of the LSM6DS3 driver you have here? The older 1.3b version uses MMA8653 + MAG3110.

https://tech.microbit.org/hardware/#motion-sensor
https://support.microbit.org/support/solutions/articles/19000087020-micro-bit-motion-sensor-hardware-change

@deadprogram deadprogram added the enhancement New feature or request label Feb 8, 2020
@alankrantas
Copy link
Contributor Author

alankrantas commented Jun 22, 2020

Tried to piece a test code together based on Adafruit's old LSM303 driver (which is pretty basic and the magnetometer registers looks incorrect.).

The following code is confirmed working on my micro:bit v1.5, for both accelerometer and magnetometer. If no one else is available to work on it, I'll later try my best to write a proper-ish driver.

package main

import (
    "machine"
    "time"
)

const (
    
    LSM303_ADDRESS_ACCEL = 0x19
    LSM303_ADDRESS_MAG = 0x1E
    
    LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20
    LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28
    
    LSM303_REGISTER_MAG_MR_REG_M = 0x60
    LSM303_REGISTER_MAG_OUT_X_L_M = 0x68
    
)

func main() {
       
    i2c := machine.I2C0
    i2c.Configure(machine.I2CConfig{})
    
    i2c.WriteRegister(LSM303_ADDRESS_ACCEL, LSM303_REGISTER_ACCEL_CTRL_REG1_A, []byte{0x77})
    i2c.WriteRegister(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_MR_REG_M, []byte{0x8C})
    
    data := make([]byte, 6)
    
    for {

        i2c.ReadRegister(LSM303_ADDRESS_ACCEL, LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80, data)

        accel_x := int16((uint16(data[1]) << 8) | uint16(data[0]))
        accel_y := int16((uint16(data[3]) << 8) | uint16(data[2]))
        accel_z := int16((uint16(data[5]) << 8) | uint16(data[4]))

        i2c.ReadRegister(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_OUT_X_L_M | 0x80, data)

        mag_x := int16((uint16(data[1]) << 8) | uint16(data[0]))
        mag_y := int16((uint16(data[3]) << 8) | uint16(data[2]))
        mag_z := int16((uint16(data[5]) << 8) | uint16(data[4]))

	println("LSM303_ACCR:", accel_x, accel_y, accel_z)
	println("LSM303_MAG:", mag_x, mag_y, mag_z)
        println("-----")

        time.Sleep(time.Millisecond * 100)

    }
       
}

Output:

LSM303_ACCR: -64 4608 -15680
LSM303_MAG: -262 193 79

LSM303_ACCR: 0 4672 -15680
LSM303_MAG: -261 187 81

LSM303_ACCR: -64 4608 -15680
LSM303_MAG: -256 192 81

LSM303_ACCR: -64 4480 -15616
LSM303_MAG: -263 194 84

LSM303_ACCR: -64 4608 -15616
LSM303_MAG: -261 190 85

LSM303_ACCR: 0 4480 -15616
LSM303_MAG: -250 192 80

@conejoninja
Copy link
Member

This will be a great addition to the driver's list, I do not have one of the newest micro:bit, so feel free to continue the work and make a PR of it.

@alankrantas
Copy link
Contributor Author

alankrantas commented Jul 2, 2020

I'll close this since in the end I solved my own request in [this PR] (#162).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants