MPU-6050 and 08M2

Steve2381

Senior Member
Hi all

I have an MPU-6050 gyro module and I have been trying to get it working with an 08m2

I lifted the code on this thread: (seeing as I have no clue how to program it from scratch)

http://www.picaxeforum.co.uk/showthread.php?29118-3-axis-gyro-accelerometer-MPU6050-problem&highlight=MEMRAN

The MPU-6050 is on a 3.3v supply, and I believe the SDA and SCL pins should be OK with a direct connection to the Picaxe, as it does so (and works) fine on a 5v Arduino Mega.

But... nothing. I don't think the clock needs connecting, just the SDA-SDA and SCL-SCL. However, quite possibly totally wrong about all of it.

All I get are maxed out, static variables.

Any pointers?
 

hippy

Technical Support
Staff member
All I get are maxed out, static variables.
If you mean they are all reading 255 / $FF that is often an indicator that the I2C Bus isn't configured correctly. Missing pull-ups, crossed-over SDA/SCL or connected to the wrong PICAXE pins.

It could be that you have the wrong Device Address for the module but if using code which is said to work that probably has it correct.
 

Steve2381

Senior Member
As usual... 2 minutes after posting, you find the problem. That will be a 180k resistor in my 1k8 resistor tray then ;)
So, now that there is a 1k8 pull-up on the sda and scl, we have results.

However... totally useless results. I cannot make head nor tail of the pile of variables in debug. I have slowed the loop and taken averages to try and clean up the returned values, but I cannot even work out which axis is which on the module
 

Steve2381

Senior Member
OK. Using that code in the earlier thread. I have divided the resulting Xa, Ya and Za by 182 and changed to I2Cslow, and that appears to give me an approximate 360 degree result. Its possible to see a 90 degree change in direction in the variables.
But... Z axis is totally not responding. X and Y.... fine. Dodgy MPU-6050? Seems unlikely that only one axis would fail.

Unless there is something in that code wrong that I cannot see/understand, then I have no idea. I copy pasted to the editor, so there should not be any typo's.
 

Steve2381

Senior Member
Mmm. Mine is a GY-521 module. Standard one off Ebay I think with a MPU-6050 mounted on it.

This is one of those projects that is kicking my backside time wise. I thought it would be simple! Picaxes not handling negative numbers isn't helping.
Its certainly not reading the Z axis. Why that is, I have no idea. The X and Y are performing as I would expect and giving me pretty accurate 360 degree results. So unless there is something wrong in that code I lifted, I can only assume a dud module.

I am trying to write a short bit of code to track an object, but my brain just cannot get in a line with it.

Imagine a car with the Gyro mounted on it. It feeds the horizontal direction results back to a trailer that the car is pulling. As the car turns left, an arrow mounted on a servo on the trailer points at the centre rear of the car in the direction it turned.
If its a slight turn, then the servo will barely move (if at all).
But if you turned sharply, the servo would turn to its limit in the direction you turned.

My brain just cannot get it. Especially when you get to that tricky part where 359 degrees becomes 0 degrees and vise-versa
 

stan74

Senior Member
My 1st attempt. The output is signed so maybe if >32768 then it's negative. Dunno what's supposed to happen ie is it tilt or rotation or movement. w0,1,2 all settle..well repeat same numbers often and tilting it different directions changes the values and they steady in the new position but can't do much without relevant sums stuff. What is z supposed to be? Mine is ebay like the picture in the link £1.95. I'm using pull ups but says they're built in I think.
Check edmunds line following car posts
 

stan74

Senior Member
I got 4 not used pins. There's control codes in my link. It causes a pin interrupt if there's data in the buffer to read so maybe go that route from your forum link. Basically convert the arduino stuff to picaxe best you can.
 

hippy

Technical Support
Staff member
What is z supposed to be?
As best as I can tell from the datasheet with the IC flat; X = pitch, Y = roll, Z = yaw / rotation.

Placed on an outstretched hand, pitch X should change as you point your hand and fingers up and down, roll Y should change when you tilt your hand, thumb up and down, yaw Z should change as you turn your wrist left and right.

When I was playing with a Wii Nunchuck via I2C that would read the X and Y but not Z. Don't know what that used as a sensor or even if related. Seems an odd coincidence though.
 

Steve2381

Senior Member
Thats it Hippy... Yaw Z - left and right is the one not working. That does seem like an odd coincidence.
However, the Z reads fine when I last used it on an Arduino. Hmm.

My module would not read the results without the 1k8 pull-up resistors on the comms lines.
I found that dividing the results from that code by 182 gave roughly 0-360 degree results (xa, ya, za).
 

stan74

Senior Member
From the original 08m2 code link posted, z is w2. I find using the pcb vertically,pins out horizontal, rotating it changes w2.
There's a convert to degrees but the results are signed so some sums first. There's no compass in this module Steve2381
 

Steve2381

Senior Member
Its a 6 axis gyro module. What does Z read then? It never seems to change.

This is the image from the PDF
 

stan74

Senior Member
Yes z or w3 doesn't do much for what that value is worth but w5 z accel, does change then settle back to original..ish value. I thought it said this code was working in the other threads.
 

Steve2381

Senior Member
I am pretty sure this module gave me all three axis reports as that diagram when it was used on my Arduino project. I will have to dig an Arduino out over the weekend and try it on that.
To be honest, I am struggling to get the math right using the Picaxe. Most of the programming will be to overcome the maths issues. Maybe a small Arduino Pro mini is the way forward
 

stan74

Senior Member
Like a lot of interesting cheap devices on line there's no instructions but it's been sorted for rpi or arduino and people like me end up trying to read c or python and get nowhere. The mechanics is usually in a lib but you can open the lib in notepad to find more info but that's in c or python to. There's a list of controls in the link I posted that are probably set up in the lib for this device and that and the data sheet is a start. I tried searching for the device and basic but no luck yet. I had the same problem with an oled but found a 28x2 picaxe program with all the setup control codes and print vars and text at position stuff. It wasn't from the picaxe forum so I posted it so others didn't have to search. Since we have the same problem it seems, using the same code, it suggests we both have faulty units or the code doesn't work. The x,y,z values are +32767 to -32767 so if w2 is > 32767 it's positive. How do I divide a negative number in picaxe basic and floating point numbers is another problem.
 

AllyCat

Senior Member
Hi,

if w2 is > 32767 it's positive.
No it isn't, it's negative.

How do I divide a negative number in picaxe basic
Code:
symbol MINUS = 32768
symbol sign = w0     ; Only one bit is required
symbol result = w1
w2 = NUMERATOR
w3 = DIVISOR
signed_div:
    sign = w2 xor w3 and MINUS    ; Or use   / MINUS for a single bit
    if w2 >= MINUS then  : w2 = -w2 : endif
    if w3 >= MINUS then  : w3 = -w3 : endif
    result = w2 / w3
    if sign = MINUS then : result = -result : endif
return
But it's often better to "work around" the use of signed division, for example by using signed multiplication (similar to above) or a signed Trig function such as ATAN2.

Cheers, Alan.
 

Steve2381

Senior Member
and I'm lost...

Tried the module on the Arduino project I made last year..... I get all three axis variables, so something is amiss
 

stan74

Senior Member
It's been done on that platform Steve and works. I think it's one of those devices that to work needs the support of common maths and speed alas but I might be wrong. Maybe a real compass and some ingenuity for your application? That's, no disrespect, part of picaxe. It's sometimes not plug and play. Like all this stuff is either pi or dweenio in that you just use a lib and you're off. It's down to do you buy it to use it or buy it to get frustrated. Most of this works ok with picaxe. http://www.dx.com/p/arduno-37-in-1-sensor-module-kit-black-142834?tc=GBP&gclid=CjwKEAjwtbPGBRDhoLaqn6HknWsSJABR-o5sh8dRrV_QZH-9HIM1TJY5RprIZPm0PVm7nloc5RPFxhoCm3bw_wcB#.WM2KDdKLRkg
 

Steve2381

Senior Member
Eh? I have no idea where this thread has gone. Sometimes I just totally regret asking. I figured out the issue. Apologies for wasting valuable forum time
 

westaust55

Moderator
Steve,

Great to read that you have figured it out.

Yes, sometimes, and seemingly more frequent these days, There are posts that are off-topic or obscure which can be distracting to the flow of information when reading though the threads.

No, you are not wasting forum time. The intent of the forum is:
(A) a place for folks to ask for help no matter how easy or difficult the topic as you have done
(B) a place to post information on your working project or code that may help others at a future date.

For (A), Many here are very willing to help to the best of their ability.
For (B), this is where many do not finally do more that say they got it working without explaining exactly what the solution/fix was.

Therefore, I ask if you can please post some details on the PICAXE forum - that would ultimately be appreciated by others. Both those asking questions and those searching for information to help answer questions.

Best in the Finished Projects area - code snippets folder.
A few words and your now working code may help others who come along in future and need help with the same device.
 
Last edited:

stan74

Senior Member
Steve2381. (we'd) I'd like to hear if you got it working with picaxe. I'm sorry I couldn't (edited) help (edited) you (edited) more.
Happy trails.
can you use the stable c code to do anything more than display example values on a plate?
I can't. That's why I use/try to use basic. And use goto to annoy people.
 
Last edited:

westaust55

Moderator
Stan,
There are those who make every endeavor not to use GOTO on the basis of structured code and readability.
While I don't go out of my way to use GOTO, nor do I deliberately avoid the use.

I can understand that GOTO's jumping all around result in hard to read "spaghetti" code.
At the end of the day though the easier the code to read the more likely those trying to help can understand and help.
 

hippy

Technical Support
Staff member
Eh? I have no idea where this thread has gone. Sometimes I just totally regret asking.
Threads can sometimes drift. We do our best to keep things in check but it can sometimes be difficult determining what is helpful and what is not contributing to what an OP or other reader may be looking for.

We don't want to curtail discussion which may be useful to others so it may sometimes be necessary to skip what's not important or interesting for an individual reader to get to what is.

If anyone does feel things are ever getting out of hand, going wildly off-topic, or a thread is getting confusing and difficult to follow; drop us an email and we will see what we can do to improve the situation.

I figured out the issue. Apologies for wasting valuable forum time
I will finish off/tidy the code tomorrow Westaust and post it up
Glad to hear you have things resolved and I think everyone will be interested in knowing why the code which worked elsewhere did not immediately work with the PICAXE.
 
Top