ckkkkk
Posts: 11
Joined: Fri Apr 26, 2019 4:39 pm

How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 10:46 am

Code: Select all

import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# MPU-6000 address, 0x68(104)
# Select gyroscope configuration register, 0x1B(27)
#		0x18(24)	Full scale range = 2000 dps
bus.write_byte_data(0x68, 0x1B, 0x18)
# MPU-6000 address, 0x68(104)
# Select accelerometer configuration register, 0x1C(28)
#		0x18(24)	Full scale range = +/-16g
bus.write_byte_data(0x68, 0x1C, 0x18)

# MPU-6000 address, 0x68(104)
# Select power management register1, 0x6B(107)
#		0x01(01)	PLL with xGyro reference
bus.write_byte_data(0x68, 0x6B, 0x01)
I wish to convert the measurement range from +-16g of the accelerometer to +-2g and 2000dps of the gyroscope to 250dps. Any Gurus can help me on this? This is the register map for MPU 6050 https://www.invensense.com/wp-content/u ... r-Map1.pdf

scotty101
Posts: 3684
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 1:43 pm

Why not have a look at some existing python implementations that read the MPU6050?

There is a decent one here
https://github.com/Tijndagamer/mpu6050/ ... mpu6050.py

It just divides the raw values by the appropriate amount depending on the range setting.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

ckkkkk
Posts: 11
Joined: Fri Apr 26, 2019 4:39 pm

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 1:48 pm

Hi scotty101,

Are there any manual way to change the measurement range? Because i am having another accelerometer and i am doing this to make sure that both of the accelerometer works just fine with the same measurement range.

User avatar
bensimmo
Posts: 4162
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 3:29 pm

manual method ?
its not automatic?

You set the registers to define the measurment range
you then need to 'scale' with the number each bit represents.
Last edited by bensimmo on Mon Apr 29, 2019 3:38 pm, edited 1 time in total.

ckkkkk
Posts: 11
Joined: Fri Apr 26, 2019 4:39 pm

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 3:33 pm

Hi bensimmo,
So it should be

Code: Select all

# Select gyroscope configuration register, 0x1B(27)
#		0x18(24)	Full scale range = 2000 dps
bus.write_byte_data(0x68, 0x1B, 0x18)
# MPU-6000 address, 0x68(104)
# Select accelerometer configuration register, 0x1C(28)
#		0x18(24)	Full scale range = +/-16g
bus.write_byte_data(0x68, 0x1C, 0x18)
bus.write_byte_data(0x68, 0x1C, 0x00) for +-2g?
bus.write_byte_data(0x68, 0x1C, 0x01) for +-4g?
bus.write_byte_data(0x68, 0x1C, 0x02) for +-8g?
bus.write_byte_data(0x68, 0x1C, 0x03) for +-16g?
Same goes to the gyro? Like 0x00 = 250 dps, 0x01 = 500 dps and so on

User avatar
bensimmo
Posts: 4162
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 3:39 pm

it is whatever the data sheet says, looking at it in front of me.
but the python link posted has it all done for you
Last edited by bensimmo on Mon Apr 29, 2019 3:52 pm, edited 1 time in total.

ckkkkk
Posts: 11
Joined: Fri Apr 26, 2019 4:39 pm

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 3:44 pm

But still, did i changed the bit correctly?

User avatar
bensimmo
Posts: 4162
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 3:45 pm

ckkkkk wrote:
Mon Apr 29, 2019 3:33 pm
Hi bensimmo,
So it should be

Code: Select all

# Select gyroscope configuration register, 0x1B(27)
#		0x18(24)	Full scale range = 2000 dps
bus.write_byte_data(0x68, 0x1B, 0x18)
# MPU-6000 address, 0x68(104)
# Select accelerometer configuration register, 0x1C(28)
#		0x18(24)	Full scale range = +/-16g
bus.write_byte_data(0x68, 0x1C, 0x18)
bus.write_byte_data(0x68, 0x1C, 0x00) for +-2g?
bus.write_byte_data(0x68, 0x1C, 0x01) for +-4g?
bus.write_byte_data(0x68, 0x1C, 0x02) for +-8g?
bus.write_byte_data(0x68, 0x1C, 0x03) for +-16g?
Same goes to the gyro? Like 0x00 = 250 dps, 0x01 = 500 dps and so on
no if you read the data sheet (easier said than done if you don't know what you are looking at)

Code: Select all

ACCEL_SCALE_MODIFIER_2G = 16384.0
    ACCEL_SCALE_MODIFIER_4G = 8192.0
    ACCEL_SCALE_MODIFIER_8G = 4096.0
    ACCEL_SCALE_MODIFIER_16G = 2048.0

    GYRO_SCALE_MODIFIER_250DEG = 131.0
    GYRO_SCALE_MODIFIER_500DEG = 65.5
    GYRO_SCALE_MODIFIER_1000DEG = 32.8
    GYRO_SCALE_MODIFIER_2000DEG = 16.4

    # Pre-defined ranges
    ACCEL_RANGE_2G = 0x00
    ACCEL_RANGE_4G = 0x08
    ACCEL_RANGE_8G = 0x10
    ACCEL_RANGE_16G = 0x18

    GYRO_RANGE_250DEG = 0x00
    GYRO_RANGE_500DEG = 0x08
    GYRO_RANGE_1000DEG = 0x10
    GYRO_RANGE_2000DEG = 0x18

0x00
0x08
0x10
0x18
in place of your 00 01 02 03

and the modifier is there for the LSB (basically the amount each bit represents)
so for 16g each bit has a larger gap between measurable values as it has to span a much wider range.

ckkkkk
Posts: 11
Joined: Fri Apr 26, 2019 4:39 pm

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 3:51 pm

Hi bensimmo,

I do not really understand why is it 0x00,08,10 and 18. Can you please do some explanation on this? Because the other accelerometer i got, it uses the 0x00,01,02 and 03 bit.

User avatar
bensimmo
Posts: 4162
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 4:19 pm

sorry for the delay as had to go work it out for myself, the answer was going to just be "because the code said so" :lol:

but i had a look and understand now.

on page 15 of the spec you'll see at the top it has the 'bits' that need to be sent.
now the first three are 000 as you are not testing(?) the next two are what you send (more later) and the last three are not used, so just 000

so we have
000##000

now
(for the two bits we have) it is an unsigned two bit as mentioned top of page 16
0 = 00
1 = 01
2 = 10
3 = 11
..decimal to binary..

fill in the ## and you get , for 3
00011000
and in hexadecimal that is
18
represented as 0x18
the 0x is to show the following number is hex 18

ckkkkk
Posts: 11
Joined: Fri Apr 26, 2019 4:39 pm

Re: How to Convert the measurement range of MPU6050?

Mon Apr 29, 2019 11:55 pm

Hi bensimmo,
Thank you, for clearing this up for me.
0 = 00
1 = 01
2 = 10
3 = 11
Also the 0,1,2,3 are 2g,4g,8g and 16 g respectively right?

User avatar
bensimmo
Posts: 4162
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to Convert the measurement range of MPU6050?

Tue Apr 30, 2019 10:49 am

On page15 of the document you posted, it does say that.
Have a look :-)

(I would certainly have a look, even if you don't understand it yet. If only to get you used to looking at them. That's what I do and I still don't get some of it. I tend to use python modules)

ckkkkk
Posts: 11
Joined: Fri Apr 26, 2019 4:39 pm

Re: How to Convert the measurement range of MPU6050?

Tue Apr 30, 2019 11:14 am

Hi bensimmo,
On page15 of the document you posted, it does say that.
Have a look :-)
Now i understand why the bits are like that and how to convert it. Thank you very much!!!

Return to “Automation, sensing and robotics”