An Arduino self-balancing robot: working prototype

I spent some time reading on accelerometers, gyros, sensor fusion, PID, optimized PWM motor control. I tinkered with the components I have for a while and then procrastinated for even longer. Finally, I can say that I have a prototype of a self balancing bot that shows promise.

More fine-tuning is needed, and I plan to add a Bluetooth module to get PID controller data wirelessly, possibly to adjust the PID coefficients in real time and, hopefully to steer the bot remotely someday.

I plan to post a more detailed description with the code and schematics as I make progress. Here is my components list:

Planned additions:

I will likely replace the Li-Po battery with a 6AA battery pack and possibly a voltage booster (if necessary). A couple of reasons:

  • I already have these components from my line following project
  • The extra weight of the AA batteries at the top should actually help with the balancing
  • The Li-Po battery goes back to being a spare for my “Betamax” quadcopter, as originally intended. Now that the weather is better in Vancouver, I plan to get some more flight practice!

This article has 29 comments

  1. Paul Reply

    Hi I found your site via Youtube whilst searching ‘Arduino balancing robot’.

    I am trying to build a robot like yours, I have had trouble sourcing rotary encoders so I would prefer to build a robot without them.

    I have the mpu6050, the arduino, the batterys and the L298N motor shield.

    The robot is made out of lego and is just needing programming ! Could I have a look at your code please becuase I am new to arduino !! (I have used Picaxe in the past)


  2. Sharda Prasad Reply

    I am doing the same work for my semester project, but i am new to arduino and also finding hard to implement PID control in arduino. Can you please give me the code and its associated libraries for balancing the robot.

  3. Hugo Toledo Reply

    Thanks for validating my expectation that there was code and successful implementation of a self- balancing robot using the Invensense MPU-6050. I appreciate the video and links you provided.

  4. Fede Reply

    congratulations for the great work. Magnificent!
    Also I’m trying to build a self balancing robot; but I’m having a lot of problems, please, could you send me the code for arduino be able to better understand how you put together all the sensors?
    Thank you very much


  5. Tyrone Reply

    Hello I am very interested in building a balance robot but I lack the proper skills for now. Could you please e-mail the code. Thank you

  6. Tyrone Reply

    Hello .
    Have you added the encoders ? Do you plan to update this site with your progress?
    I am really .looking forward to see what you have done.

    • Stan Reply

      Hi Tyrone, yes, I will update the site as I make progress. Unfortunately, I have not had much time and desire to get back to this and finish it over the last few weeks. One reason is that I built a frame that is too heavy for the motors I have. I do not look fortward to rebuilding the frame and re-wiring everything once again, so naturally, I procrastinate…

      Thanks for your comment though – the fact that people come to this site and find it useful is the main reason to get me to finish these projects!


      • Piyumal Reply

        Hello friend,
        your project is awesome, thank you very much. I am a university student, and as my final year project I selected to make this kind of thing. So I have no idea how to start it, could you please help me to do this.
        For my project I use MPU 6050 gyro and arduino mega board. I have no idea how to work with PID in here. can you send me the code, it is highly appreciated.
        Thank you.

        • mus'ab Reply

          Hi, my graduate project is self-balancing robot and i planned to use PID controller via arduino uno .. i have parts almost like ur own >>plz i need ur code

          this is me Email:

  7. Simon B. Reply

    Hi Stan,
    Great post! I am currently also working on a balancing robot project (using MPU-6050) and your introduction was a great help. Will get time to finish the project soon? Although my balancing robot has been able to balance on the odd occasion, it is not at all perfect yet. Do you think that using an accelerometer-gryo and a PID is enough to get the robot balanced 100% of the time, or should I get some motor encoders?

  8. Adrian Reply

    I’m doing similar project, could you please send me the code, I have some problems. I do not know what to do with imu sensor values, how to control motor pwms. Thanks 😉

  9. Tyrone Rob Reply

    Hello, Stan

    Happy New years. I was wondering if you can send me the new update code for your balance robot with the encoders through email.
    Thank you.

  10. John Paul Reply

    I like your project. would you please send me the code. I am trying to build one. thank you

  11. Jose Antonio Reply

    I am trying to build one but I’m not very well with the code, would you please send me the code too?

    Thank you

  12. farid Reply

    Hi I found your site via Youtube..
    i am trying to do the same project as yours but i am new to arduino. i already have all the component that you have stated above..could u please send to me the code and schematics for this project as soon as possible because i need to finish this project in this week. i really appreciate your kindness..

    here is my email :
    thank you very much..

  13. Charles Smith Reply

    Hi, I have a question about the reason why the batteries are place on top of the robot.
    Your reason is ‘The extra weight of the AA batteries at the top should actually help with the balancing.’, and doing that takes the center of gravity higher, right? That means if it falls or goes out of balance, the motors will have a harder time trying to get the robot back in balance again. I am working on the same project, with the same Gyro and stepper motors and I’ve placed my batteries as low as possible. Because than the center of gravity wil be much lower and it would be easier to get back in balance when the robot goes out of it.
    I am doing it wrong or are there more possibilities?
    Please respond, this is my year-project that I am working on, thank you.

  14. David Reply

    Hi Stan,

    I’ve got my self balancing robot using the same components, I’ve completed the wiring but now stuck on the coding part. Can I possibly get a copy of your code and libraries. That would be much appreciated. Great Job btw!

    my email:

  15. Am Labrie Reply

    Hello Stan. your project are awesome. I am a final year university’s student and do the same project for my final project. But i have a problem to do it. Can You please send me the code and the schematic for this project? I am really appreciate if you want to help me. Thanks. this is my email :

  16. wissam Reply

    I am doing the same work for my semester project, but i am new to arduino and also finding hard to implement PID control in arduino. Can you please give me the code and its associated libraries for balancing the robot.
     Thanks. this is my email :

  17. Dominic Reply

    I have a similar configuration (MPU6050, UNO, l298n, 5V battery) but I cant stay balanced at all. some of it is backlash in the gearboxes. Some of it is that my code is way too simple.. “get X axis from MPU….if roll is 0 wheels reverse…pause… start over… ” I don’t see where you posted the code that drives your robot so hacks like me can see your solution and draw inspiration from it.

    Any chance you could share??

  18. John Mathews Reply

    Hi, I just watched the tutorial from EEEnthusiast (Arduino Accelerometer MPU-6050 Tutorial) and was wondering if you could help me with it. I’m working on a quadcopter and wanted to use the MPU to balance the unit. Do I need to use both accelerometer and gyroscope data to do that? Do you have an example I could use for it?


  19. Chaitanya Reply

    Hi ,

    I’m new to PID control and I need it to work on my project . Can you please share your PID arduino code with me .


  20. Tuan Nguyen Anh Reply

    I am doing the same work for my semester project, but i am new to arduino and also finding hard to implement PID control in arduino. Can you please give me the code and its associated libraries for balancing the robot.
    Thanks. this is my email :

  21. Pingback: Arduino script for MPU-6050 auto-calibration – Scattering Space

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.