Asked — Edited

2 Wheel Balancing Robot

I got a 4 - in - 1 sensor and have put together a 2 wheeled balancing robot. Now I need to figure out some coding. I have the Initialization script and it works good - getting lots of data. I'm not having any luck after that. Does anyone have some scripting they would be willing to share with me to help me get going? Here's what I've got so far.

ControlCommand("MPU9150", Init)


#Added these because $AccelX and $AccelY were not recognized later in the script for some reason.
$XAccel = $AccelX
$YAccel = $AccelY

if ("x" =0)


ControlCommand("MPU9150", RunOnce)

if($AccelX > 1000 and $AccelX < 1000)

print("x: " + $XAccel)
print("y: " + $YAccel)

if($AccelX > 1000)


elseif ($AccelX < 1000)

print( 1)

I am working with a code that d. cochran posted awhile ago. It will change directions when it is tipped but will never stop. Also, after it has run for 30 seconds or so it will just keep running and the only way to stop the wheels is by disconnecting the power to the EZB.


Upgrade to ARC Pro

ARC Pro is your passport to a world of endless possibilities in robot programming, waiting for you to explore.


This cannot be done with the ezb - also, the code is not as simple as change direction based on falling.

Firstly, the data from an accelerometer is noisy due to the nature of the sensor. This is why sensors combine gyro. A gyro combined with the accelerometer with a complimentary filter is the most efficient method. There are other more complicated filters, but that's usually not necessary.

Once you get the two sensors combined to return a trusted value, the next step is to define a pid filter.

The pid filter uses three parameters to calculate the power/force required to correct the error. The value inputted into a pid filter is the sensor data, which is considered the "error".

The pid calculation will provide the force necessary to correct the error.

It's also a good idea to convert the error into degrees rather than an arbitrary number returned by the sensor. That's simple atan2 math.

The values of the pid must be adjusted per application - and per battery charge, because the force of the motors over time will decrease as the batteries drain.

The ezb communication is too slow. You will need to sample the sensor data every 10ms. I guess that can be done with the /2 but not the regular ezb v4.

The way reliable balancing robots work, such as the woweee toy is because the entire platform is engineered static. Meaning you can't change it. It is what it is and that's all you will get.

The inverted pendulum that ezrobot has been struggling with is a dynamic calculation to balance nearly anything. This complicates things, you can imagine - because it's never been done before. We have been close many times but not with varying hardware.

If the Inverted pendulum included an hbridge and motors and wheels that MUST be used within specified weight and height measurements, it can be done.

Releasing a product of that nature might end up being all we can do - due to the complications of doing it dynamically per application.

If you'd like to know more, research some of the pid and filters used online. Most balance projects don't work very well. I've ordered every balancing robot kit available and none of them work - I repeat, NONE.


Thanks DJ. So much for that project! I was looking for a project to learn more about the 4-in-1 sensor and how to program it. Perhaps I'll look at something that involves regular servos instead of continuously rotating ones. Any suggestions?


I have seen two self balancing robots work at MakerFaires.

  1. husarion
  2. sociallyshaped - the guy put a bottled water on the top and it was still balancing ok.

Just curious if you own those two and had different experiences?


I don't think either of those are available to purchase - or haven't seen them.

But again, as I stated - if you get the pid values tweaked for your application, it will work. I have had plenty of balancing robots working with the inverted pendulum prototype. But tweaking the pid was the biggest pain, as it took days! And with small changes to the structure or power, it required more tweaking.

It's best to reread my original post above to fully understand the challenges of a dynamic balancing solution.

I should also add that without encoders, balancing is darn near impossible without moving in one direction or the other. This is because the algorithm has absolutely no concept of the physical position of the robot. All it knows is the force it has applied in either direction to keep it upright (ie reduce error). Because of that, without encoders the robot will just run away.


Even though DJ does not need my support I have to validate his statement that PID's are a pain in the but and take day's (or even weeks) to get (even close to) right. I've been studying this issue for a while now because it's one of the issues I've had to overcome in trying to get the motors on the very heavy human sized arms of my Lost in Space B9 robot to move smoothly. Even motor controllers with an auto tune feature will fall short on anything but the most simple of movement needs. Then if any constant changes, even a little, like the weight of the load, external forces or power supply, the tune will fall flat and your motor will be jerky at some points. You can only imagine what a jerky motor would do to a self balancing robot.

In fact there are many automated manufacturing plants around the world that depend on proper PID adjustments to run manufacturing robots where the plant operators end up taking the machines off auto and run them manually. These plants spend tens of thousands of dollars to hire contractor technicians to come in to these plants to tune the PID's just to have the operators switch back to manual later on. :(


I should add that I have not given up on a dynamic inverted pendulum module. Everything is possible given enough time


There are plenty of projects for self balancing robots or Segway clones using gyro/accelerometers and Arduino. The difference is that that the Arduino can respond very quickly where EZ-B needs to report back to ARC, the computer needs to respond. By that time, the robot has already fallen over. Also, as DJ has pointed out, each of those need to be tuned to the specific h-bridge/motors or rotation servos.

If you need to build this before EZR gets the pendulum working, I would use an Arduino and sensor for balance, and communicate drive commands to it from the EZ-B via UART. (this is 3rd on my project list that I will probably never get to. I have plans and sketches for a Segway Clone from a Robotics with Arduino book, want to add EZ-B to make it able to drive independently)



None of the arduino sketches actually work. I've gone through a year of constant research to get ideas. That's why there are terrible quality videos and absolutely ridiculously inefficient and confusing code. If you do find a video that looks like it works, it was a fluke recording that took the person all day or week to get right.

All responses from anyone with no experience on this matter are hypothetical, even if you post links to "working balancing robots". It's a far more complicated rabbit hole than you're aware. The amount of fudged/fake arduino stuff on this subject is overwhelming as well. I do highly encourage anyone of you to explore the rabbit hole as well:). It's valuable knowledge at any extent!

I have a number of self balancing kits, and they're all based on arduino - pretty much the same code as well.... they hardly work, if at all. I assure you most of the stuff out there is truly made up and faked or accidentally worked for one video. Take a look at the code and you will see how it's nearly all based on the same base, which uses one of two filters and a few variations of PID loops. The most remarkable thing you will find is all the arduino code out there contains redundant variables and some variables that are never assigned or even used. There's so many bizarre things in the code if you take a minute to look through it.

My code for the specific build i test with actually does work - but it requires specific configuration and the pid values constantly need tweaking. Also the encoders are absolutely necessary for the common sense reason I posted above.

Lastly, Alan is incorrect in that the ezb v4/2 can do it alone - but I wouldn't dedicate such a task to the controller. The slash 2 will communicate at well beyond the necessary poll rate. So will the iotiny.


DJ, as usual the Arduino can't do anything per your say. Yes you can purchase the robots I listed, as I have both. If your referring to me with no experience, I started working with PID loops on IMM in 1987, but I'm no guru on it. Given, your a smart dude and great programmer, but I do get tired of you putting down the Arduino. James Burton and Mr. Stolpner are super smart dudes also on robots/electronics and have done many balancing robots. But thats just my opinion and this an EZ-Robot forum.


I didn't put down arduino and never have and never will. I put down the code for this subject on the arduino. Please visit the arduino code examples to better understand my statement so you can view the facts yourself.

As I've previously stated in all similar conversations, arduino and mcu programming is great for repetitive tasks and monitoring. If I were to have any negative opinion of arduino than I would most surely and logically need to have the same opinion with gcc compiling on stm32 arm or ccs picc on microchip. please do not accuse me of anything not included in the current topic, specifically having a negative opinion regarding something that would contradict tools ezrobot current uses, such as arduino.

Lastly, if you have both of those robots, id love for you to share purchase locations so I can get them for research as well. It would be very useful. If you have something additional to contribute to this topic, that would be awesome - it's not a good use of my effort in this thread to discuss anything off topic.

I would prefer to focus our combined efforts on an inverted pendulum solution.


Yes I have both of these robots, do I need to take a picture and show you? As I told you above who made them, but here you go, buy one at and the other at

What I have said in my post was to help out the original topic of 2 wheel balancing robot.

Some more on the Balancing: A PID cascade should be used, one PID adjust motors speed to maintain the angle and the other PID adjusts this angle. This would also allow a new set point angle for balancing if the center of gravity is moved.


I did not ask for proof. My goal in this conversation is to identify ideas that contribute to the continued development of the inverted pendulum, as i have previously stated.

I'll check those links and get those robots to add to my research collection.


@69developer, I had not heard of those robots, the one from SociallyShaped looks interesting. It won't help me do what I was looking to do but thanks for the response.


I'm unable to find information of either robot. However, on sociallyshaped i found a balancing robot named Roby, but it's discontinued and there is no further information available on any google searches. To get over using encoders, they use stepper motors which is a good idea but not practical for universal applications. Do you have access to a codebase for either of these robots?


The Roby says backorder not discontinued.

The other is on which I said above - they have it in stock.

I do have access to the codebase, one is opensource and the other is not.

Also, I will try and show a video of balancing robot and adding an adjustable wrench hanging off the front and still balances. I have another one that is about 3 feet tall and balances well also.


I believe they balance - that's not the question. Please provide a link to the product on robot shop by using the copy and paste option provided by the Microsoft Windows Operating System from your web browser. Right click in the address bar of your web browser and select COPY and then perform the same right click function into this window and select PASTE. Thanks!


DJ, how about you go to their website and use their search. I think its about time for me to leave this forum, I get tired of seeing your smarta$$ comments. Even my 13 year old son said same thing, sad.


Apologizes for what you may have interpreted as rude - however, as stated earlier, i am unable to find the robot on robot shop website. If you wouldn't mind copying and pasting the web URL using the above instructions, it would be helpful


All you had to do was type in self balancing robot in their search box, but here is link since you could not find it. Their software is on github. What would be great is you put your software on github.

Ralph - Self Balancing Robot


Awesome! Thanks - I had a hard time finding it by the name, this helps a lot.


Thanks for the link. I also could not find it. Balance Bot had 0 hits, Balance had 2 that were unrelated. Only Balancing worked as a proper search term.

This is why many companies pay Google or Microsoft to drive their search engines. Either of them would have searched synonyms.



Could not find any videos related to this robot. confused



I type in "balance" and in drop down menu it shows self balance robot.

But I would think proper search would be "self balancing robot", my opinion.

I have only seen one video for this robot, but the robot does has a lot of documentation online.



I type in "balance" and in drop down menu it shows self balance robot.
Maybe a difference in the mobile web interface vs full interface. I was searching from my phone, and had no luck.

Interesting looking device now that I found it. A bit pricey for what it does, but this is not a cheap hobby....



@proteusy - on YouTube search for husarion robot . You should find a few videos on their stuff.

@thetechguru - not a cheap hobby at all, but it was cheaper that my main hobby of drag racing Mustangs. There are a few pretty decent self balancing robots out there.


There are a number of awesome self balancing robots out there - and they're pretty neat to see. Balancing robots are quite simple and that's not an issue with EZ-Robot's inverted pendulum, as you can see from this inverted pendulum test from last year...

However, the challenge ez-robot is taking on isn't a simple balancing robot. EZ-Robot's inverted pendulum is taking the challenge to make anything inverted balancing. Meaning a selection of motors, wheel sizes, hbridges (for differing motor sizes), voltage, and robot sizes.

The reason that above video is working with the inverted pendulum prototype is that the pid and algorithm is tuned for the voltage, hbridge, motors, wheel size, and weight.

The idea of the inverted pendulum is to provide easy options to the user to very simply fine tune an inverted pendulum balancing robot that was built within the range of hardware specifications.

The reason i'm looking at other balancing robots is because everyone does the pid and other work within the main loop a little different - and i'd like to learn what others are doing to assist with our goal. Sadly, all of the open source code that i have seen is either very inefficient or only works due to pure fluke because sometimes the PID's don't even make sense. Those with professional knowledge of programming can view the code and see unused variable declarations, re-assigning variables with nullifying previous calculations, and a pile more.

But - that doesn't mean EZ-Robot giving up on the inverted pendulum:D We've been interviewing some math professionals to grow/fix/enhance the algorithm i've been working on.


Thanks for the update DJ, the robot in your video is what I was wanting to create, but as I've said before, I had not idea of the complexity of it. Good luck with your Inverted Pendulum.


I don't get the appeal of two wheeled robots and having to use all this self balancing effort to make them work. It's a neat technical challenge, but of what practical use is it? It must be a power hog, what with having to make constant corrections to maintain balance. Just add one or two wheels or casters and away you go. It could still have the same turn radius and all. I saw the Segway as the same sort of thing. More of a gimmick than a practical solution to anything. So, what exactly is the advantage of a two wheeled self balancing robot?


Ezeobots interest in any robot technology is to provide a platform for learning. What you do with it is your own reasons:)

I think the balancing action is neat and ppl r curious about it... that's probably the only interesting part lol



So, what exactly is the advantage of a two wheeled self balancing robot?

For me it is because it looks cool, which improves the WAF (Wife Acceptance Factor). My wife has been much more supportive of my robot supplies purchases and the time I spend on them since I got a Six. Six is the most useless of my robots, but looks and moves the coolest, so she understands why I want to build them. If I could build a balance bot, it would amaze her, and I would have another year of high WAF.



For me, it's like a good challenge and the kids are interested in it also. I have some friends at CMU (PA) and I had the chance to pick their brains. R. Hollis and G. Kantor (and some others) are very talented and they put me to shame.

@DJ, just don't tap it, push it. Are you using a cascading PDI?


@DJ here some of my samples, please let me know once you view them. All three use different software and hardware. I have one of Ralph also, but ran out of time.

Yours looks like it may be using SainSmart stuff? Could be wrong.


Thanks! I really really enjoyed those videos!


I am going to be doing some acrylic laser cuts of the last one, if you interested in a set let me know and maybe we work out a deal.


That will be neat to see an acrylic version of that bot! Looking forward to it



The last one is very crafty (wood details).

@DJ, Does yours works without encoders ?

Most of them use encoders, i don't know if is a requirement to handle the balance or not.


Yes and no.

If there are no encoders, the encoder pid is disabled.


Just my opinion and I am sure a lot of effort has gone in to building and programming these (kudos), but after about 10 minutes the novelty for me is gone... I say save you battery and the need for extensive processing power and add a 3rd castor wheel.... KISS for reliability and efficiency. Practicality wise balancing bots are for show only...


They sure show well! Sure, practicality isn't the strong point of a balancing robot yet, but when we (ezrobot) gets a universal inverted pendulum module reliable... imagine the applications. We could slowly start getting increased reliability and stability out of balancing robots. Who knows, maybe one day they'll all balance on two wheels:D

Heck, humans have made it pretty far balancing on two feet!


@DJ.... Does the inverted pendulum off load some/all of the processing power in order to balance the bot? I am all for that if the ezb isn't bogged down just maintaining balance....


Yeah - that's exactly what the purpose of it is


Oh and don't forget about OldBotBuilder and his Lego EV3 / EZB4. This will be the last of me participating here. You can find me on twitter if any questions.