{"id":21077394,"url":"https://github.com/ros2jsguy/mpu6050-motion-data","last_synced_at":"2026-04-04T04:32:25.935Z","repository":{"id":94000424,"uuid":"364078790","full_name":"ros2jsguy/mpu6050-motion-data","owner":"ros2jsguy","description":"6-DOF MPU6050 driver providing fused orientation (quaternion), acceleration and rotational data via the on-device digital motion processor (DMP)","archived":false,"fork":false,"pushed_at":"2021-08-24T19:10:16.000Z","size":402,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-26T12:39:17.029Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ros2jsguy.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2021-05-03T22:36:12.000Z","updated_at":"2024-05-28T04:47:00.000Z","dependencies_parsed_at":"2023-07-08T09:48:17.158Z","dependency_job_id":null,"html_url":"https://github.com/ros2jsguy/mpu6050-motion-data","commit_stats":{"total_commits":19,"total_committers":1,"mean_commits":19.0,"dds":0.0,"last_synced_commit":"0819863463db455c88a37b94c9280dba9a5118b2"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ros2jsguy%2Fmpu6050-motion-data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ros2jsguy%2Fmpu6050-motion-data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ros2jsguy%2Fmpu6050-motion-data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ros2jsguy%2Fmpu6050-motion-data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ros2jsguy","download_url":"https://codeload.github.com/ros2jsguy/mpu6050-motion-data/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254496046,"owners_count":22080641,"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":[],"created_at":"2024-11-19T19:36:18.563Z","updated_at":"2026-04-04T04:32:25.929Z","avatar_url":"https://github.com/ros2jsguy.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MPU6050-Motion-Data\nAn efficient MPU6050 driver based on the [I\u003csup\u003e2\u003c/sup\u003eCdevlib library](https://www.i2cdevlib.com/devices/mpu6050), providing fused orientation (quaternion), acceleration and rotational data via the on-device digital motion processor (DMP). Includes sensor offset calibration and raw unfused sensor data. This TypeScript module runs on Raspberry Pi 3 \u0026 4. Communications with MPU6050 hardware is via I\u003csup\u003e2\u003c/sup\u003eC with the MPU6050 in the slave role. I\u003csup\u003e2\u003c/sup\u003eC communications is provided by the [rpio](https://www.npmjs.com/package/rpio) package.\n\n# Contents\n* [Prerequisites](#prerequisits)\n* [Installation](#installation)\n* [Quick Start](#quick-start)\n* [Things to know](#things-to-know)\n* [Using Digital Motion Processing](#using-digital-motion-processing-dmp)\n* [Sensor Calibration](#sensor-calibration)\n* [I\u003csup\u003e2\u003c/sup\u003eC Configuration](#i2c-configuration)\n* [Hardware Interrupts](#hardware-interrupts)\n* [Example](#example)\n\n# Prerequisites\n* Node 10+\n* MPU6050 IC\n* Raspberry Pi 3 or 4 with [I\u003csup\u003e2\u003c/sup\u003eC enabled](https://pimylifeup.com/raspberry-pi-i2c/)\n\n# Installation\n```\nnpm install @ros2jsguy/mpu6050-motion-data\n```\n\n# Quick Start\n\nWith your MPU6050 powered up and connected to a Raspiberry Pi or similar microcontroller (see circuit below), begin by importing this module's key TypeScript constructs.\n\n```javascript\nimport {\n  Data3D, MotionData, MPU6050, Utils \n} from '@ros2jsguy/mpu6050-motion-data';\n```\n\nNext create an instance of the `MPU6050` class to communicate with your \nMPU6050 chip via I\u003csup\u003e2\u003c/sup\u003eC.\n\n```javascript\nconst imu = new MPU6050();\nimu.initialize();\n\n// use testConnection() to ensure the mpu6050 is working properly\nconsole.log('Device connected:', imu.testConnection());\n```\n\nAt this point motion data should be available from the chip. Here's how we access it:\n\n```javascript\nconst motionData = imu.getMotionData();\n// {\n//   accel: {\n//     x:   -56,\n//     y:   128,\n//     z: 16498\n//   },\n//   gyro {\n//     x:   3,\n//     y:   8\n//     z: -21\n//   },\n// }\n```\n\nThe MPU6050 class provides additional methods for accessing a subset of the motion data:\n* getAccel()\n* getGyro() \n\n# Things To Know\n1. Always call MPU6050#initialize() before using new instances of the class.\n```ts\nlet imu = new MPU6050();\nimu.initialize();\n```\n\n2. The default I\u003csup\u003e2\u003c/sup\u003eC address is `0x68` and baud rate is `400000`. See [I\u003csup\u003e2\u003c/sup\u003eC Configuration](#i2c-configuration) to customize these settings.\n\n3. The MPU6050 sensors must be calibrated before use. Do this by computing the accelerometer and gyroscope sensor offsets and applying them before use. See [Sensor Calibration](#sensor-calibration) for details.\n\n4. Volatile Data\n\u003cbr\u003eSeveral of the MPU6050 class methods read and return data from the MPU605 device as a JavaScript Buffer. These buffers are volatile and should never be written to or accessed beyond the use of any method that reads from the MPU6050 device. \n```ts\nlet buf = imu.dmpGetCurrentFIFOPacket(); // buf data is valid\nlet data = imu.dmpGetMotionData(buf); // buf data still valid\nimu.dmpPacketAvailable() // buf data invalid \n```\n\n# Using Digital Motion Processing (DMP)\n\nWhere this library excels over other Nodejs MPU6050 drivers is its access to the MPU6050 Digital Motion Processing (DPU) Unit. The DPU performs complex sensor fusion computation directly on the MPU6050 which can greatly improve the performance of your application. See [The MPU6050 Explained](https://mjwhite8119.github.io/Robots/mpu6050) for an expanded description of the DMP feautures.\n\nNote that use of DMP is optional.  \n\nWhen DMP is enabled the MPU6050 is configured as follows:\n* DMP runs at 200 Hz\n* Sensor data is output to the FIFO\n* The FSYNC/INT pin has a 50 us pulse sent every 5 ms to indicate data is available\n* Clock Source = X-Gyro\n* Accelerometer Full-scale Range = 2g\n* Gyroscope Full-scale Range = +-2000 deg/sec\n* DMP Raw Data Interrupt is enabled\n* Interrupt is cleared on any read\n* Rate divider = 4\n\n\nThe MPU6050 class `dmpInitialize()` method uploads the [InvenSense Motion Driver v6.12](https://www.digikey.com/en/pdf/i/invensense/motion-driver-6-1-user-guide) image to the DMP. The DMP then continuously produces fused sensor data stored into FIFO memory.\n\n\n\n# Sensor Calibration\nMPU6050-Motion-Data provides the *calibrate* script and an api for calculating and loading the MPU6050 accelerometer and gyro sensor offsets used by the DMP.\n\nTo use the calibrate utility, open a terminal on your Raspberry Pi with current working directory (cwd) in your Nodejs project for which @ros2jsguy/mpu6050-motion-data has been added as a dependency. \n\nNext run the following command:\n```shell\nsudo npx calibrate\n\n//           X Accel  Y Accel  Z Accel   X Gyro   Y Gyro   Z Gyro\n//OFFSETS      -4470    -1753     2100       94      129      145\n\n```\nThe utility outputs the accelerometer and gyroscope offsets for future use with the `mpu6050.setSensorOffsets()` method.\n\n```javascript\n// example\nmpu6050.setSensorOffsets(-4470, -1753, 2100, 94, 129, 145);\n```\n\nAlternatively you can use the mpu6050 api as shown below to dynamically compute offsets during your application's initialization.\n\n```javascript\nmpu6050.calibrateAccel();\nmpu6050.calibrateGyro();\n```\n\n# Data-Ready Hardware Interrupt\nThe MPU6050 can be configured to generate an interrupt signal on the INT pin when data is ready. The INT pin has two modes for signalling a data-ready event.\n\n![GY-521 breakout board](docs/mpu6050-int.png)\n\nThe default mode is to apply a 50 us active state pulse on the INT pin. The default active state is HIGH. Alternatively, the interrupt system can be programmed to set the INT pin to its active state when data is ready and to remain active until the interrupt is cleared. This is known as the \"latch\" mode.\n\n```javascript\n// enable latch signaling mode on the INT pin \nsetInterruptLatchEnabled(true);\n```\n\nThe data-ready interrupt is cleared by either directly reading the data-ready interrupt register or by configuring the interrupt system to automatically clear the data-ready interrupt when any register is read.\n\nInternally the \"onoff\" GPIO library is used for polling GPIO pins for state changes. This library lacks the polling resolution to consistently detect the 50 us active state pulses of the default INT pin signaling mode. Therefore, the \"latch\" mode is recommended where the sensitivity of your GPIO pin polling may miss a data-ready interrupt signal.  \n\n```javascript\n// Data-ready interrupt is cleared only when reading \n// the data-ready interrupt \nsetInterruptClearMode(0);\n\n// Clear the data-ready interrupt on any data read\nsetInterruptClearMode(1);\n```\n\n# I\u003csup\u003e2\u003c/sup\u003eC Configuration Options\nThe MPU6050 class constructor can accept an optional `I2COptions` parameter for specifying the I\u003csup\u003e2\u003c/sup\u003eC address and baud rate for communicating with the MPU6050 chip.\n\nThe default I\u003csup\u003e2\u003c/sup\u003eCOptions are:\n```javascript\n{\n  i2cAddress: 0x68,\n  baudRate: 400000\n}\n```\n\nHere is an example using the alternate MPU6050 I\u003csup\u003e2\u003c/sup\u003eC address.\n\n```javascript\nnew MPU6050({i2cAddress: 0x69});\n```\n\n\n# Example\nIn this example a Raspberry Pi 4 communicates with a MPU6050 on a low cost [GY-521 breakout board](https://www.amazon.com/gp/product/B01DK83ZYQ) via I\u003csup\u003e2\u003c/sup\u003eC. The MPU6050 operates in I\u003csup\u003e2\u003c/sup\u003eC slave mode with a default I\u003csup\u003e2\u003c/sup\u003eC address of `0x68`. Note the orientation markings of your breakout board for alignment with the data output in the following example.\n\nThe wiring is simple with the GY-521 board powered directly from the 3.3V output of the RPI. \n\n![Fizz schematic](docs/mpu6050.fzz.png)\n\nNote you must enable I\u003csup\u003e2\u003c/sup\u003eC on your RPI device as it is typically not enabled by default. Additionally you may need to run in `sudo` mode.\n\n*I set up this example with I\u003csup\u003e2\u003c/sup\u003eC enabled on my RPI running Ubuntu 20.4 and node 12.*\n\n```shell\ntsc \u003cyour-example.ts\u003e\nsudo node \u003cyour-example.js\u003e\n```\n\nTypeScript example code:\n```javascript\nimport { InterruptMonitor, MPU6050, Utils } from \"@ros2jsguy/mpu6050-motion-data\";\n\nconst GPIO_MPU6050_DATA_PIN = 18;\n\nfunction main() {\n  let interrupts = 0;\n\n  const imu = new MPU6050();\n  imu.initialize();\n  \n  console.log('MPU6050 Device')\n  console.log('       connected: ', imu.testConnection());\n  console.log('              id: ', imu.getDeviceID());\n  console.log('  temperature(F): ', Utils.celciusToF(imu.getTemperature()).toFixed(2));\n  Utils.msleep(500);\n\n  console.log('\\nDMP initialize and calibrate...');\n  imu.dmpInitialize();\n\n  // calibrate sensors\n  imu.calibrateAccel();\n  imu.calibrateGyro();\n  imu.printActiveOffsets();\n\n  // setup interrupt\n  imu.setInterruptLatchEnabled(true);\n  imu.setInterruptDMPEnabled(true);\n\n  // setup interrupt event monitoring and handling\n  const interruptMonitor = new InterruptMonitor(GPIO_MPU6050_DATA_PIN);\n  interruptMonitor.on('data', () =\u003e {\n      if (++interrupts === 1) console.log('  Receiving interrupt(s)');\n      const buf = imu.dmpGetCurrentFIFOPacket();\n      if (buf) {\n        const data = imu.dmpGetMotionData(buf);\n        console.log(data);\n      }\n  });\n  interruptMonitor.on('error', (error: Error) =\u003e {\n    console.log('Data error:', error.message);\n  });\n  interruptMonitor.start();\n\n  // run for 10 seconds then shutdown process\n  setTimeout(()=\u003e{\n    imu.shutdown();\n    interruptMonitor.shutdown();\n    \n    process.exit(0);\n  }, 10000);\n\n  console.log('\\nSampling data for 10 seconds');\n  imu.setDMPEnabled(true); // start DMP, data-ready interrupts should be raised\n  console.log('  Waiting for interrupts');\n}\n\nmain();\n```\n\n# Resources\n1. [MPU-6050 Specification Datasheet](https://www.digikey.com/en/datasheets/tdk-invensense/tdk-invensense-rm-mpu-6000a)\n2. [MPU-6050 Register Map and Descriptions](https://www.digikey.com/en/datasheets/tdk-invensense/tdk-invensense-rm-mpu-6000a)\n3. [Motion Driver 6.1 - User Guide](https://www.digikey.com/en/pdf/i/invensense/motion-driver-6-1-user-guide)\n4. [The MPU6050 Explained](https://mjwhite8119.github.io/Robots/mpu6050)\n4. [mpu6050 Arduino Library Github repo by Electronic Cats](https://github.com/ElectronicCats/mpu6050)\n5. [I\u003csup\u003e2\u003c/sup\u003eCdevlib MPU6050 library](https://www.i2cdevlib.com/devices/mpu6050)\n\n# Credits\nMPU6050-Motion-Data is mostly a direct TypeScript translation of the I\u003csup\u003e2\u003c/sup\u003eCdevlib MPU6050 Arduino library. Thus all credit goes to it's principle author, Jeff Rowberg.\n\n# Contributions\nAll assistance welcome. Please fork this repo, make your proposed changes and submit as a PR. \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fros2jsguy%2Fmpu6050-motion-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fros2jsguy%2Fmpu6050-motion-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fros2jsguy%2Fmpu6050-motion-data/lists"}