Code Monkey home page Code Monkey logo

mpu6050's Introduction

mpu6050

license: MIT latest PyPI version PyPI download count

A Python module for accessing the MPU-6050 digital accelerometer and gyroscope on a Raspberry Pi.

Example

Assuming that the address of your MPU-6050 is 0x68, you can read read accelerometer data like this:

>>> from mpu6050 import mpu6050

>>> sensor = mpu6050(0x68)

>>> accelerometer_data = sensor.get_accel_data()

Dependencies

Either the python-smbus or python3-smbus package, according to your Python version.

Installation

There are two ways of installing this package: via PyPi or via the git repository. Installing from the git repository insures that you have the absolute latest version installed, but this can be prone to bugs.

1. install the python-smbus package

sudo apt install python3-smbus

2a. Install this package from PyPi repository

pip install mpu6050-raspberrypi

Or:

2b. Clone the repository and run setup.py

git clone https://github.com/m-rtijn/mpu6050.git
python setup.py install

Issues & Bugs

Please report any issues or bugs here:

https://github.com/m-rtijn/mpu6050/issues

License

Copyright (c) 2015, 2016, 2017, 2018, 2021 Martijn ([email protected]) and contributors. Available under the MIT License. For more information, see LICENSE.

mpu6050's People

Contributors

davidmam avatar fredrikaugust avatar m-rtijn avatar nickcoutsos avatar s-gol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mpu6050's Issues

'module' object is not callable

when I try to run the example code:

from mpu6050 import mpu6050
sensor = mpu6050(0x68)
print(sensor.get_accel_data())

It prints out an error on line 2 stating: 'module' object is not callable
I've installed it correctly, and there's no error when importing the module

OSError: 121

I just got a this libary installed and pluged in and when I run any thing that retruns a map it reads this error:
Traceback (most recent call last): File "/home/pi/Desktop/IMU_MPU6050.py", line 7, in <module> accelerometer_data = IMU.get_gyro_data() File "/home/pi/.local/lib.python3.9/site-packages/mpu6050/mpu6050.py, line 235, in get_gyro_data z = self.read_i2c_word(self.GYRO_ZOUT0) File "/home/pi/.local/lib.python3.9/site-packages/mpu6050/mpu6050.py, line 87, in read_i2c_word low = self.bus.read_byte_data(self.address, register + 1) File "/home/pi/.local/lib/python3.9/site-packages/smbus/util.py", line 59, in validator return fn(*args, **kwdefults) File "/hoome/pi/.locallib/python3.9/site-packages/smbus/smbus.py", line 132, in read_byte_data raise IOError(ffi.errno) OSError: 121

ModuleNotFoundError when importing mpu6050

the smbus installation worked, I was able to test the i2c and that works. I also got no errors when installing through pip install. However as soon as I try to run a script in Python3, it says that:
Traceback (most recent call last):
File "/home/pi/Documents/gyrompu.py", line 1, in
import mpu6050 as mpu6050
ModuleNotFoundError: No module named 'mpu6050'

no checking connectivity

after cloning the file, everything works but i don't get the line that says checking connectivity... done.
the next line 'python setup.py install' seems to have no action then because it cannot find the file

Gyro and accelerometer range

Hello,

In mpu6050.py you seem to always be reading raw values using the raw range in get_gyro_data() and get_accel_data():

 def read_accel_range(self, raw = False):
        raw_data = self.bus.read_byte_data(self.address, self.ACCEL_CONFIG)
        if raw is True:
            return raw_data
        elif raw is False:
            ...

def get_accel_data(self, g = False):
        accel_range = self.read_accel_range(True)

So I was wondering what is the point of having such an if/else code block ?

All the best;

Access data from DMP?

Is it possible to modify the library in order to read data directly from the DMP?

accelometer data

When I getting data from mpu6050, even if I hold the sensor on my hand and don't allow to move, I still see 1g acceleration on my raspberry. When I drop the sensor, I still see 1g acceleration. What should I do to make difference between hold and drop the sensor?

OSError: [Errno 121] Remote I/O error

  File "/home/pi/.local/lib/python3.7/site-packages/mpu6050/mpu6050.py", line 60, in __init__
    self.bus.write_byte_data(self.address, self.PWR_MGMT_1, 0x00)
OSError: [Errno 121] Remote I/O error

I2C is enabled

Init_py Type error

20240417_19h31m17s_grim

File init.py has an extra period inside the script

It only works for me when I rewrite the code like this:

from mpu6050 import mpu6050

Before this I was receiving the following message:
Traceback (most recent call last):
File "/home/labgnss/imu_project/mpu_6050_ex3.py", line 6, in
mpu6050 = mpu6050.mpu6050(0x68)
^^^^^^^^^^^^^^^
AttributeError: type object 'mpu6050' has no attribute 'mpu6050'

OS Error : [Errorno 121] Remote I/O error

Hello,
I'm a student and I'm trying to implement a gimbal servoed on 1 axis with cheap components. To do that I bought a MPU6050 and a motoreductor. I have succeded to get angle on the X axis. However, when I'm trying to use these values to servo my motor, and when my motor starts to turn, the error 121 occured.

Traceback (most recent call last): File "/home/pi/Documents/TIPE/Programme de stabilisation.py", line 224, in <module> accel_data=mpu.get_accel_data() File "/usr/local/lib/python3.7/dist-packages/mpu6050_raspberrypi-1.1-py3.7.egg/mpu6050/mpu6050.py", line 139, in get_accel_data x = self.read_i2c_word(self.ACCEL_XOUT0) File "/usr/local/lib/python3.7/dist-packages/mpu6050_raspberrypi-1.1-py3.7.egg/mpu6050/mpu6050.py", line 72, in read_i2c_word low = self.bus.read_byte_data(self.address, register + 1) OSError: [Errno 121] Remote I/O error

I tried to reduce the sample rate at the exit from the MPU6050, and to reduce the lenght of the wires as I have seen on other platefroms, but it doesn't work.

Let me know if you have any solutions !

P.S : I have seen the previous thread on this error but it wasn't the same issue I think ( I have connected the SCL and the SDA on the GPIO 2 and 3 )

FileNotFoundError: [Errno 2] No such file or directory

self.bus = smbus.SMBus(bus)
is the code the error references to. I ran
sudo apt install python3-smbus
pip3 install mpu6050-raspberrypi

here is the code until it breaks

from mpu6050 import mpu6050

sensor = mpu6050(0x68)```

I0Error

Hey, seems like I've gotten the module installed correctly but when trying to run some sample code just to check if it will compile:

`from time import sleep
import RPi.GPIO as GPIO
from mpu6050 import mpu6050

sensor = mpu6050(0x68)

while True:
accel_data = sensor.get_accel_data()
gyro_data = sensor.get_gyrp_data()
temp = sensor.get_temp()
`
I get this error:

2017-04-30-105728_1824x984_scrot

Any ideas what that may be referring to / how to fix?
Looking into that directory it seems like all the files are properly placed.
Using Pi 3 if that changes anything.

Thanks!

Only zeros

When I read from the sensor I only have zeros. I set the baud rate in /boot/config.txt:
dtparam=i2c_baudrate=400000

What do I need to do on a fresh RPI?

Calibration?

How do you enter the offsets acquired during calibration? Otherwise, the values read from the device are raw and meaningless.

Ask

How to know the register map of MPU 6050 without a documentation ? by the way thank you.

Not defined

So I in fact am a little new to programming, however ive used sample code from here a few times successfully. I'm running into an issue with yours however. When I go to run the sample code with my mpu6050, it's telling me that line 6 "from mpu6050 import mpu6050" there is an import error and that no module exists. Its also said in your other code that mpu6050 line 2xx "mpu6050" is undefined. I am sure I'm missing something that I didn't install from you or something of this nature. Could you please provide some insight as to what could possibly fix my issue? Thank you.

TypeError: 'module' object is not callable

python3 sensor.py

Traceback (most recent call last):
File "sensor.py", line 3, in
mpu = mpu6050(0x68)
TypeError: 'module' object is not callable

my code:

from mpu6050 import mpu6050
import time
mpu = mpu6050(0x68)

while True:
    print("Temp : "+str(mpu.get_temp()))
    print()

    accel_data = mpu.get_accel_data()
    print("Acc X : "+str(accel_data['x']))
    print("Acc Y : "+str(accel_data['y']))
    print("Acc Z : "+str(accel_data['z']))
    print()

    gyro_data = mpu.get_gyro_data()
    print("Gyro X : "+str(gyro_data['x']))
    print("Gyro Y : "+str(gyro_data['y']))
    print("Gyro Z : "+str(gyro_data['z']))
    print()
    print("-------------------------------")
    time.sleep(1)

Documentation overhaul

I am just looking into this module and it seems interesting, would you be interested in me going through and re-vamping the docstrings and then generating a GitHub pages compatible docs site to make the package a bit more accessible (this is a free service by GitHub, there is no associated hosting fee)?

An example of what this would look like can be found here

I want to check first before doing all the work and having no one on the team be interested, let me know if you have any questions or are interested.

Thanks!

IOE error

Dear Tijndagamer,
when I set up the gyro, following error message popped up.

File "./gyro.py", line 6, in
sensor = mpu6050(0x68)
File "/usr/local/lib/python2.7/dist-packages/mpu6050/mpu6050.py", line 60, in init
self.bus.write_byte_data(self.address, self.PWR_MGMT_1, 0x00)
File "/usr/local/lib/python2.7/dist-packages/smbus/util.py", line 59, in valid ator
return fn(*args, **kwdefaults)
File "/usr/local/lib/python2.7/dist-packages/smbus/smbus.py", line 145, in wri te_byte_data
raise IOError(ffi.errno)
IOError: 110

I made sure physical setting was correct, pre-req libraries are all installed and updated, I did a "i2cdetect -r 2" check and I can see 0x68.

Can you please give any hint in terms of solving this problem?

Thanks!

AttributeError: 'int' object has no attribute 'read_i2c_word'

sensor = mpu6050(0x68)

Running this gives the following error -
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/dist-packages/mpu6050/mpu6050.py", line 139, in get_accel_data
x = self.read_i2c_word(self.ACCEL_XOUT0)
AttributeError: 'int' object has no attribute 'read_i2c_word'

In get_all_data self is missing

Code should be:

def get_all_data(self):
    """Reads and returns all the available data."""
    temp = self.get_temp()
    accel = self.get_accel_data()
    gyro = self.get_gyro_data()

    return [accel, gyro, temp]

MODULE ERROR

Traceback (most recent call last):
File "HTR.py", line 6, in
mpu = mpu6050(0x68)
TypeError: 'module' object is not callable

Python3 support

Support python3 by switching to the smbus2 drop-in replacement for the python-smbus package.

The python3-dev branch should work with python3 and python2, but I can't test it at the moment.

Getting Tracebacks on calling get_accel_data()

We are testing mpu6050 and are currently getting such tracebacks :
``
Traceback (most recent call last):

File "balance.py", line 72, in

accel_data = sensor.get_accel_data()

File "/usr/local/lib/python3.5/dist-packages/mpu6050/mpu6050.py", line 141, in get_accel_data

z = self.read_i2c_word(self.ACCEL_ZOUT0)

File "/usr/local/lib/python3.5/dist-packages/mpu6050/mpu6050.py", line 71, in read_i2c_word

high = self.bus.read_byte_data(self.address, register)

OSError: [Errno 121] Remote I/O error
``

What can be the reason for these tracebacks?
cc @Manvi07

Any possibility of a pypi release?

I don't know how much interest you'd have in maintaining it, and I guess releases might not be so common anyway but it might be nice to provide this as a package on pypi for easy installation via pip.

I did it locally with the following setup.py:

from setuptools import setup

setup(
    name="MPU6050",
    version="1.0.0",
    url="https://github.com/Tijndagamer/MPU-6050-Python",
    py_modules=["MPU6050"],
    description=("Handles the I2C communication between a Raspberry Pi and a MPU-6050 Gyroscope/Accelerometer sensor."),
    author="MrTijn/Tijndagame",
    license="MIT"
)

I could be wrong but I think even just having that in the repo (no published package) would allow people to install via pip install https://github.com/Tijndagamer/MPU-6050-Python or maybe a git URL.

In any case, thanks very much for writing this module, I've just started playing around with the Pi and this has been a huge help.

Raspberry Pi - No Accelerometer Data Detected

The raspberry pi is always getting zeros for the Accelerometer data.
Accelerometer data. Do you know the reason ?
x: 0.0
y: 0.0
z: 0.0
Gyroscope data
x: -156.183206107
y: 0.0
z: 0.0
Temp: 43.1182352941 C

root@raspberrypi:/opt/CANBUS/GYRO/py3/6050/mpu6050/bin# sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.