GY-521/MPU6050

hippy

Technical Support
Staff member
My results are the same, does not matter how I try to read them, I cannot get over this problem where I get the same results for different angles, so something is not reporting correctly.
Forget about that issue. Just check that it works as expected for small turns to the left and right, small tilts upwards and downwards. And do it with simple code -

Code:
Symbol ReserveW0 = w0 ; b1:b0

Symbol x         = w1 ; b3:b2
Symbol x.lsb     = b2
Symbol x.msb     = b3

Symbol y         = w2 ; b5:b4
Symbol y.lsb     = b4
Symbol y.msb     = b5

Symbol z         = w3 ; b7:b6
Symbol z.lsb     = b6
Symbol z.msb     = b7

hi2csetup i2cmaster, $A6, i2cslow, i2cbyte

HI2cOut $2D, (8)

Do
  HI2cIn $3B, ( x.msb,x.lsb , y.msb,y.lsb , z.msb,z.lsb )
  SerTxd( CR, LF, "X=" ) : w0 = x : Gosub Show
  SerTxd( TAB,    "Y=" ) : w0 = y : Gosub Show
  SerTxd( TAB,    "Z=" ) : w0 = z : Gosub Show
Loop

Show:
  If w0 >= $8000 Then
    SerTxd( "-" )
    w0 = -w0
  End If
  SerTxd( #w0 )
  Return
 
Last edited:

Buzby

Senior Member
Hi Steve,

If I remember correctly ( too busy to check ) your device has both accelerometers and gyros.

To do the 'Angle relative to Earth' stuff you only need the accels, not the gyros.

Run that code I posted, plot some graphs ( export to Excel, or live-stream to Serialplot ), and you'll soon see patterns as you tilt the chip.

Once you have that we can work on the maths.

Cheers,

Buzby
 

Steve2381

Senior Member
I never use the Terminal function... all I get is 7x scrolling variables. What am I missing?
Yes - its a 6 DOF. I realise the Gyro data is basically the amount the unit turns from on point to the next. Been reading up!
 

hippy

Technical Support
Staff member
I never use the Terminal function... all I get is 7x scrolling variables. What am I missing?
Not sure it should be 7 variables but a scrolling list of numbers is what you should be seeing for both my and the previous suggested code. You can add PAUSE commands to slow down the rate of the numbers being shown. Or you can simply power off the PICAXE and examine the latest numbers shown.
 

Steve2381

Senior Member
Not had a chance to sit down at this yet.... I just saw your X,Y and Z text and assumed it was supposed to be showing the text X, Y and Z.
I will have a go shortly
 

Steve2381

Senior Member
OK. It doesn't show any the of the text in that program in Terminal - at any Baud.. It scrolls a pile of meaningless variables that do not change or appear to have anything to do with the Gyro.
Now the programming problem has returned and it refuses to see Com port 5 again. If fact, the editor has now totally frozen. Ctrl+alt+del time again.

Code:
Symbol ReserveW0 = w0 ; b1:b0

Symbol x         = w1 ; b3:b2
Symbol x.lsb     = b2
Symbol x.msb     = b3

Symbol y         = w2 ; b5:b4
Symbol y.lsb     = b4
Symbol y.msb     = b5

Symbol z         = w3 ; b7:b6
Symbol z.lsb     = b6
Symbol z.msb     = b7

hi2csetup i2cmaster, $D0, i2cslow, i2cbyte

'HI2cOut $2D, (8)

Do
  HI2cIn $3B, ( x.msb,x.lsb , y.msb,y.lsb , z.msb,z.lsb )
  SerTxd( CR, LF, "X=" ) : w0 = x : Gosub Show
  SerTxd( TAB,    "Y=" ) : w0 = y : Gosub Show
  SerTxd( TAB,    "Z=" ) : w0 = z : Gosub Show
Loop

Show:
  If w0 >= $8000 Then
    SerTxd( "-" )
    w0 = -w0
  End If
  SerTxd( #w0 )
  Return
Think its time I gave up on Mr Picaxe and made my life easier.
 

Buzby

Senior Member
Here is a short video showing the effect on the X and Y accelerometers when the device is tilted +/- 90' on each axis.

When the board is level both traces are at zero.

The board is first tilted 90' left, so the blue trace (rotation around Y axis) drops to -2047. The board is then returned to level, then to 90' right, and the blue trace moves to +2047. The board is then returned to level.

After that, the board is tilted forward and backward 90' each way (rotation around the X axis) and the red trace shows a similiar effect.

These effects could be used to build a spirit level, or a balancing robot, or a 'ball in maze' game, and lots of other ideas.

If the board is tilted more than 90' around the X or Y axis ( not in video ) the results are ambiguous, as each accelerometer is now sensing the same gravitational pull as before, but at 180' 'out of phase'.

The solution is to read the third accelerometer ( rotation around the Z axis ) which can then be used to deduce which 'quadrant' the board is in, which allows the full 360' spherical orientation of the board to be calculated.

I can't think of an application for this, but there must be some.

Now that I have got the chip working, I can't think of anything I want to build with it, so it's going back in the bits box !.

Cheers,

Buzby

 

Steve2381

Senior Member
Thanks Buzby. I have not given up on the Picaxe, but for the purposes of my project (which was work related)... I could not 'waste' any more time trying to get the Picaxe to play ball.
I ended up using an Arduino Mini Pro, which worked within 10 minutes of loading the MPU6050 library. It gives me full 360 degree feedback in all directions and is very accurate.
I just wanted to use a Picaxe, as the basic principle amount of code required for its intended use is fairly simple.
Sometimes I think you just need to use the right tools for the job and save yourself the headache.

Many thanks Buzby for your time and help. In my off time, I will pursue this further and see if I can get it to work. I have managed to 'lift' the maths out of the Arduino library.
 

hippy

Technical Support
Staff member
I just wanted to use a Picaxe, as the basic principle amount of code required for its intended use is fairly simple.
Sometimes I think you just need to use the right tools for the job and save yourself the headache.
The PICAXE should, as you note, be as equally capable of doing the job as the Arduino or anything else is. It is after all just reading three words of data and processing those. Others have achieved that with a PICAXE.

There may be some maths which is more difficult with the PICAXE but we never got near that, never really got out of the starting gate, due to issues with your serial port, lack of familiarity of using it with a PICAXE, and issues with two's complement numbers.

Once the pressure is off you will hopefully be able to come back to it on a PICAXE and will get it working with that.
 
Top