Franko – Arduino Self Balancing Robot

FrankoMy latest Arduino DIY project is a self balancing robot.

Arduino Uno R3 microcontroller board is used for this project. MPU6050, an 6DOF IMU (with accelerometer and gyroscope) is used to get the angle and L298N motor controller board controls the 2 motors.

The motors are 12V 122rpm and the wheels are 8cm in diameter.

Robot size is 17x10x30 cm. Three plexiglas plates are used to hold the frame and parts. Standard PID controller is used to control the robot. To tune the PID controller parameters 3 potentiometers are used.

You may find the Arduino sketch and libs on my GitHub repo: https://github.com/lukagabric/Franko

Parts:
1 x Arduino Uno R3
1 x MPU6050 (IMU)
1 x L298N Motor Driver Controller
2 x 12V 122 RPM Gear Motors + 80mm Wheels
3 x 10k Potentiometer

Parts on Amazon:

If you liked this blog post I’d appreciate if you followed me on Twitter

Related Post

Remember to share...Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someone

155 thoughts on “Franko – Arduino Self Balancing Robot

      1. i see, and where did you download I2Cdev library? because i downloaded from here: http://www.i2cdevlib.com/usage ,and i can compile the sketch, but the mpu just dont show me the angle, it just say: “DMP ready! Waiting for first interrupt…”

          1. Now, i did and it works, it just have a detail, after a time, tends to fall, and not react until reset the arduino, do you have any idea?, by the way, thank you for all your help

          2. good night franko I’m having an error in the code, you can give me a hand? the error and this (LMotorController does not name the type)

      2. I want to try your code on my hardware which is very similar except motor driver, I am using L293D. Can you please help me in porting the code for L293D. I am a not much experienced do the task, sorry for asking. I will add necessary pot and try to adjust PID.

      3. help with this im need any l2cdev library

        Arduino: 1.6.9 (Windows 10), Board: “Arduino/Genuino Uno”

        C:\Users\EMIL\Documents\Arduino\balancing\porfaa\Franko.ino:3:20: fatal error: I2Cdev.h: No such file or directory

        #include “I2Cdev.h”

        ^

        compilation terminated.

        exit status 1
        Error compiling for board Arduino/Genuino Uno.

        This report would have more information with
        “Show verbose output during compilation”
        option enabled in File -> Preferences.

  1. al compilar me sale este erro que puedo hacer ?
    In file included from Franko.ino:1:
    C:\Program Files\Arduino\libraries\PID_v1/PID_v1.h:1: error: stray ‘\357’ in program
    C:\Program Files\Arduino\libraries\PID_v1/PID_v1.h:1: error: stray ‘\273’ in program
    C:\Program Files\Arduino\libraries\PID_v1/PID_v1.h:1: error: stray ‘\277’ in program

  2. Hello Franko, I find your project very interesting. I have made SBR from other website but the code is not working to well. It just falls after few seconds on hard surface, but balances forever on mattress, I don’t understand why.

    I want to try your code on my hardware which is very similar except motor driver, I am using L293D. Can you please help me in porting the code for L293D. I am a not much experienced do the task, sorry for asking. I will add necessary pot and try to adjust PID.

    I hope you will find some time to answer my query.

    Thanks in advance
    max

    1. Hi, I hace the same problem, and I use 2 Pid, for angle and acceleration…..I use a L293 driver, motors under 1Amp.

    2. Hi, I hace the same problem, and I use 2 Pid, for angle and acceleration…..in software surface works fine, but in the floor dont

    3. Hi

      Can u help me with the same project. I am also making self balancing robot.
      I just dont know how to even use an Arduino and my final project is coming near around less than one month

  3. Hey man! These weeks I could not work on the robot, but now, and it works! and now you have to say, thanks for sharing your code really helped me a lot, now I have just one question, in your code does the moveBackForth function is to control the robot with xbee usb or something? Greetings from Mexico

  4. Hey, I’m glad it works. Function moveBackForth was just to test the robot moving for a couple of seconds in one direction and then the other. So later I could add bluetooth and control it with Android device, like with the 2WD robot I made.

  5. Hi Franko,
    I am from Turkey. I’m an Air Traffic Controller in Dalaman/ TURKEY.
    I can’t speak very well. I am so sorry about that.
    Very interesting project. If it is possible, I want to use your code. But I can’t compile
    succesful.I do not have enough experince about the compiling, I try to compile your code and
    give a lot of error. Also , I try to compile your GitHub code but I have a lot of compiling
    problem. So, Can you help me please?
    I need a ino file your – Arduino Self Balancing Robot.
    if it is necessary, also send me a

    …….

    …..<LMotorController.h

    ……"I2Cdev.h" , "MPU6050_6Axis_MotionApps20.h" files.
    I hope you will help me.
    Many many thank you.
    ALP from TURKEY

  6. I am also designing a Self balancing robot but not on arduino. I am having trouble finding the set point of my robot dynamically i.e., say if the physical weight distribution of the robot changes at runtime, the CG will shift and so the set point so in this situation how to rebalance the system automatically?
    Please help me I am in a serious trouble.

  7. Hi. can u tell me the pin connition between MPU 6050 and the microcontroller. i got the library but could not get the code to upload in microcontroller. Can you plz tell me the code. thanks in advance
    rex

  8. About the questions on connecting the Arduino and IMU. Check the code comments, you need to connect the I2C interface and the interrupt pin.

    About the motors, you need a motor driver board. Check the code samples for that controller.

      1. caymanguy and luka.
        i have conecting my arduino with MPU6050 like this
        arduino MPU6050
        5V ==> 5V
        GND ==> GND
        A4 ==> SDA
        A5 ==> SCL
        2 ==> INT

        in serial COM arduino
        Initializing I2C devices…
        Testing device connections…
        MPU6050 connection successful
        Initializing DMP…
        Enabling DMP…
        Enabling interrupt detection (Arduino external interrupt 0)…
        DMP ready! Waiting for first interrupt…

        can do you help me??

  9. following errors were come at the time compilation of code

    1 ‘PID’ does not name a type
    2 ‘LMotorController’ has not been declared
    please tell me the way to correct it.
    thanks
    akash

  10. I can’t make this work. that said I’m getting all kinds of motor noise and it’s resetting the mpu and lockup on the arduino. I’m using a cheap Tamiya gear box and I’ve replaced the motors added caps and ferrite beads and dual power supplies. I don’t get this using another sketch doesn’t use the lmotorcontrol.h

  11. Ok changed the motors again and got it working. I found some ff-130sh motors in a cd-rom changer I was scrapping. these motors have the terminals straight through the back and I think have real carbon brushes. Neat project, now time to try and get Bluetooth working on the serial. Thanks for sharing.

  12. What if we do not use 3 potentiometers for for external PID tuning what if we want to have internal PID implementation in this program, is it possible …. Waiting for your reply…

  13. Hi Admin,
    I have few quick questions:
    1. Could you please share the updated link of your code with Bluetooth enabled for
    movebackandforth feature ?
    2. I was also wondering that, in general, is it possible to interface two serial interfacing
    devices with Arduino. (For example, for SBR robot already Gyro is serially
    communicating then how come Bluetooth can communicate simultaneously)

  14. we built the balancing robot successfully.But there is a small problem relate with balancing.Although It balance fine,It goes in a circle in order to keep the balance.It is like balancing with one wheel while keeping the other wheel isolate.Is there any thing to do with code?we change motor control and motors.But the problem is still there.Hope a good answer for this.Thanks.

  15. Hey Chamal,

    This is probably because of the unbalanced PWM values that are going to the motors. You can either check the duty cycle command from the controller or you can measure it directly on the drive.

    I also faced the same problem with the code that you are using. I have now rectified it. You can try using my code. Share your email address for the code.

    1. can u send me your updated code. having some problem with pwm values. still it is not balancing well, also goes in ciecular motion with one motor runs fastly than other.

    2. can u send me your updated code. having some problem with pwm values. still it is not balancing well, also goes in circular motion with one motor runs fastly than other.

  16. hello Franko! i have a problem:

    Initializing I2C devices…
    Testing device connections…
    MPU6050 connection successful
    Initializing DMP…
    Enabling DMP…
    Enabling interrupt detection (Arduino external interrupt 0)…
    DMP ready! Waiting for first interrupt…
    FIFO overflow!

    Can you help me please? thanks!

  17. hi franko,
    i made a self balancing robot with the code you shared.
    based on the mpu output wheels working fine but as chamal reported It is like balancing with one wheel while keeping the other wheel isolate.Is there any thing to do with code?
    here i am using motor with 200rpm. one motor runs slowly.
    any updation in code?
    and where to add potentionmeter.

    thanks

    1. There is a code like this. LMotorController motorController(ENA, IN1, IN2, ENB, IN3, IN4, 0.6, 1);
      You have to change the 0.6 to 1.Then it will balance perfect. 😀

    2. hai, i am from malaysia. can you share the code obtained that franko have shared since the page is no longer available.

      thank you.

  18. Hi Admin,
    I’ve connected all components together and compiled the code successfully, but the serial output always shows:

    DMP ready! Waiting for first interrupt…

    I am using Arduino Mini Pro 16Mhz and I’ve connected MPU6050 interrupt pin to Arduino Mini pro pin2/3 and tested pin3 without success. Beside connect interrupt pins to Arduino Mini Pro, do I need to do anything else?

    Can some one give me an advise here?

    Stoenz

    1. Hi Sudheesh,
      Nice work. Where do you connect your Interrupt pin? I am using Arduino Mini Pro and connect INT pin to Arduino pin2, but it didn’t work.

      Any hint for me?

      Stonez

  19. Please delete previous post for me. I would like to revise my post as below. Thanks!

    Hi Admin,
    I’ve connected all components together and compiled the code successfully, but the serial output always shows:

    Initializing I2C devices…
    Testing device connections…
    MPU6050 connection successful
    Initializing DMP…
    Enabling DMP…
    Enabling interrupt detection (Arduino external interrupt 0)…
    DMP ready! Waiting for first interrupt…

    I printed out the Packet size & Interrupt status as below:

    PacketSize: 42
    Interrupt status: 3

    I am using Arduino Mini Pro 16Mhz and I’ve connected MPU6050 interrupt pin to Arduino Mini pro pin2/3 and tested pin2/3 without success. Beside connect interrupt pins to Arduino Mini Pro, do I need to do anything else?

    Can some one give me an advise here?

    Stonez

    1. //MOTOR CONTROLLER

      int ENA = 3;
      int IN1 = 4;
      int IN2 = 8;
      int IN3 = 5;
      int IN4 = 7;
      int ENB = 6;

  20. Hey man! Where exactly did you place MPU6050 on your robot?
    I didn’t see it clearly…
    Thanks in advance!

  21. Hi Admin,

    Why 3 potentio meters for for external PID tuning is not functioning.
    The controller works well, when I tilt the motors responds forward -backward with full speed. What should I do….!!! thanks

  22. Hi,I am using your code and have set the gyro offsets according to my calculated values.But on starting,the tyres are moving back and forth as if trying to balance,but its not balancing and is only giving small jerks back and forth.What should i do? Please help me out.

  23. I’m implementing your code in my self balancing robot , it is a big scale one but I’m unable to balance it till now.
    Do I need to set the MPU6050 100% horizontal OR the code should use the current position as a default value ?
    On other meaning how can I identify the zero level for the IMU ?
    One more question please, what is the best way you used to adjust the PID ?

    1. May i get you email Ahmad Moustafa because there are some question wanna ask to you? it would be great we can contact each other.

  24. Excellent balancing robot! I uploaded your code and my robot was trying to balance yaay! I will of course have to spend time tuning the PID parameters. Also, I was looking for a great example of how to use the MPU6050 DMP instead of Kalman or Complimentary filters and your robot was exactly just that. Thanks! Now i’ll try to figure out how to implement encoders and radio control.

  25. Hello how are you? Have you been able to update you’re balance robot code? I’m a little stuck on how to move the robot forward and backwards while in balance. Where you able to add encoders? Please offer as much info as you can.
    Thank you.

  26. hi
    can you please help me with the connections of l298n motor driver with arduino because i am in a real trouble with all this please help me.

  27. hey’ i’m also doing same project i have a lot of question,i’m very glad if you answer the question of my.

    in the timer you use 1Hz for pid tuning and 5 Hz for motor. How do you set that time? can i use different value of time for that?

    originalSetpoint = 174.29
    how do you come up with that value??

    kp = map(potKp, 0, 1023, 0, 25000)
    ki = map(potKi, 0, 1023, 0, 100000)
    kd = map(potKd, 0, 1023, 0, 500)
    can you explain to me where is that value for kp,ki and kd came from??

    thanks. hope that you will answer my queries,it will help a lot to me. 🙂

  28. is anyone here knows how can i see the value of PID constants in the serial monitor while i’m adjusting the potentiometer??

  29. Ive built my first balancing robot and it balances ok but uses no motor encoders. I want to add these for better stability. Anyone any advice on how the encoder ticks is added to the motor drive.
    I understand you need a second pid loop for the motors and this is summed with the output from the pid loop from the balancing. Am I along the right lines here?

    Thanks
    John

  30. hi franco
    im living turkey in muğla. I have problems with the MPU6050. I can’t install code to arduino uno, arduino leonardo.because it doesnt approve the program code.Can you send the mpu6050 code to arduino uno or arduino leonardo it doesnt matter

  31. Hi Franko,
    I am from / TURKEY.
    I can’t speak very well. I am so sorry about that.
    Very interesting project. If it is possible, I want to use your code. But I can’t compile
    succesful.I do not have enough experince about the compiling, I try to compile your code and
    give a lot of error. Also , I try to compile your GitHub code but I have a lot of compiling
    problem. So, Can you help me please?
    I need a ino file your – Arduino Self Balancing Robot.
    if it is necessary, also send me a…………………………………………………………………………………………………………………………………………………arduino uno r3 + mpu6050 ……………..kani_48@hotmail.com

  32. Hi. Greetings from the Philippines. I tried to upload your code using Arduino UNO and MPU6050, the code compiled and uploaded successfully. No error whatsoever. Serial monitor even shows that I’m connected, I enabled LOG_INPUT and is showing readings whenever I move the robot, but there’s no movement on the motors. My motor shield which is using L298N seems to be not responding. My motor shield is working perfectly with my Obstacle Avoidance Robot and so I’m sure my motor shield is ok.

    I was wondering why my robot will just lie on the floor and won’t do anything. Please help me. Thank You

    Ariel

  33. Hi, I uploaded the code successfully but there’s no response from the motor shield. The robot just sits there. I’m sure the MPU6050 is working, I enable LOG_INPUT and Serial Monitor shows reading when I move the bot. My motorshield is also working with my Obstacle Avoidance Robot.

    My Arduino and MPU6050 and my motorshield works well with other code for controlling the motors. Only in this code. What could be the problem? Please help

    Thank you

  34. hi, I used your sketch for a robot balancing similar to yours, but I have a problem I can not settle with the three potentiometers. using a test sketch the potentiometers work. can you help me. thank you.

  35. Hi Admin.

    I finally made it working! I discovered that the pin assignment of my motor shield is different from the one stated in the code. So, I changed it according to the pin connection of my motor shield. Also, battery power vs. motor power requirement is crucial. Motor needs sufficient power, as I used the Arduino’s 5V supply when I actually used 6 volts geared motor, the robot stays flat faced on the ground, when I used 7.1 volts LiPo batt, it balances.

    The physical weight distribution is also an important issue. My Arduino is placed almost 80% forward at the edge of the lower part chassis. Terribly imbalanced at one glance. The code used 174.xx as setpoint.

    The setpoint is also an important thing to experiment with. When I changed it to 180, my SBR rocks violently but when I placed my Arduino and the battery almost at the center of the chassis and frame, the robot balances perfectly.

    Making it work at first time is a real pain in the neck. Patience and experimentation is necessary.

    The “Waiting for first interrupt…” issue is not really a problem. It means the MPU is working, just change the LOG_INPUT from 0 to 1, then, it will display readings

    Thank you for your effort, Franko. I’ve been experiment with ADXL345 but can’t balance my robot. Your site is great

    1. hi, im now facing the same problem as yours before..i connect all those thing together already, however the motor is still no any response..what should i do now?thank you

  36. Hi! I’m also doing same project I have a lot of question. I’m very glad if you answer my questions.

    In the timer you use 1Hz for pid tuning and 5 Hz for motor. How do you set that time? CanIi use different value of time for that?

    originalSetpoint = 174.29
    How do you come up with that value?

    kp = map(potKp, 0, 1023, 0, 25000)
    ki = map(potKi, 0, 1023, 0, 100000)
    kd = map(potKd, 0, 1023, 0, 500)
    Can you explain to me where is that value for kp,ki and kd came from?

    Thanks a lot. I hope that you will answer my questions. If so, it will help a lot to me. 🙂

  37. Hi Franko,
    I recent built my own balacing robot, based on the “chillibasket | wired.com” project. I choosed this project, even i saw urs and look great too, because the PID setting was easier for me. At least it run now, even i still have some setting to do, to be perfect.
    I have a question for u 🙂
    I would like be able to add some distance sensor, or an IR module, able to make reaction during it alone deplacement or gestures, or being remotable easely. A bit as the robot MIP do.
    But for now, i really don’t know and understand well, which are the parameters and the value that need to be changed to make the robot moving forward when it is stable balanced !
    I hope U can give me some informations about it, maybe u have thought about it for urs, already.
    Thks in advance, for ur reply.

    Serge. From France

  38. Sir,

    One question for you: I have nearly same setup as you do, except for the motors. And I have separate power for arduino and L298N module. But, the problem is that when I power only arduino – it works great. But if I power l298n module, arduino stucks and doesn’t react on tilting. Reseting doesn’t help much, just for a few seconds. The only connections between arduino and l298n are control lines and ground wire.
    Any thoughts?
    Thanks in advence!

  39. Many thanks for posting your code, I have tested it on my set up, and all working fine,but I have to tune it. Already it is working better than my previous set up using separate Gyro and accel and Kalman filter.
    I have adapted the code to my way of thinking,I see many comments about not working etc, you have to understand the code. The motor drive library is unfamiliar, but I worked it out OK, and adjusted the parameters to balance the response of both motors,yes it went in a circle at first. I may remove the motor library,it is easy enough to code without it. The PID routine works well,again easy enough to do it without the library, but I will keep this.
    So now, it balances perfectly on carpet, and oscillates on a hard floor. The MPU I have located near the base of the robot,will try at high level.
    You need to experiment with the set point, I changed the code to use 0.0 as the balance point, as opposed to 180 ( yes I know it says 174).
    I also calibrated the MPU6050 and inserted my measured offsets.
    Now I have a basic working balancing robot, I can experiment further,next step potentiometers, then encoders, then bluetooth.
    Thanks once again.

    1. hi john..could you please send me the code and schematic for this project..actually im trying to do this project..but im new to arduino..hopefully you can send to me as soon as possible..i really appreciate your kindness..

      here is my email : faridsuhaimi90@yahoo.com

      thank you very much..

  40. hi franko
    actually i am trying to do this project..but i am new to arduino..could u please send me the code and schematic for this project..i already have all the component that have you stated above..i really appreciate if you are willing to share the code..

    here is my email : faridsuhaimi90@yahoo.com

    thank you very mush franko..

  41. Franco, thanks for sharing this fantastic project with us. I am using arduino mini pro with 2 TLE5205 bridge motor drivers. Some time ago I made a protoboard with arduino + mpu6050 + nrf24l01 to use as a fall detector. This morning that came in very handy, because I only had to add the motor drivers and change the pins on the arduino. I was pleasantly surprised when it worked right away, after loading your code. I intend using your project to do robotic training, if that is in order with you.

  42. HI…
    Can any one please guide me?
    I am using the same components, except the motor shield. Its an arduino shield, for that do I need the Lmotorcontroller library, and how can I program it for the back and forth function. Or do I have to make changes in the library?
    please help if anyone knows about it.

    Thanks

  43. i have a issue in self balancing robort . i used H-bridge ardino uno and mpu 5060
    it ‘s not working properly . when i turn on robort it wil viberate and not stand on one point ..
    please help me out from this issue .
    thank u

    1. One reason why it vibrate and can’t stand by itself is due to PID setting. Check your PID setting . There are a lot of good instructions just google it on how to set P, I, D.

      Hope this help.

  44. Hi,
    Did you set gyro mpu6050 or use default? .
    mpu.setXGyroOffset(220); mpu.setYGyroOffset(76); mpu.setZGyroOffset(-85)].

    Because i can’t change that. I must calibration by hand it so difficult.

    1. I already made this project and went perfectly, add the libraries( LMotorController.cpp and .h) to your arduino IDE library. Make sure all needed lib are included before compiling the sketch.

      Hope this will help.

      Good luck

  45. Hi Franko. Can you please send me the code and the schematic of the project? I am doing a self balancing robot and i am beginner with arduino and the code. I am really appreciate if you want to help me. thanks.
    My e-mail is adept.ncl@gmail.com

  46. Hi Franko. Can you please send me the code and the schematic of the project? I am doing a self balancing robot and i am beginner with arduino and the code. I am really appreciate if you want to help me. thanks.
    My e-mail is adept.ncl@gmail.com

  47. Hi Franco, you have used 12V 122 RPM geared motor. Is it possible to use 600 RPM geared motor and still can tune PID with potentiometers?

    BTW you don’t have RC input to control forward, backward and steering of robot?

  48. How do I sync the motors I got the kit from makers faire? trying to connect the balance sheild MTU 6050 with the arduino and balance sensor

  49. Hi Franko. Can you please send me the schematic of the project ? I am doing a self balancing robot and i am beginner with arduino and the code. I am really appreciate if you want to help me. thanks.
    My e-mail is fadlyliyas14@gmail.com

  50. Awesome code… I placed the mpu 6050 near the bot’s base and the tunings a lil bit changed….tada…it balances perfectly

    I had a doubt…I wanna control the bot’s direction via NRF24l01 module….plz help me 🙂

    1. Could you tell me pls, where did you install the potentiometer?I will be very pleased if you send the photo of your robot to my e-mail.

      Thank you 🙂

  51. Hi Franko. Can you please send me the code and the schematic of the project? I am doing a self balancing robot and i am beginner with arduino and the code. I am really appreciate if you want to help me. thanks.
    my email is : nicknguyen1993@gmail.com

  52. hi~
    I did not use any potentiometers.
    Robot wheels continuously rotate forward .
    I change robot angle but not change wheels direction.
    help me.

  53. Hi Franko. Can you please send me the code and the schematic of the project? I am a computer teacger at the technical hıgh school. I will make your project with my students.

    thanks.
    my email is : odgnc@hotmail.com

  54. very inspiring, i want to make this for m final project, but i have problem
    Can you tell how to get the value of P, I, D and how to alter the value of PID ?
    thank youu
    my mail hafidhef@gmail.com

  55. I have achieved balancing process,but how to move it forword and backward I’m getting problem with it .plz tell me.

  56. “Hi, I just watched the tutorial from EEEnthusiast (Arduino 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?

    Thanks,
    John”

  57. Hey, awesome work! I’m having a look a your code since I want to do the same thing. I have a quick question for you, hoping is not too silly.

    In your PID implementation I saw that you set the initial value of the variable “Setpoint” to 174.29. What is exactly the setpoint and why did you chose that specific value?

    Thanks!
    Matteo

  58. can someone please send me the code of the project … the code i used is showing me
    Initializing I2C devices…
    Testing device connections…
    MPU6050 connection successful
    Initializing DMP…
    Enabling DMP…
    Enabling interrupt detection (Arduino external interrupt 0)…
    DMP ready! Waiting for first interrupt…
    FIFO overflow!

    please help in trouble shooting.

  59. Hi Luka,

    1- This is your own spesific generated code or taken from another project?
    2- Which axis of rotation yo used? And what is place of IMU 6050?
    3- You used MPU 6050 library ypr angle? Why and why not used compimentary filter becasue of drift problem if you encountered before

Leave a Reply

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