Ultimate 2.0 Selfbalancing robot - issues with Arduino code


Hi there, first time posting so apologies if the post isn’t great.

I’m working on a self-balancing robot from the Ultimate 2.0 kit. Robot is built according to instruction, etc. But it doesn’t quite work. Even using the Firmware for it straight from the app/robot itself, without making any code ourselves, it doesn’t stay up - just violently throws itself against the box and maybe manages to stay up for a second or so before falling over.

But, as it’s a part of the college project, we want to write our own code for it. However, I can’t quite get it working - I’m not sure if it’s issue with my P & I values for the controller or just general lack of Arduino knowledge (haven’t programmed in it at all prior to this, pretty much).

I’ve seen some useful tips here, especially from @CommandeR (thanks for all the help so far!) but it still isn’t working and I’m pretty sure it’s the code itself. I’ll attach the txt file - hope that works.

Merv_code_1.0.zip (1.2 KB)

Please let me know if anyone has ideas, we’ve been struggling with this guy for the last few weeks so we would appreciate any advice.


Hi @pineapple_wasp

I don’t have time to test it, but it looks to be a nicely written bit of code that ought to work.

Lindsay @CommandeR


thanks for getting back to me! Sadly, this code doesn’t seem to do the trick - the robot keeps falling over. Do you have any suggestions as to what could be causing this? We’ve been at it for a while now and sadly don’t have enough knowledge just yet to know where does the problem come from in order to improve it.

Is it the P & I values? Is it the equations we use?

Obviously, I’d imagine it’s very hard to say just from the info provided but this kind of seems to be a common issue some people were having so I’m wondering if perhaps there is something we’ve overlooked and the solution might be quite simple since we don’t even quite know why this keeps happening after hours of trial and error.



@pineapple_wasp I know it’s really hard to make these codes. I have one for my mBot Ranger, if I can try to adapt it to an mBot Ultimate it might help you.
Give me about a week to find my code and touch it up (I’m a bit busy right now :confused:), and if I don’t respond by then be sure to @ mention me as @Best_codes to see if I’ve made any progress.

Good luck!


@Best_codes no rush - thank you so much for looking into this! we’ll keep chipping away at it with what we can think of currently and I’ll update the thread if we come up with something.


Do remember (see my notes to @AAA earlier) that you need to provide maximum battery power …


@pineapple_wasp I haven’t found my code, so I will have to redo it. Thank you for your patience.


@CommandeR here’s what I find odd - if I use the pre-prorgammed self balancing robot in the app it drives way more power to the motors than when I code it myself. No matter what values I input it never goes as powerful as the default ones from makeblock. I couldn’t quite find an answer for it online


@Best_codes hey thanks so much for looking into this to begin with! really appreciate it, no rush


Hi @pineapple_wasp

You have clearly worked much harder on this than I ever did. The evidence for how balancing works lies in the firmware for the MegaPi board as seen in the GitHub (Makeblock Libraries) distributed version control and source code management repository - to be found in the following link:

This seems to show most of the default Makeblock settings which I guess you have studied already?: I fear that you are destined to have to accept either the default load or your own codes as they are currently.

Good luck.


@pineapple_wasp Here was my code attempt — I think it failed, I don’t have mBot Ranger assembled right now so I can’t test it.

I added the Maker's Platform extension to use the encoder motor functionality. With a lot of fine-tuning of the rotation speed and other values, you might get something to work. :confused: I wish I could help you more with this.

Good luck!

Needed: Ultimate 2.0 Self-balancing Robot Code

@CommandeR yeah been digging through this huge pile of code to find something in there, sucks that there are almost no comments and it’s bit hard to tell straight away what does what (i mean it probably isn’t that hard but never worked in Arduino before really so it takes a really long time of trial and error and seeing which methods don’t cause an error). thanks so much for all your help


@Best_codes thanks a mil! i will try this out tonight and see if it works out, i looked at the extensions but couldn’t quite find what would work so decided to leave it for the time being. thanks for all your help, i really appreciate it - will try to make the best of what we have currently


@Best_codes @CommandeR sorry guys i completely forgot to ask - would it be okay for us to credit you in the report? we have to write a technical report on our work and i want to make sure to credit whatever sources we used and both of you so far have been the most help, thanks so much


That’s fine by me.


:sweat_smile: You don’t have to credit me, but if you want to, it’s perfectly fine! :slight_smile:

For mBot Ultimate, no extension is needed — just use the encoder motor blocks (rework the logic a bit) in the Motion category:
If you don’t have encoder motors connected, your options are pretty limited, though it might still be possible.

Sorry for the confusion.


and yeah, we’ve got two encoders and been using these blocks in mblock, they’re having some issues reacting on time but i have suspicions that its PID to blame. we’ve swapped them out but it looks to still cause problems where i think the robot doesn’t react quick enough


:confused: Sorry for the delayed response here! I don’t really know how to help you if the motors are not reacting on time. Sometimes, if your code is really big, the bot will react slower, so check the code size (some codes in blocks are actually multiple lines of code in C).
Otherwise, I’d suggest contacting mBlock support. They might be able to help you, but I don’t know if mBot Ultimate is easily capable of what you need. :frowning:

I wish I could be of more help…

Good luck, @pineapple_wasp!


@Best_codes no worries, my apologies for the delayed reply here too! we kind of got it working, had to change the actual physical design of it as well. we are currently working on the technical report for the little guy and making our presentation, i will make an update on the end results and a video of the guy in action! i still cant quite explain why it works some times and not others but hopefully the lecturers will be satisfied with our work - thanks so much for all your help guys!


@pineapple_wasp No problem! I do wish I had an Ultimate, so I could help you troubleshoot more. :frowning:
Good luck!