Introduction: Chappie- Self-Balancing Robot

Picture of Chappie- Self-Balancing Robot
After getting so much frustrated over PID tuning of quadcopter, I decided to master PID first on some basic project. Self-balancing robot seems an idle choice. Since it’s not new and still challenging, I decided to go for it.
I never thought it will be easy and I am not disappointed. I learned a lot from it and decided to share it with others.

EDIT - Some people were getting NaN values in their PID readings.The reason was EEPROM use. PID values were loaded from the EEPROM on initialisation. If values haven't already been stored in EEPROM,it will return NaN. People who don't need EEPROM can find a separate version in GITHUB.

Step 1: Concept/Theory

What is Self-balancing robot?

Self-Balance Robot is somewhat which balances itself and automatically corrects its position on disturbance.

How it works?

It will be prevented from falling by giving acceleration to the wheels according to its inclination from the vertical. If the bot gets tilts by an angle, than in the frame of the wheels, the center of mass of the bot will experience a pseudo force which will apply a torque opposite to the direction of tilt.

That’s the technical answer. If you want to know in simple terms, recall the stick balancing game which you used to play in your childhood.Yeah, the one in which you balance stick in your hand. If stick falls forward, you move your hand forward and vice-versa. Now consider the stick your robot and the 2 tire’s as your hands. The same technique you used to balance stick will now be used to balance robot.

Step 2: Materials

Picture of Materials

First we need some materials to build our chassis. I choose Acrylic sheet. Some screws will be needed according to design which we will discuss shortly plus 2 tires.

For remaining Electronics part, here’s the list

1x Arduino UNO

1x HC-05 Bluetooth Module

1x MPU 6050

1x L293D

1x IC7805

1x Battery

2x 500 rpm Motors

2x Motor clamps with screws

Some jumper wires and breadboard/PCB

We will talk about the choices of these components later

Step 3: Chassis

Picture of Chassis

I designed my chassis using acrylic sheet (just to save trouble of drilling holes). However, it can be designed using Wooden plank, plastic sheet etc. too. In my opinion, wooden plank will be good choice if you want to build a stable chassis and plan to mount something on it. Anyways, I cut sheet to form 3 levels/floors. It’s not mandatory, but is a good practice. No. of levels can be increased if desired. After markings, holes has to be made for screws. For making holes, I simply use Solder iron. (Here acrylic sheet comes to rescue. After making holes, I realized that I made mistakes which I corrected easily) If you are using, wood, double check your markings.

Choice of tires should be ones with good grip (rubber, if available) and large surface area.

Assemble the whole chassis and we are ready to move to electronics part.

Step 4: Connections

Picture of Connections

Now why these components?

MPU-6050 is used due to its DMP capability. It can process the Raw Accelerometer & Gyro values to give the Yaw, Pitch, and Roll. It greatly decreases the computation overhead.

HC-05 Bluetooth module is used for communication between Bot and Android app.

L293d motor driver is used for controlling motor.

IC7805 voltage Regulator is used for providing 5V power supply to Arduino. However, using this IC is not necessary and battery can be directly plugged to Arduino. This is just used for Safety Purposes

A fritizing file is attached to show the connections. Connect all as shown.

Now as everything set, we can dive into the code for our robot.

Step 5: Software

Arduino is known for its friendly coding environment and large open source user libraries which makes the prototyping really easy.

I first started with separate Accelerometer and Gyroscope. But as things progressed, I find it difficult to balance with computational overhead of calculating pitch & roll (using kalman/Complementary filter).When I switched to MPU-6050, it drastically improved the balancing.

To use the DMP of MPU, I used Jeff Rowberg’sI2CDev libraries. To calibrate the mpu, I used Luis Rodenas’ssketch.

Now, we can determine the angle of bot with vertical. But how can we calculate the amount of force needed to push bot back to vertical when it is unbalanced by some uncertain degrees from vertical. This is where PID kicks in.PID will specify this force which will change according to time and angle by which bot is unbalanced.

I will not go into technical details. Brett Beauregard wrote a nice PID library with explanation if you want.

To further speed up the motor response, I used digitalIOPerformance library for fast writing of pins.

Finally for storing PID values in EEPROM of Arduino, EEPROMex library for efficiency and clean code.

Complete code can be found on the GITHUB.

Step 6: APP

Picture of APP

Re-uploading code every time to change PID values is very tiresome. So I decided to attach Bluetooth and change values from my Android phone. I saw many people using MIT App Inventor for building apps for such purposes. I never used it before. So I thought, I must give it a shot. After watching some tutorials and some apps from other people regarding Bluetooth connection, I was all set. I must admit,App inventor revolutionize the app creation by letting you make apps in a way even non-programmers can make it without concerning what is happening behind the scenes.

MIT app inventor was fun except I have to deal large graphical blocks even for simpler tasks. Arrangements of these blocks was tiresome and reading it was pain.

You can find my app and Sample code from here.

Note-Although Multi-scene was introduced in App inventor 2, it treats every scene as separate app.For ex-you started some Bluetooth activity in scene1 and paired a device and then switched to scene2.scene2 will be completely unaware of the the pairing and tries to pair (& obviously fails) a device. A good way of handling it is build a single scene app and hide/show elements just like single page websites.

Step 7: PID Tuning

Frankly, this is the most difficult part of the process and also the most crucial. You will spend many days working on it and yelling “WTH am I supposed to do?” How much frustrating it may be but there is no escaping it? Control engineers set the PID parameters by experience. It’s an art learnt through experience.

There are various methods out there for PID tuning.

The best simple & easy method for PID tuning is-

  • · Set I and D term to 0, and adjust P so that the robot starts to oscillate (move back and forth) about the balance position. P should be large enough for the robot to move but not too large otherwise the movement would not be smooth.
  • · With P set, increase I so that the robot accelerates faster when off balance. With P and I properly tuned, the robot should be able to self-balance for at least a few seconds.
  • · Finally, increase D so that the robot would move about its balanced position more gentle, and there shouldn’t be any significant overshoots.
  • · If first attempt doesn’t give the satisfying results, reset PID values and start over again with different value of P.
  • · Repeat the steps until you find a certain PID value which gives the satisfactory results.
  • · A fine tuning can be done to further increase the performance of PID system.
  • · In fine tuning, PID values are restricted to neighboring values and effects are observed in practical situations.

Important Points

  • · There is no clear boundary for taking P, I or D values and is mostly taken based on experience.
  • · Theoretically, ID values depends on the state of the system.Ex. Mechanical Structure, Physical properties, Electrical properties (if any) etc.
  • · But practically, it also depends on the external conditions.Ex. Atmospheric conditions etc.
  • · PID values and method of choosing PID values depends largely on the characteristics of system. A method producing good result for a system may not work at all for another system with different characteristics.

What P, I & D values means practically?

In case of Self-Balancing Robot-

  • P-P determines the force with which the robot will correct itself. A lower P shows robot’s inability to balance itself and a higher P will shows the violent behavior.
  • I-I determines the response time of robot for correcting itself. Higher the P, Faster it will response.
  • D- D determines the sensitivity of robot to the error in its state. It is used to smoothen/depress the robot oscillations. A lower D is unable to remove oscillations and a higher D will cause violent vibrations.

Depending on your PID tuning, the bot will be able balance itself now.

Congratulation!!! On your first self-balancing robot. Maybe you can name it now, I called mine Chappie.

Step 8: What’s Next?

Well, now you have a self-balancing robot which you can control by Bluetooth. Maybe you can upgrade it to Wi-Fi or use it as replacement to your 4 wheel robot.

You can upgrade the Arduino to Raspberry pi to harness the power of mini-computer e.g. Image processing etc.

You can also use decoder motor or stepper will also significantly increases the performance. However, you will need to update the code accordingly.


manpreetsingh80 (author)2016-03-31


digitalIOPerformance library doesn't work on Arduino 1.6. Use Arduino 1.5 for compiling the program.

Sir plz guide regarding the version of Arduino 1.5 as there are various other alternatives. I downloaded 1.5.8. will it work if no then plz specify your version too

masoodr1 (author)2017-11-11

hello ...please send your code for me/??


Kevalrsavla (author)2016-04-02

Thanks bro your code work perfectly for me and i got a A+ in my project

VarunK11 (author)Kevalrsavla2017-10-20

Did you use Arduino Uno

h-craft (author)2017-01-13

thanx a lot...manpreet bro....i ve i found that the motor connection to l293d is wrong in the ckt diagram....secondly your app is not I did the PID tuning by connecting to pc....every time I give a command through the app it shows "broken pipe"....I hope you would reply soon...once again thanx a lot!

vivekduneja (author)h-craft2017-09-05

Hello sir plz solve my problem give me the correct circuit diagram as well as plz send me your code too.

My e-mail address is

vivekduneja (author)h-craft2017-08-28

Please tell the mistake in the circuit diagram as i am also facing the same problem and my dc motors are not moving at all.

Thanks in advance

Bro can u send me the correct circuit diagram. I have made the connections. In serial port I a getting Nan as output and the motor driver IC is not driving the motors. Plz help me out. I have a deadline to complete this. Thank u

hii dheeraj prabhuswamy can you please send me the correct circuit diagram and code. its urgent, i hope you revert thanks in advance

Ankit06 (author)h-craft2017-04-08

Can u please send me the correct circuit diagram to I am working on same project

John PaulP5 (author)h-craft2017-01-26

Can you send mo the codes? Thanks dude.

6000214082 (author)2017-07-17

i have a lipo battery of 11.1v, so instead of using 9v battery can i use that? will the voltage regulator be able to subtend the voltage and be able to produce the required output voltage? please let me know and thanks in advance...

ounkazmi (author)2017-05-16

bro can u send me full coding of your project ???????plz

aporizga (author)2017-04-08

nice project

plese send me code

John PaulP5 (author)2017-01-26

Nice project. Can you send me the codes for this? thanks!

souad95 (author)2017-01-09

can you please send me the code ...

AbhishekB113 (author)2017-01-07

can you plz send me the code ...

TarunK65 (author)2017-01-02


I am getting an error in the code that "MPU6050 does not name a type"

please email me to

MikeH433 (author)2016-12-23

Dude. Nice code.


martincoetzee (author)2016-11-28

Love the name did you name him after the movie? One of my favourite movies.


SapnilG (author)2016-11-08


i have done the circuit as given above. can you please send me the arduino program for arduino 1.5 and also for arduino 1.6

can you please send it at the earliest.....

thank you

SapnilG (author)SapnilG2016-11-08

my email

please don't send the link, send the whole program............................

sk_upadhyay (author)2016-11-05

Hi Manpreet,

I am using your code without EEPROM. The wheel seems to be moving in only one direction no matter what I do. I tried to calibrate by starting with a value of p and i and d to zero. The wheel still keeps on moving in same direction. oscillation is not happening. Am I missing something here ? Will be helpful if you can give some direction.

Email if


Randomgui (author)2016-09-24


Thanks for the tutorial!

I used your code (SelfBalance_robot0_66_withoutEEPROM) on the Arduino IDE 1.5.8, with the MPU 6050 calibrated and in the horizontal, and the engines started work forward and didn't stop, i needed shut off the Arduino for the process stop. Then i used the app RC Blue, and i hadn't sucess to control the engines.

I'm using the all the conections which you put in your project, and i don't using any type of structure for test the project, all the pieces are on the my table in the horizontal. What i can do to do the system work the right way?? And i need use the sample code of RC Blue (which is in folder of rc blue) to work the right away??

I apologize for the written in english, i have a low level in the this language...but i hope you understand the problem which happened.

ctheadn8954 (author)2016-09-22


Love your work!

Can you explain more about how to code the robot, so it can "maintain balanced while moving, or other directions". I've looked up your code, but still can not figure out the relationship between the remote direction control and the output pwm.


ctheadn8954 (author)ctheadn89542016-09-22

Is it just simply change the set point and break the balance condition, so the robot would lean on one particular side?

Yeah.just change the set point by a little bit. eg. if you change yaw,it will move left and right to correct it.For the balance point slightly in that direction.If its too low the bot will not move.If too big,the bot will slide too fast and may lost it balance.

Thanks a lot! What if I want to make it turn right and left?

JoãoM134 (author)2016-09-22

hi ! if i use the arduino leonardo the code will stay the same?or will i have to change it?

Madhan_2117 (author)2016-08-31

Hi bro, Thanks for the wonderful tutorial. when bot is place in normal position where for few sec its stable and suddenly there will be some garbage value running and ll come back to normal. Its hard for me to calibrate the PID. motors are running too fast, how to control them.

mithileshs3 (author)2016-05-20

All the connection are perfectly done, but its not working!!!



yuvalbir (author)2016-05-14

manpreetsingh80, thank you for a great tutorial.

I have a question about your arduino code (with EEPROM). The function save_pid seems to write to the first 3 addresses both the pid and rot values, depending on the function variable. don't they overwrite each other? I would expect addressFloat to start with sizeof(float)*3 for the rot branch.

majenko (author)2016-05-11
IC7805 voltage Regulator is used for providing 5V power supply to Arduino. However, using this IC is not necessary and battery can be directly plugged to Arduino. This is just used for Safety Purposes

Not only is it "not necessary", it's a bloody stupid thing to do. By using that you are starving the Arduino of power by under-powering it. The minimum voltage for the barrel jack input is 6V with a recommended minimum of 7V. Providing it with 5V means the internal regulator would then not be regulating and just dropping the voltage by its drop-out voltage of around a volt, so the Arduino will only be getting 4V. At 4V you are right on the lower voltage limit for running at 16MHz. Any slight variation (which there may well be since you don't precisely know the dropout of the 1117 is 1V, in fact it's nearer 1.2V) will put the Arduino into an unguaranteed state where you are running at too low a voltage for the frequency, and stability issues may well occur, making you self-balancing robot run over your pet hamster.

You only need the external regulator if you want to bypass the on-board regulator and feed the power direct into the 5V pin, which you aren't doing, and since the 1117 is marginally more modern (by about 30 years) than the ancient 7805 (which few designers actively use in current designs unless they started using it back in the dark ages), why would you want to do that? If it's for increasing current availability then you don't want a linear LDO in any case - you need a buck regulator.

Also your choice of battery tells me you don't have much practical knowledge. 9V PP3 batteries aren't suitable for powering much more than a smoke detector or small transistor radio. They lack the current and power capacity to do anything useful. Whoever first publicised using them with an Arduino needs shooting. They are the worst choice ever for doing anything that isn't highly optimized for low current consumption. At little more than 150mAh you can expect maybe an hour of use out of it - if it can even provide enough current to drive the motors under load, especially with the current hungry Bluetooth module in the mix. You really need at least 7.2V worth of AA batteries (6S NiMh are good) or a 2S Lithium-Ion battery pack.

blaze16 (author)2016-05-07

Thanks for the upload. everythings connected but the motors r working on their own. can you please help.

HarishR23 (author)2016-04-14

How much torque do the motors need to provide?

TaritD (author)2016-04-12



TaritD (author)2016-04-12



KartavyaK (author)2016-04-10

Arduino: 1.5.8 (Windows 8), Board: "Arduino Uno"

SelfBalance_robot0_66_withoutEEPROM.cpp.o: In function `setup':

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:107: undefined reference to `PID::SetMode(int)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:108: undefined reference to `PID::SetOutputLimits(double, double)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:109: undefined reference to `PID::SetSampleTime(int)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:110: undefined reference to `PID::SetMode(int)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:111: undefined reference to `PID::SetOutputLimits(double, double)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:112: undefined reference to `PID::SetSampleTime(int)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:137: undefined reference to `PID::SetTunings(double, double, double)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:138: undefined reference to `PID::SetTunings(double, double, double)'

SelfBalance_robot0_66_withoutEEPROM.cpp.o: In function `new_pid()':

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:387: undefined reference to `PID::Compute()'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:388: undefined reference to `PID::Compute()'

SelfBalance_robot0_66_withoutEEPROM.cpp.o: In function `__static_initialization_and_destruction_0':

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:84: undefined reference to `PID::PID(double*, double*, double*, double, double, double, int)'

/Arduino/SelfBalance_robot0_66_withoutEEPROM.ino:85: undefined reference to `PID::PID(double*, double*, double*, double, double, double, int)'

collect2: error: ld returned 1 exit status

Error compiling.

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

It seems like you haven't installed the PID library.Please install the library from the links provided above or from my GITHUB.

AnshuA1 (author)2016-02-01

Bro i have made all the connections as given and i have also installed your rc blue app , but after giving the power supply nothing happens . before installing the component on the chasis i thought of checking it but the wheel is not moving whatever i do.

1.there is no problem with the coding.

2.i have checked every component so no problem with components either.

3.i have made the circuit from your schematics.

please help if you can , i will be really thankful if you can give some of your precious time.

gmail -

KartavyaK (author)AnshuA12016-04-11

same problem here
if you get the solution
can you please tell me too

manpreetsingh80 (author)AnshuA12016-02-05

Do you even know what you are doing?

Have you read the whole instructable and understood the code?

sezgingul (author)2016-02-07

everything made. but Engines do not move . please help me

KartavyaK (author)sezgingul2016-04-11

same problem here
if you get the solution
can you please tell me too

IshantP (author)2016-03-30

hey bro,

i m 14 but i love robotic i had maded sevral robots and now thinking for made a robot with a self balancing feature i m also not that expert in programing i have learnt the concept of pid <thanks 2 u> but i m facing some problem regarding coading can u pls. tell me briefly about how can i upload the program in my arduino and can u also tell me from where 2 download the app <rc blue> and can u also controll its movement via bluetooth i mean can u move it in all 4 directions using ur app ????

manpreetsingh80 (author)IshantP2016-03-31

It's good to hear you have interest in robotics at such an early age.For learning Arduino,you can refer any book or tutorials on internet.One example is:-

The link to download app is given in the tutorial above and yes you can move it in all the four directions.

PranavN3 (author)2016-01-22

Not getting the right PID value.... The motors are moving in opposite direction is it correct?

manpreetsingh80 (author)PranavN32016-02-05

Try reversing the motor connections.Tell me if the problem still exist.

AnshuA1 (author)PranavN32016-02-03

how did your motor start moving?

About This Instructable




Bio: If you like my work,Please visit my Website for more awesome things
More by manpreetsingh80:Chappie- Self-Balancing Robot
Add instructable to: