Getting wrong values from MPU6050

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
RahulSharma
Posts: 2
Joined: Wed Dec 25, 2019 4:52 pm

Getting wrong values from MPU6050

Post by RahulSharma » Sat Jan 04, 2020 6:05 pm

Hi .. I have a NodeMCU ESP8266 board and I have programmed it as below shown code. I have pasted the output too. Please check what am I doing wrong.

I am trying to get Raw values from MPU6050 but I am getting acc values upto 4 and Gyro values near to 500. Temp is 217 deg c. Please help.I know i am not reading the registers right.but please help.

================================================================================
code
=================================================================================
import machine
import time
class MPU6050 :
def __init__(self, scl , sda , freq):
self.bus_freq = freq
self.scl = scl
self.sda = sda
self.bus = machine.I2C(scl = self.scl , sda = self.sda , freq = self.bus_freq)
self.bus_addr = self.bus.scan()
if self.bus_addr == [] :
print(' No device Found ')
else :
print('Device found at following locations(Decimal) - ',self.bus_addr)
self.My_device_addr = input('Enter your device''s address in decimal ')
PWR_MGMT_WAKE = 0
self.bus.writeto_mem(int(self.My_device_addr),107,bytearray([PWR_MGMT_WAKE]))
self.GYRO_CONFIG = int(input('Enter the scale of Gyrometer : \n FS_SEL Full Scale Range \n 0:250deg/sec,\n 1 : 500 deg/sec , \n 2: 1000 deg/sec , \n 3 : 2000 deg/sec :'))
self.ACC_CONFIG = int(input('Enter the scale of Gyrometer : \n FS_SEL Full Scale Range \n 0:2g,\n 1 : 4g , \n 2: 8g , \n 3 : 16g :'))
self.bus.writeto_mem(int(self.My_device_addr),27,bytearray([int(self.GYRO_CONFIG )*8]))
self.bus.writeto_mem(int(self.My_device_addr),28,bytearray([int(self.ACC_CONFIG)*8]))
print('The factor set for GYRO_SCALE : ', self.bus.readfrom_mem(int(self.My_device_addr),27,1))
print('The factor set for ACC_SCALE : ', self.bus.readfrom_mem(int(self.My_device_addr),28,1))
self.read_req_register = input('Enter the register whose value is to be read( 1 byte)')
print(self.bus.readfrom_mem(int(self.My_device_addr),int(self.read_req_register),1))

def __str__(self) :
return 'The device address for your MPU fixed to '+str(self.My_device_addr)
def get_data(self,reg_addr_high_byte) :
x_hbyte = self.bus.readfrom_mem(int(self.My_device_addr),reg_addr_high_byte,1)
x_lbyte = self.bus.readfrom_mem(int(self.My_device_addr),reg_addr_high_byte+1,1)
print('High bit register' ,reg_addr_high_byte,'value is',x_hbyte)
print('Low bit register' ,reg_addr_high_byte+1,'value is',x_lbyte)
x_h = x_hbyte[0] <<8 | x_lbyte[0]
return int(x_h)
def Get_Raw_Data(self):
self.GY_X = self.get_data(67)/131
self.GY_Y = self.get_data(69)/131
self.GY_Z = self.get_data(71)/131
self.ACC_X = self.get_data(59)/(16384)
self.ACC_Y = self.get_data(61)/(16384)
self.ACC_Z = self.get_data(63)/(16384)
self.T = int(self.get_data(65))/340 + 36.53

def Print_Raw_Data(self) :
print(self.GY_X)
print(self.GY_Y)
print(self.GY_Z)
print(self.ACC_X)
print(self.ACC_Y)
print(self.ACC_Z)
print(self.T)
def Print_Raw_Data_Plot(self) :
print('self.GY_X = ' ,self.GY_X)
print('self.GY_Y = ',self.GY_Y)
print('self.GY_Z = ',self.GY_Z)
print('self.ACC_X = ',self.ACC_X)
print('self.ACC_Y = ',self.ACC_Y)
print('self.ACC_Z = ',self.ACC_Z)
print('self.Temperature = ',self.T)
def Print_Acc_X(self) :
print('self.ACC_X = ',self.ACC_X)
def Print_Acc_Y(self) :
print('self.ACC_Y = ',self.ACC_Y)
def Print_Acc_Y(self) :
print('self.ACC_Z = ',self.ACC_Z)
def TCS(self) :

pass

my_device = MPU6050(scl = machine.Pin(14) , sda = machine.Pin(2) , freq = 400000)
my_device.Get_Raw_Data()
my_device.Print_Raw_Data()
print(my_device)
n = 2
while(n > 1) :
my_device.Get_Raw_Data()
my_device.Print_Raw_Data_Plot()
n-=1
time.sleep_ms(100)
================================================================
OUTPUT
================================================================
Device found at following locations(Decimal) - [104]
Enter your devices address in decimal 104
Enter the scale of Gyrometer :
FS_SEL Full Scale Range
0:250deg/sec,
1 : 500 deg/sec ,
2: 1000 deg/sec ,
3 : 2000 deg/sec :0
Enter the scale of Gyrometer :
FS_SEL Full Scale Range
0:2g,
1 : 4g ,
2: 8g ,
3 : 16g :0
The factor set for GYRO_SCALE : b'\x00'
The factor set for ACC_SCALE : b'\x00'
Enter the register whose value is to be read( 1 byte)36
b'\x00'
High bit register 67 value is b'\x01'
Low bit register 68 value is b'\xa9'
High bit register 69 value is b'\xff'
Low bit register 70 value is b'\xef'
High bit register 71 value is b'\x05'
Low bit register 72 value is b't'
High bit register 59 value is b'\xc3'
Low bit register 60 value is b' '
High bit register 61 value is b'\x05'
Low bit register 62 value is b'\xcc'
High bit register 63 value is b'\x14'
Low bit register 64 value is b'\xd4'
High bit register 65 value is b'\xf1'
Low bit register 66 value is b'0'
3.24427
500.145
10.6565
3.04883
0.0905762
0.325439
218.13
The device address for your MPU fixed to 104
High bit register 67 value is b'\xff'
Low bit register 68 value is b'#'
High bit register 69 value is b'\x01'
Low bit register 70 value is b'\xd0'
High bit register 71 value is b'\xff'
Low bit register 72 value is b'\xac'
High bit register 59 value is b'\xc9'
Low bit register 60 value is b'\xa0'
High bit register 61 value is b'\x07'
Low bit register 62 value is b'\x80'
High bit register 63 value is b'\x13'
Low bit register 64 value is b'H'
High bit register 65 value is b'\xf1'
Low bit register 66 value is b'0'
self.GY_X = 498.588
self.GY_Y = 3.54198
self.GY_Z = 499.634
self.ACC_X = 3.15039
self.ACC_Y = 0.117188
self.ACC_Z = 0.30127
self.Temperature = 218.13

User avatar
pythoncoder
Posts: 4030
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Getting wrong values from MPU6050

Post by pythoncoder » Sun Jan 05, 2020 10:31 am

I suggest you look at this repo which has drivers for MPU6050, MPU9150 and MPU9250.
Peter Hinch

RahulSharma
Posts: 2
Joined: Wed Dec 25, 2019 4:52 pm

Re: Getting wrong values from MPU6050

Post by RahulSharma » Sun Jan 05, 2020 1:10 pm

Thanks pythoncoder...will do that...and let you know

Post Reply