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

<p>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.</p><p>What can I do to correct this?</p><p>Pd: sorry for my English.</p>
I faced a similar situation.<br>Whta you did wrong is that one of the motor pins from L293d to Aruduino have been plugged wrong.<br>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)<br>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.<br><br>Have a look on you coding pins for motor<br>Happy Balacing
<p>Thank you very much, I will try to solve it</p>
<p>A very nice work</p><p>I am waiting with excitement for installation</p><p>Thanks</p>
<p>i really need your help....i have found various codes 123_ &amp; I2C &amp; KALMAN LIBRARIES i reallly dont understand wht are instructions for uploading me please can you email me details adityabedre123@gmail.com</p>
<p>Hello Could you please tell me where the part of PID contol in this code ?</p><p>Thanks</p>
<p>D i have taken it as 0; hence not used in the code</p><p>P is pwm_on variable</p><p>I is m and m1 ; Both change at same rate 'here' by 0.7</p><p>I have applied PID concept by a different algo. </p>
<p>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 ?</p><p>thank you </p>
<p>updated to a more 'stable' code and baud rate is 115200.</p><p>re run and post if any problems faced</p>
<p>sorry i could not understand what you mean by more stable code ? thank you</p>
<p>I have updated the code link </p>
<p>yes sir I used those links just today but still i have this problem</p>
<p>have you modified the code?</p><p>check SCA and SDA pins on arduino once //this solved my problem</p>
<p>yes it has been solved , thank you :) </p>
<p>hi..</p><p>where to connect the pins of MPU 6050</p><p>plz assuist</p>
<p>Vcc goes to 5V arduino</p><p>GND goes tio GND arduino</p><p>SDA goes to A4 arduino</p><p>SCL goes to A5 arduino</p><p>MPU should be connected at bottom of bot alligned with wheels</p>
<p>does it work with l298n?</p>
<p>Yes It Works</p><p>Best part is that L298N handles large currents to motors easily</p><p>#Ambika :)</p>
<p>I tested the program and the circuit on my table and i think it worked.Next step is to do the structure.</p><p>Do you think its better to put 3 potentiometer to change the PID Values?</p>
<p>Updated video have a look</p>
<p>my code doesn't require potentio's coz I just need to find my penpendicular angle(neither left tilt nor right tilt) and set different pwm_on values for a different angle range. All Ihave to do is adjust pwm_on values which varies with size of bot </p>
<p>where do i connect the enable pins</p>
<p>no connection is required for enable pins. I played with value of &quot;pwm_on&quot; and &quot;pwm_off&quot; and perpendicular angle to balance without even any jerks. :)Happy New Year</p>
On the halloween!
<p>Nice!It would be great if you put some background music like that of Conjuring 2 spooky one (i.e VALAK) :D</p>
<p>Since your bot is speaker enabled! ;)</p>
On the halloween xD
<p>I'm getting this error trying to upload your code.</p><p>Self_balance:130: error: unterminated #else</p><p> #ifdef RESTRICT_PITCH</p><p> ^</p><p>exit status 1</p><p>unterminated #else</p><p>Can you post the complete code please.</p>
<p>Check the new link and let me know if it works.</p><p>I hope you have imported I2C and main code together. Savvy ??</p>
<p>Ya worked flawlessly also you should edit that A5 = SCL and A4 = SDA.</p><p>One more question I am having trouble with this project </p><p><a href="https://bayesianadventures.wordpress.com/2013/10/27/balance-good-everything-good/" rel="nofollow">https://bayesianadventures.wordpress.com/2013/10/2...</a></p><p>A similar balancing robot but instead he is using Complementary filters</p>
<p>Is your bot balancing ? </p><p>What problem are you facing ?</p><p>P.S I used the same link you provided however I noticed that by using complementary I was getting less torque thus making my bot to fall to either side hence I switched to Kalman. </p><p>P.P.S Balancing can even work without Kalman or complementary!</p>
<p>I can't compile the code I used this file</p><p><a href="https://github.com/vu2aeo/Arduino/blob/master/BalanceBot2_PID_Control/BalanceBot2_PID_Control.ino" rel="nofollow">https://github.com/vu2aeo/Arduino/blob/master/BalanceBot2_PID_Control/BalanceBot2_PID_Control.ino</a></p><p>BOmotor library: <a href="https://github.com/vu2aeo/Ard-Class/blob/master/BOMotorPair/bomotorpair.h" rel="nofollow">https://github.com/vu2aeo/Ard-Class/blob/master/BOMotorPair/bomotorpair.h</a></p><p>MPU6050 library: <a href="https://github.com/vu2aeo/Ard-Class/blob/master/MPU6050/MPU6050.h" rel="nofollow">https://github.com/vu2aeo/Ard-Class/blob/master/MPU6050/MPU6050.h</a></p><p>Now when I try to compile them I get this error</p><p>BalanceBot2_PID_Control:46: error: 'class MPU6050' has no member named 'GetGyroReading'</p><p>float g = sensor1.GetGyroReading(1);</p><p>^</p><p>BalanceBot2_PID_Control:48: error: 'class MPU6050' has no member named 'GetAccelerometerReading'</p><p>float a = sensor1.GetAccelerometerReading(0);</p><p>^</p><p>exit status 1</p><p>'class MPU6050' has no member named 'Initialise'</p><p>What changes I have to make to the code?</p>
<p>Import correct header file</p><p>Correct MPU6050 code link is this</p><p>https://github.com/vu2aeo/Ard-Class/blob/master/MPU_6050/mpu6050.h</p>
<p>Alright the program compiled fine but I am not getting any response from the robot. In serial monitor only this line shows up.</p><p>WHO_AM_I : 68, error = 0</p><p>TX and RX on arduino are not blinking at all</p>
<p>error 0 is for successful working however it means that gyro module is working and giving you raw values but these raw values haven't been converted to something meaningful. Its just raw values. </p>
i have been working on this two with a collegue on the last year xD the self balancing robots are a litle bit hard to make, especially the tune part, but they are awesome xD<br>great job xD looks great xD
<p>Thanx :)</p><p>Amazing height !! . A Halloween balance bot. Awesome. Are those speakers on the top ? </p>
On this robot we put a smartphone to connect to the internet (3G or 4G), the sound jack its used to plug the megaphone and because of the front camera, its possible to a person control it from home, see were is the robot and speak to others trough the megaphone, just making a video call. xD
<p>Cool !! </p>
Hey... <br>I am working on a project similar to this one only in which I am struggling with the PID so if you know how to tune it perfectly or want to give some suggestions regarding this, please mail me at hmngtm@gmail.com <br>It would be a great help.. <br><br>Seeking for ur response..
<p>First of all try to balance ur bot on carpet or or some soft surface. If it does you are near about.</p><p>First set P,I,D to zero then select a P value like 2,3 or 4 whatever. Your bot will either move to fastly to overcome the tip or it will fall on the tipping side. if former is your case increase P else decrease P.</p><p>Once your bot balances even for few seconds Increase I slowly. Afterwards it should balance for a minute or so. Then to remove jerks increase D. If found the magic value great else start with a new P.</p><p>However I have worked in a different way here. My bot perpendicular is 0 deg. So for -25 to +25 deg I send 15ms pulse to motor. I angle goes beyond +25 or less than -25 then 55ms pulse for faster comeback. However after a certain angle the motor stops so it doesn't runaway. I play with the pulses to get a stable way. At zero motors are disconnected. Hence my bot finds 0 closely and stop there without any movement. Enjoy </p>
Great <br>Thanks for the reply... <br>And can you suggest me some alternative to tune this, I mean MATLAB or anything else??
<p>Finding accurate PID is tough. I even thought to hammer my bot cause of frustation Regards #Ambika :)</p>
<p>Cool robot design. Two wheel balancing robots are always more impressive.</p>
<p>Thanks :)</p>

About This Instructable



More by Suman_Shashi:Self Balancing Arduino Robot_PID  
Add instructable to: