Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:11 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MPU-6050 accelerometer

Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:20pm 15 Oct 2015
Copy link to clipboard 
Print this post

Back in April MOBI mentioned the MPU-6050 but I put in in the todo pile for future reference.

I wanted an inclinometer to monitor the vents in my hothouses so I ordered a couple of modules and had a play.

Talking to the module was straight forward and converting 'g' readings to angle is not too difficult. As with all trig, allowing for zero and 90 degrees is a must.


' TassyJim
' 16 October 2015
' MPU-6050 gyro/accelerometer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
OPTION EXPLICIT
DIM MPU = &h68 ' MPU-6050 address
DIM temp, accelX, accelY, accelZ, gyroX, gyroY, gyroZ, tilt, accelM
DIM gyro(14)
PRINT CHR$(27)+"[2J" ' VT100 CLS
I2C OPEN 100,0

I2C WRITE MPU,0,2,&h6B,0 'wake up the MPU-6050
IF MM.I2C = 0 THEN
DO
I2C WRITE MPU,1,1,&h3B 'Reset memory pointer.
I2C READ MPU,0,14,gyro(1) 'Read 14 bytes
accelX=sint16(gyro(1)*256+gyro(2))
accelY=sint16(gyro(3)*256+gyro(4))
accelZ=sint16(gyro(5)*256+gyro(6))
temp=sint16(gyro(7)*256+gyro(8))/340+36.53
gyroX=sint16(gyro(9)*256+gyro(10))
gyroY=sint16(gyro(11)*256+gyro(12))
gyroZ=sint16(gyro(13)*256+gyro(14))

accelM=SQR(accelX*accelX+accelZ*accelZ)

IF accelM=0 THEN ' avoid divde by zero error
IF IF accelY<0 THEN
tilt=-90
ELSE
tilt=90
ENDIF
ELSEIF accelZ<0 THEN
IF accelY<0 THEN
tilt= -180 - DEG(ATN(accelY/accelM))
ELSE
tilt= 180 - DEG(ATN(accelY/accelM))
ENDIF
ELSE
tilt = DEG(ATN(accelY/accelM))
ENDIF

PRINT CHR$(27)+"[1;1H",TIME$
PRINT "ACCEL_XOUT ",accelX," "
PRINT "ACCEL_YOUT ",accelY," "
PRINT "ACCEL_ZOUT ",accelZ," "
PRINT "TEMP_OUT ",STR$(temp,3,1)," "
PRINT "GYRO_XOUT ",gyroX," "
PRINT "GYRO_YOUT ",gyroY," "
PRINT "GYRO_ZOUT ",gyroZ," "
PRINT
PRINT STR$(tilt,4,1)," Degrees"

PAUSE 1000
LOOP
ELSE
PRINT "I2C error ",MM.I2C
ENDIF
END

FUNCTION sint16(x) ' convert to signed 16 bit number
IF x>32767 THEN
sint16 = x - 65536
ELSE
sint16 = x
ENDIF
END FUNCTION


The module works in signed 16bit values so I had to convert them to floats with the function sint16().

Jim

Edit: Fixed the code for getting the quadrants right - goofed while tidying up.Edited by TassyJim 2015-10-17
VK7JH
MMedit
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:36am 16 Oct 2015
Copy link to clipboard 
Print this post

Hi TJ, this is great. I will have to give it a try. One for the library!!! Thanks.

I just notice that this thing also has temperature capabilities. I'll have to look into this.Edited by viscomjim 2015-10-17
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 11:35am 16 Oct 2015
Copy link to clipboard 
Print this post

Hi,

I found it to be a useful little device, although I didn't play with it further than as a simple inclinometer (spirit level).

It is quite easy to use - the hardest part was reading and understanding the data sheet.
David M.
 
centrex

Guru

Joined: 13/11/2011
Location: Australia
Posts: 320
Posted: 03:40pm 16 Oct 2015
Copy link to clipboard 
Print this post

Hi TassyJim
Small error, you have two if statements on the same line here

IF accelM=0 THEN ' avoid divde by zero error
IF IF accelY<0 THEN
tilt=-90
ELSE

removing the extra if I do not get any readings other than 36.5 for temperature and a line that says 90.
None of which appear to be correct.

Regards
Cliff
Cliff
 
VK2MCT
Senior Member

Joined: 30/03/2012
Location: Australia
Posts: 120
Posted: 04:45pm 16 Oct 2015
Copy link to clipboard 
Print this post

I hadn't heard of this device before and I'm thinking it might be useful for measuring the level in tanks. Maybe a float on a lever. Measure the angle of the lever for indication of fluid depth.

John B
 
centrex

Guru

Joined: 13/11/2011
Location: Australia
Posts: 320
Posted: 04:59pm 16 Oct 2015
Copy link to clipboard 
Print this post

All working doesnt like having an rtc plugged in at the same time.
But it doesnt show from 1 to 360 is this possible .
Other than that all is good.
Cliff
Cliff
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:21pm 16 Oct 2015
Copy link to clipboard 
Print this post

  centrex said  
Small error, you have two if statements on the same line here

IF accelM=0 THEN ' avoid divde by zero error
IF IF accelY<0 THEN
tilt=-90
ELSE

Cliff


Thanks.
I should stop 'tidying up' code without testing it afterwards.
I started with only using two axis and decided to use the third one for greater accuracy. I should have left it alone.

The chance of having accelM=0 is very slim so the faulty code would not run very often, but still needs to be fixed.

I chose +/- 180 degrees but you could add 180 to get 0 to 360.
It also depends on where you want zero to be.

The orientation of the module might also require different axis to be used.

I am surprised that the unit doesn't play nice with the RTC connected. Perhaps the pullups end up being too low when in parallel.

@John,
It could be attached to a float arm. I will have some on hothouse vents which will tilt over 20 degrees or so from closed to fully open.

For my tank levels, I use ultrasonic distance devices (the waterproof ones with the sensor remote from the electronics.

Jim
VK7JH
MMedit
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 07:21pm 16 Oct 2015
Copy link to clipboard 
Print this post

Jim, Centrex
The problem with the RTC is the MPU-6050 uses the same I2C address as the DS323x RTCs. If they are on the same bus then it's a problem.
Bob
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:47pm 16 Oct 2015
Copy link to clipboard 
Print this post

  BobD said   Jim, Centrex
The problem with the RTC is the MPU-6050 uses the same I2C address as the DS323x RTCs. If they are on the same bus then it's a problem.
Bob

That's not nice.

The MPU-6050 has a pin for changing the LSB of the address so there is a solution.
Tie the 'AD0' pin to VCC and use &h69 for the address.

Jim

VK7JH
MMedit
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:51pm 16 Oct 2015
Copy link to clipboard 
Print this post

  centrex said   But it doesnt show from 1 to 360 is this possible .
Other than that all is good.
Cliff


Replace the last PRINT statement with these line:

PRINT STR$(tilt,4,1),"Degrees (+/-)"
tilt = ((tilt*10+3600) mod 3600)/10 ' MOD gives the result in integers
'if tilt = 0 then tilt = 360 ' uncomment if you want 360 instead of 0
PRINT STR$(tilt,4,1),"Degrees (0-360)"


The MOD operator gives results as integers so I had to multiply by 10 to get one decimal place. There are other ways of doing it but this is a 'oneliner'.

Jim
VK7JH
MMedit
 
darthmite

Senior Member

Joined: 20/11/2011
Location: France
Posts: 240
Posted: 07:10am 19 Oct 2015
Copy link to clipboard 
Print this post

Hi

I have this module to home and will try to integrate it directly as command/function
into the st7mite
Thing like this is a must have ... he he ...
The command will be :

mpu6050(#init) : initialise module on I2C port
mpu6050(#read) : read data from mpu6050

then one function per data like :
print mpu6050(#gx) for show x angle and so forth...

and as i see in other post , i go for make everything in background (angle , flip etc ..)

Cheers.
Edited by darthmite 2015-10-20
Theory is when we know everything but nothing work ...
Practice is when everything work but no one know why ;)
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025