micropython-mpu9250


Namemicropython-mpu9250 JSON
Version 0.4.0 PyPI version JSON
download
home_pagehttps://github.com/tuupola/micropython-mpu9250
SummaryMicroPython I2C driver for MPU9250 9-axis motion tracking device
upload_time2023-12-01 18:59:38
maintainerMika Tuupola
docs_urlNone
authorMika Tuupola
requires_python
licenseMIT
keywords accelerometer gyro magnetometer micropython i2c
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MicroPython MPU-9250 (MPU-6500 + AK8963) I2C driver

MPU-9250 is a System in Package (SiP) which combines two chips: MPU-6500 which contains 3-axis gyroscope and 3-axis accelerometer and an AK8963 which is a 3-axis digital compass.

## Usage

Kevin Wheeler has an [extensive video](https://www.youtube.com/watch?v=ph10GSO8pDk) describing how to use this library. The short code snippets in this README should also help you to get started. First is simple example with never ending loop.

```python
import utime
from machine import I2C, Pin
from mpu9250 import MPU9250

i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = MPU9250(i2c)

print("MPU9250 id: " + hex(sensor.whoami))

while True:
    print(sensor.acceleration)
    print(sensor.gyro)
    print(sensor.magnetic)
    print(sensor.temperature)

    utime.sleep_ms(1000)
```

By default the library returns 3-tuple of X, Y, Z axis values for either acceleration, gyroscope and magnetometer ie compass. Default units are `m/s^2`, `rad/s`, `uT` and `°C`. It is possible to also get acceleration values in `g` and gyro values `deg/s`. See the example below. Note that both the MPU6500 and the AK8963 drivers are available as separate classes. MPU9250 is actually a composite of those two.

```python
import utime
from machine import I2C, Pin
from mpu9250 import MPU9250
from mpu6500 import MPU6500, SF_G, SF_DEG_S

i2c = I2C(scl=Pin(22), sda=Pin(21))
mpu6500 = MPU6500(i2c, accel_sf=SF_G, gyro_sf=SF_DEG_S)
sensor = MPU9250(i2c, mpu6500=mpu6500)

print("MPU9250 id: " + hex(sensor.whoami))

while True:
    print(sensor.acceleration)
    print(sensor.gyro)
    print(sensor.magnetic)
    print(sensor.temperature)

    utime.sleep_ms(1000)
```

More realistic example usage with timer. If you get `OSError: 26` or `i2c driver install error` after soft reboot do a hard reboot.

```python
import micropython
from machine import I2C, Pin, Timer
from mpu9250 import MPU9250

micropython.alloc_emergency_exception_buf(100)

i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = MPU9250(i2c)

def read_sensor(timer):
    print(sensor.acceleration)
    print(sensor.gyro)
    print(sensor.magnetic)
    print(sensor.temperature)

print("MPU9250 id: " + hex(sensor.whoami))

timer_0 = Timer(0)
timer_0.init(period=1000, mode=Timer.PERIODIC, callback=read_sensor)
```

## Magnetometer Calibration

For real life applications you should almost always [calibrate the magnetometer](https://www.appelsiini.net/2018/calibrate-magnetometer/). The AK8963 driver supports both hard and soft iron correction. Calibration function takes two parameters: `count` is the number of samples to collect and `delay` is the delay in millisecods between the samples.

With the default values of `256` and `200` calibration takes aproximately one minute. While calibration function is running the sensor should be rotated multiple times around each axis.

NOTE! If using MPU9250 you will first need to open the I2C bypass access to AK8963. This is not needed when using a standalone AK8963 sensor.

```python
from machine import I2C, Pin
from mpu9250 import MPU9250
from ak8963 import AK8963

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

dummy = MPU9250(i2c) # this opens the bybass to access to the AK8963
ak8963 = AK8963(i2c)
offset, scale = ak8963.calibrate(count=256, delay=200)

sensor = MPU9250(i2c, ak8963=ak8963)
```

After finishing calibration the `calibrate()` method also returns tuples for both hard iron `offset` and soft iron `scale`. To avoid calibrating after each startup it would make sense to strore these values in NVRAM or config file and pass them to the AK8963 constructor. Below example only illustrates how to use the constructor.

```python
from machine import I2C, Pin
from mpu9250 import MPU9250
from ak8963 import AK8963

i2c = I2C(scl=Pin(22), sda=Pin(21))
dummy = MPU9250(i2c) # this opens the bybass to access to the AK8963

ak8963 = AK8963(
    i2c,
    offset=(-136.8931640625, -160.482421875, 59.02880859375),
    scale=(1.18437220840483, 0.923895823933424, 0.931707933618979)
)

sensor = MPU9250(i2c, ak8963=ak8963)
```

## Gyro Calibration

TODO

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tuupola/micropython-mpu9250",
    "name": "micropython-mpu9250",
    "maintainer": "Mika Tuupola",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "tuupola@appelsiini.net",
    "keywords": "accelerometer,gyro,magnetometer,micropython,i2c",
    "author": "Mika Tuupola",
    "author_email": "tuupola@appelsiini.net",
    "download_url": "https://files.pythonhosted.org/packages/0d/18/2cd76280acc1ed4557003aeef0751ff5c081a9c993a20a2af3697f08c7f2/micropython-mpu9250-0.4.0.tar.gz",
    "platform": null,
    "description": "# MicroPython MPU-9250 (MPU-6500 + AK8963) I2C driver\n\nMPU-9250 is a System in Package (SiP) which combines two chips: MPU-6500 which contains 3-axis gyroscope and 3-axis accelerometer and an AK8963 which is a 3-axis digital compass.\n\n## Usage\n\nKevin Wheeler has an [extensive video](https://www.youtube.com/watch?v=ph10GSO8pDk) describing how to use this library. The short code snippets in this README should also help you to get started. First is simple example with never ending loop.\n\n```python\nimport utime\nfrom machine import I2C, Pin\nfrom mpu9250 import MPU9250\n\ni2c = I2C(scl=Pin(22), sda=Pin(21))\nsensor = MPU9250(i2c)\n\nprint(\"MPU9250 id: \" + hex(sensor.whoami))\n\nwhile True:\n    print(sensor.acceleration)\n    print(sensor.gyro)\n    print(sensor.magnetic)\n    print(sensor.temperature)\n\n    utime.sleep_ms(1000)\n```\n\nBy default the library returns 3-tuple of X, Y, Z axis values for either acceleration, gyroscope and magnetometer ie compass. Default units are `m/s^2`, `rad/s`, `uT` and `\u00b0C`. It is possible to also get acceleration values in `g` and gyro values `deg/s`. See the example below. Note that both the MPU6500 and the AK8963 drivers are available as separate classes. MPU9250 is actually a composite of those two.\n\n```python\nimport utime\nfrom machine import I2C, Pin\nfrom mpu9250 import MPU9250\nfrom mpu6500 import MPU6500, SF_G, SF_DEG_S\n\ni2c = I2C(scl=Pin(22), sda=Pin(21))\nmpu6500 = MPU6500(i2c, accel_sf=SF_G, gyro_sf=SF_DEG_S)\nsensor = MPU9250(i2c, mpu6500=mpu6500)\n\nprint(\"MPU9250 id: \" + hex(sensor.whoami))\n\nwhile True:\n    print(sensor.acceleration)\n    print(sensor.gyro)\n    print(sensor.magnetic)\n    print(sensor.temperature)\n\n    utime.sleep_ms(1000)\n```\n\nMore realistic example usage with timer. If you get `OSError: 26` or `i2c driver install error` after soft reboot do a hard reboot.\n\n```python\nimport micropython\nfrom machine import I2C, Pin, Timer\nfrom mpu9250 import MPU9250\n\nmicropython.alloc_emergency_exception_buf(100)\n\ni2c = I2C(scl=Pin(22), sda=Pin(21))\nsensor = MPU9250(i2c)\n\ndef read_sensor(timer):\n    print(sensor.acceleration)\n    print(sensor.gyro)\n    print(sensor.magnetic)\n    print(sensor.temperature)\n\nprint(\"MPU9250 id: \" + hex(sensor.whoami))\n\ntimer_0 = Timer(0)\ntimer_0.init(period=1000, mode=Timer.PERIODIC, callback=read_sensor)\n```\n\n## Magnetometer Calibration\n\nFor real life applications you should almost always [calibrate the magnetometer](https://www.appelsiini.net/2018/calibrate-magnetometer/). The AK8963 driver supports both hard and soft iron correction. Calibration function takes two parameters: `count` is the number of samples to collect and `delay` is the delay in millisecods between the samples.\n\nWith the default values of `256` and `200` calibration takes aproximately one minute. While calibration function is running the sensor should be rotated multiple times around each axis.\n\nNOTE! If using MPU9250 you will first need to open the I2C bypass access to AK8963. This is not needed when using a standalone AK8963 sensor.\n\n```python\nfrom machine import I2C, Pin\nfrom mpu9250 import MPU9250\nfrom ak8963 import AK8963\n\ni2c = I2C(scl=Pin(22), sda=Pin(21))\n\ndummy = MPU9250(i2c) # this opens the bybass to access to the AK8963\nak8963 = AK8963(i2c)\noffset, scale = ak8963.calibrate(count=256, delay=200)\n\nsensor = MPU9250(i2c, ak8963=ak8963)\n```\n\nAfter finishing calibration the `calibrate()` method also returns tuples for both hard iron `offset` and soft iron `scale`. To avoid calibrating after each startup it would make sense to strore these values in NVRAM or config file and pass them to the AK8963 constructor. Below example only illustrates how to use the constructor.\n\n```python\nfrom machine import I2C, Pin\nfrom mpu9250 import MPU9250\nfrom ak8963 import AK8963\n\ni2c = I2C(scl=Pin(22), sda=Pin(21))\ndummy = MPU9250(i2c) # this opens the bybass to access to the AK8963\n\nak8963 = AK8963(\n    i2c,\n    offset=(-136.8931640625, -160.482421875, 59.02880859375),\n    scale=(1.18437220840483, 0.923895823933424, 0.931707933618979)\n)\n\nsensor = MPU9250(i2c, ak8963=ak8963)\n```\n\n## Gyro Calibration\n\nTODO\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "MicroPython I2C driver for MPU9250 9-axis motion tracking device",
    "version": "0.4.0",
    "project_urls": {
        "Homepage": "https://github.com/tuupola/micropython-mpu9250"
    },
    "split_keywords": [
        "accelerometer",
        "gyro",
        "magnetometer",
        "micropython",
        "i2c"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0d182cd76280acc1ed4557003aeef0751ff5c081a9c993a20a2af3697f08c7f2",
                "md5": "a281f60ac9704a3a1f14967045a358ed",
                "sha256": "d69513d0734e9db370ad6f75d698ddaea817735ce607a66ab3ccf0e08027b17d"
            },
            "downloads": -1,
            "filename": "micropython-mpu9250-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a281f60ac9704a3a1f14967045a358ed",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7091,
            "upload_time": "2023-12-01T18:59:38",
            "upload_time_iso_8601": "2023-12-01T18:59:38.047514Z",
            "url": "https://files.pythonhosted.org/packages/0d/18/2cd76280acc1ed4557003aeef0751ff5c081a9c993a20a2af3697f08c7f2/micropython-mpu9250-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-01 18:59:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tuupola",
    "github_project": "micropython-mpu9250",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "micropython-mpu9250"
}
        
Elapsed time: 0.14594s