{"id":14967266,"url":"https://github.com/micropython-imu/micropython-mpu9x50","last_synced_at":"2025-04-09T08:03:40.896Z","repository":{"id":33711232,"uuid":"37364687","full_name":"micropython-IMU/micropython-mpu9x50","owner":"micropython-IMU","description":"Drivers for InvenSense inertial measurement units MPU9250, MPU9150, MPU6050","archived":false,"fork":false,"pushed_at":"2024-10-23T09:43:06.000Z","size":163,"stargazers_count":267,"open_issues_count":3,"forks_count":83,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-04-09T08:03:30.423Z","etag":null,"topics":["accelerometer","driver","magnetometer","micropython","mpu9150","mpu9250","sensor"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/micropython-IMU.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-06-13T08:51:59.000Z","updated_at":"2025-04-03T06:50:14.000Z","dependencies_parsed_at":"2024-12-08T17:03:12.402Z","dependency_job_id":"4ab6a5ff-28b4-4a7b-a51b-bc0fe88fd2e5","html_url":"https://github.com/micropython-IMU/micropython-mpu9x50","commit_stats":{"total_commits":34,"total_committers":4,"mean_commits":8.5,"dds":"0.32352941176470584","last_synced_commit":"ae68046e310224ebc3e196a1e3692de018991dbb"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropython-IMU%2Fmicropython-mpu9x50","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropython-IMU%2Fmicropython-mpu9x50/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropython-IMU%2Fmicropython-mpu9x50/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropython-IMU%2Fmicropython-mpu9x50/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/micropython-IMU","download_url":"https://codeload.github.com/micropython-IMU/micropython-mpu9x50/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247999859,"owners_count":21031046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["accelerometer","driver","magnetometer","micropython","mpu9150","mpu9250","sensor"],"created_at":"2024-09-24T13:37:45.243Z","updated_at":"2025-04-09T08:03:40.879Z","avatar_url":"https://github.com/micropython-IMU.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"As of now, this repo holds:\n* vector3d - a vector class for IMU devices\n* imu - a base class for MPU9x50 devices. Supports the MPU6050.\n* mpu9150 - a class for the MPU9150\n* mpu9250 - a class for the MPU9250\n\nvector3d will eventually be spun out to serve as common starting point for other\nimu-device drivers.  \nDocumentation for the MPU9150/MPU6050 can be found [here](./README_MPU9150.md).\n\n# Module mpu9250\n\nmpu9250 is a micropython module for the InvenSense MPU9250 sensor.\nIt measures acceleration, turn rate and the magnetic field in three axes.  \nBreakout board: [Drotek](https://store.drotek.com/imu-10dof-mpu9250-ms5611),\n[Sparkfun](https://www.sparkfun.com/products/13762)  \n\nThe MPU9250 has a number of advantages over the MPU9150 (which has manufacturing status\n\"not recommended for new designs\"). The magnetometer is capable of continuous operation,\nupdating at 100Hz. It supports separate filters for accelerometer and gyro. And the\nbreakout board is cheaper. It also supports SPI however this driver assumes an I2C connection.\n\n# Introduction\n\nThe MPU9250 is a dual chip module, with the magnetometer provided by an AsahaiKASEI AK8963 chip.\nIn consequence the coordinate system of the magnetometer is not aligned with that\nof the other components. This driver corrects this so that the axes of each instrument\ncorrespond with those of the accelerometer.\n\nSince the driver is likely to be used with the [sensor fusion module](https://github.com/micropython-IMU/micropython-fusion.git)\nthe orientation of the sensor relative to the vehicle is significant. The Madgwick algorithm assumes\nx is orientated towards the front of the vehicle, y is left-right, and z is down. To accommodate\ncases where the sensor is mounted orthogonally to this orientation, support is provided for inverting and\ntransposing axes. The driver returns vehicle-relative coordinates.\n\n### Wiring the sensor to the pyboard\n\nThe MPU9250 chip does not support 5V operation however the Drotek board has a voltage regulator so\ncan be powered from 3.3V or 5V.\n\n| pyboard| mpu9250 |\n|:------:|:-------:|\n| VIN    | 3V3     |\n| GND    | GND     |\n| SCL    | SCL     |\n| SDA    | SDA     |\n\n### Quickstart\n\nExample assuming an MPU9250 connected to 'X' I2C interface on the Pyboard:\n```python\nfrom mpu9250 import MPU9250\nimu = MPU9250('X')\nprint(imu.accel.xyz)\nprint(imu.gyro.xyz)\nprint(imu.mag.xyz)\nprint(imu.temperature)\nprint(imu.accel.z)\n```\n\n# Modules\n\nTo employ the driver it is only necessary to import the mpu9250 module and to use the ``MPU9250`` class.\n\n### mpu9250\n\n``MPU9250``  \nClass for the MPU9250 sensor.  \n``MPUException``  \nThis will be raised in the event of an I2C error. It is derived from the Python ``OSError``.\n\n### imu\n\n``InvenSenseMPU``  \nBase class for InvenSense inertial measurement units.\n\n### vector3d\n\n``Vector3d``  \nClass for a 3D vector. This is documented [here](./vector3d.md).\n\n# MPU9250 Class\n\nThe class has various properties returning Vector3d instances. The principal properties\nof the Vector3d are ``x``, ``y`` and ``z`` returning the vector's components and ``xyz`` which returns\na 3-tuple of the components (x, y, z). It also supports calibration and conversion to vehicle\nrelative coordinates.\n\n## Methods\n\n``MPU9250()`` The constructor supports the following arguments  \n  1. side_str 'X' or 'Y' (mandatory) defines the I2C interface in use. Alternatively an initialised\nI2C object may be passed.\n  2. device_addr 0 or 1 (optional) Two devices may be used with addresses determined by the voltage\non the AD0 pin. If only one device is used, this argument may be None when the device\nwill be automatically detected.\n  3. transposition (optional) Enables axes to be transposed (see below).\n  4. scaling  (optional) Enables axes to be inverted (see below).\n\nThe defaults for transposition and scaling will cause the driver to return sensor-relative results.\n\n`` wake()``  \nwakes the device  \n\n``sleep()``  \nsets the device to sleep mode  \n\n``get_accel_irq()``  \n``get_gyro_irq()``  \n``get_mag_irq()``  \nThese methods are somewhat experimental. They are capable of being called from within an\ninterrupt callback and update the integer properties only of the relevant Vector3D object.\nCurrently writing nontrivial MicroPython interrupt callbacks is something of a black art\nas it can be unclear when the heap is likely to be invoked causing an exception.\n\n## Principal Properties\n\n``sensors``  \nReturns three Vector3d objects, accelerometer, gyro and magnetometer.\n\n``accel_range`` integer 0 to 3 read/write  \nReturns or sets the current accelerometer range: this determines the accelerometer full scale\nrange as per the table below. Note that the x, y, z values from the driver are scaled to units\nof g regardless of the range selected. Range affects only the full scale capability and resolution.\n\n| Range | Value (+-g) |\n|:-----:|:-----------:|\n|   0   |    2        |\n|   1   |    4        |\n|   2   |    8        |\n|   3   |   16        |\n\n``gyro_range`` integer 0 to 3 read/write  \nDetermines the gyro full scale range as per the table below. Note that the x, y, z\nvalues from the driver are scaled to units of degs/s regardless of the range selected.\nRange affects only the full scale capability and resolution.\n\n| Range | Value +- degs/sec |\n|:-----:|:-----------------:|\n|   0   |       250         |\n|   1   |       500         |\n|   2   |      1000         |\n|   3   |      2000         |\n\n``temperature`` float read only  \nReturns the chip temperature in degrees celcius\n\n``accel`` Vector3d instance read only  \nReturns the ``Vector3d`` holding the current accelerometer data. Units are g.\n\n``gyro`` Vector3d instance read only  \nReturns the ``Vector3d`` holding the current gyro data. Units degrees/s.\n\n``mag``  Vector3d instance read only  \nReturns the ``Vector3d`` holding the current magnetometer data. As configured by this\ndriver the MPU9250 reads the magnetometer at 10mS intervals. Units are uT (microtesla).\n\nAccessing a Vector3d instance x, y, z or xyz properties will cause the device to be\nread and will return the latest data. In the case of the magnetometer, if it is accessed,\nand then accessed again within 10mS and before the unit has had a chance to update,\nthe old data will be returned and a counter ``MPU9250.mag_stale_count`` will be\nincremented. The counter will be cleared the first time fresh data is acquired and read.\n\nIn the event that the most recent data read by the magnetometer is in error the Vector3d\ninstance will return the most recent valid data and increment ``mag_stale_count``.\nA high value of ``mag_stale_count`` (or any nonzero value if the read rate is below 100Hz)\nsuggests an error condition.\n\n``mag_stale_count`` integer read only  \nAs described above: a count of the number of consecutive times in the curent sequence\nof reads that the driver has returned out-of-date values.\n\n``accel_filter_range`` integer 0 to 7 read/write  \nThe digital low pass filter enables the effect of vibration to be reduced in the\naccelerometer readings. The following table gives the approximate bandwidth\nand delay for the filter.\n\n| value | bw(Hz) | Delay(mS) |\n|:-----:|:------:|:---------:|\n|  0    |  460   |    1.94   |\n|  1    |  184   |    5.8    |\n|  2    |   92   |    7.8    |\n|  3    |   41   |   11.8    |\n|  4    |   20   |   19.8    |\n|  5    |   10   |   35.7    |\n|  6    |    5   |   66.96   |\n|  7    |  460   |    1.94   |\n\nNote: in my testing option 7 produced garbage.\n\n``gyro_filter_range`` integer 0 to 7 read/write  \n\nThe digital low pass filter enables the effect of vibration to be reduced in the\ngyro readings. The following table gives the approximate bandwidth and delay for the filter.\n\n| value | bw(Hz) | Delay(mS) |\n|:-----:|:------:|:---------:|\n|  0    |  250   |    0.97   |\n|  1    |  184   |    2.9    |\n|  2    |   92   |    3.9    |\n|  3    |   41   |    5.9    |\n|  4    |   20   |    9.9    |\n|  5    |   10   |   17.85   |\n|  6    |    5   |   33.48   |\n|  7    | 3600   |    0.17   |\n\nSee \"Other MPU9250 properties\" below.\n\n#### Use in interrupt callbacks\n\nMicroPython interrupt callbacks prohibit the use of the heap, which rules out a number of\nstandard Python techniques including exception handling and the use of floating point.\nCurrently it is not always evident whether code will use the heap, so any use of these\ntechniques should be considered experimental. The following MPU9250 methods provide access\nto the device where this must be performed in a callback.\n\n``get_accel_irq()``  \n``get_gyro_irq()``  \n``get_mag_irq()``  \nThese methods read the device and update the integer values of the Vector3D objects only.\nThese values hold unscaled values direct from the device so coordinates are device\nrelative and no calibration, correction or scaling is applied.\n\nNote that the scaling factors of the accelerometer and gyro depend only on the range\nselected, hence these can be hard coded in the callback. To get the most accurate readings\nfrom the magnetometer the factory-set corrections should be applied. These are in\nthe property ``mag_correction`` but being floating point values cannot be used in a callback.\nOptions (depending on application) are to apply them outside the callback, or convert\nthem to scaled integers in initialisation code.\n\nSee tests/irqtest.py for example code.\n\n## Other MPU9250 properties\n\n``passthrough`` Boolean read/write  \nsets passthrough mode. It is activated (True) by default. This needs to be activated\nto enable the magnetometer interface to be linked to the SDA, SCL pads.\n\n``sample_rate`` 8 bit integer read/write  \nDetermines the update rate of the sensor registers. Values should be in range 0 to 255.\nThe update rate is given by rate = Internal_Sample_Rate / (1 + sample_rate). It is not\nclear, given the subset of functionality supported by this driver, why you might want\nto change this from the zero default.\n\n``mag_correction`` float 3-tuple  \nHolds factory-set magnetometer correction values.\n\n``chip_id``  \nThe ID of chip (113) is hardcoded on the sensor. Reading this property will test the\ncommunications with the IMU by reading this value which is returned to the user. A\nValueError will be raised if the value is incorrect.\n\n``mpu_addr``  \nI2C adress of the accelerometer and the gyroscope.\n\n``mag_addr``  \nI2C adress of the magnetometer.\n\n``timeout``  \nTimeout for I2C operations.\n\n# Exception handling\n\nIncorrect values such as\n```python\nimu = MPU9250('Z')\n```\nwill raise a ValueError with a meaningful error message.  \nWhen any communication with the IMU takes place it is possible for the I2C bus to lock up.\nThis is normally a consequence of hardware failure such as the device being connected incorrectly\nor leads being too long. In this instance a custom MPUException will be raised with a\ndscriptive message. This is derived from Python's OSError: the user may trap either in the hope\nof continuing operation. In my experience this seldom works: if the I2C bus locks up a\npower cycle is required to clear it.\n\n# Demo of calibration\n\n```python\n\u003e\u003e\u003e from  mpu9250 import MPU9250\n\u003e\u003e\u003e a = MPU9250('x')\n\u003e\u003e\u003e a.mag.cal\n(0, 0, 0)\n\u003e\u003e\u003e import pyb\n\u003e\u003e\u003e sw = pyb.Switch()\n\u003e\u003e\u003e a.mag.calibrate(sw) # User rotates unit about each axis then presses the Pyboard switch\n\u003e\u003e\u003e a.mag.cal\n(35.30567, 18.92022, -9.428905)\n\u003e\u003e\u003e\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicropython-imu%2Fmicropython-mpu9x50","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicropython-imu%2Fmicropython-mpu9x50","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicropython-imu%2Fmicropython-mpu9x50/lists"}