Self Balancing Arduino Robot_PID





Introduction: Self Balancing Arduino Robot_PID

Two-Wheel Self Balancing Robot using Arduino and MPU6050 . Well guys I know its not a new type invention but still many hobbyist still love to see the electronics. I will go here with the cheap parts in order to keep minimum cost for all those young inventors who want to complete this instructable !! Savvy !!

Hats Off Lets go

Sorry for my english as it isn't my tongue language so beware and bear me for some moments.

Step 1: Collecting Items (Tiresome :P)

1. Arduino UNO in USA or Genuino outside USA

2. MPU 6050 Gyroscope from ebay

3. BO geared plastic motors (150-300 RPM)

4. L293D Motor Driver

5. Wheels

6. Capacitors of ceramic type (0.01uF)(not used here)

7. A charger (Wall charger USB type)

Note - Motors should be of high torque and speed as well. Torque is required to lift the weight when the bot falls on either side. If it wont have such required force then you might have to visit hobby shop again!! for better ones.Speed is required for a faster response to any change in bot from sudden jerks.

Step 2: Frame"Work"

Without any screws or bolts I am keeping here simple.

Take a (1 X 2 cm) strip of double sided tape and stick it to the ede of motor.

Similarly we will need four strips here. Two for each motor.

Then choose distance between motors. I generally keep it between 8-9 cms.

With a rotary cutting tool cut off rule of length 6cm from beginning.

We will do here same for two rulers.

Then stick the rulers keeping symmetry to both sides of motor.

I actually did this step so kinda lack my images. Well nothing to worry I have a completed one.

Step 3: Upperframe Work

As you can see in the above video how I managed to make a cardboard frame to fix it on the motor frame. You can choose any height. However I chose around 10.5 cms.

Now on each side as in picture you have to punch out two holes on each side of frame. They must remain aligned. We are going to fix the motor driver L293D on it. I have cutted out a piece of cardboard with dimensions. It is having 3 holes. Two for motor wires and the fron one for IC and power wires of shield.

Step 4: Full Body Assemble

In the video you can see how I have assembled this bot using just some tapes and band. Remember the bot must not be too heavy at the top as we are using low quality motors. High RPM with HIGH torque is preferred like the johnsson motor. RPM will help to bring robot back to its C.O.G position if tipped by a large angle and torque will help the bot to bear the weight above itself. So try to keep the height a bit low to keep the bot stable.

Step 5: Calibration

Open Arduino IDE and find the source code from this *link*

The code involves main code with I2C ino. Rename this code to I2C.ino.

Also download the Kalman filter library from TKJ Electronics Github.

Open the first link and then click sketch the add file. Select I2C.ino and add.

Now hold your bot still perpendicular as possible and note down the angle from serial monitor. Now in the source file use that angle as limit.

For circuit Motor Shield pins would go as

INT1 = D6

INT2 = D7

INT3 = D3

INT4 = D4

SCL = A5

SDA = A4

For a person like me who hates batteries I shorted 12V with 5v terminals on motor shield and powered them with charger 5V/1.55 A output. You may follow your own power method.

Thank you guys,

Instructable managed by Ambika and Shashi Cheers

Links Updated Guys !!! Comment to let me know if doesn't work

Step 6: Updates

Updated the code

Bot can now face some pushes

Some code changes introducing a better auto callibration.

Have a look on pic.

Will update code soon after some testing.

Links Updated


  • my code doesn't requ...-Suman_Shashi

    Suman_Shashi made it!


  • Epilog Challenge 9

    Epilog Challenge 9
  • Gluten Free Challenge

    Gluten Free Challenge
  • First Time Author Contest 2018

    First Time Author Contest 2018

We have a be nice policy.
Please be positive and constructive.




Hello, my problem is that when the robot to one side turn the 2 wheels to balance, but when it falls to the other side only one wheel rotates to balance and the other wheel stays still.

What can I do to correct this?

Pd: sorry for my English.

I faced a similar situation.
Whta you did wrong is that one of the motor pins from L293d to Aruduino have been plugged wrong.
For ex. if D0 and D1 is for motor 1 but you may have plugged it as D1 and D2 (D2 hasn't been used hence remember it will remain always low)
so when D2 is low(always low) and D1 is high it rotates on one side however when it falls on other side D1 will be low. Correctly D0 will be high and D1 will be low but you have plugged D1 and D2. Since D2 is already low means both D1 and D2 are now low hence it won't rotate.

Have a look on you coding pins for motor
Happy Balacing

Thank you very much, I will try to solve it

A very nice work

I am waiting with excitement for installation


i really need your help....i have found various codes 123_ & I2C & KALMAN LIBRARIES i reallly dont understand wht are instructions for uploading me please can you email me details

Hello Could you please tell me where the part of PID contol in this code ?


D i have taken it as 0; hence not used in the code

P is pwm_on variable

I is m and m1 ; Both change at same rate 'here' by 0.7

I have applied PID concept by a different algo.

Hello, When i opened the serial monitor and i changed the serial speed to 115200 it keeps monitoring 0 values and sometimes monitors 1 0 as a two colomous , what that means ? and what should i do to get the right values of the sensor ?

thank you

updated to a more 'stable' code and baud rate is 115200.

re run and post if any problems faced

sorry i could not understand what you mean by more stable code ? thank you