Arduino Based Self Balancing Bot

Self Balancing Bot

 Self Balancing Bot

Hey everyone what’s up!

So here’s my 3D Printed Self-balancing robot which is powered by an Arduino nano board and has an MPU6050 to get the accelerometer and gyro readings to balance itself using the PID function.

This robot setup itself is almost 3D Printed except for its big nylon wheels, I bought the wheels from the local hardware store but you can purchase them from the internet.

In this Instructables, I’m gonna show you guys how you can make your own Self-balancing bot in few easy steps with an Arduino board and MPU650.


3D Printed Parts-

  • Base x 1
  • Battery Holder x 1
  • Bottom x 1
  • Bush x 2
  • Upper-Level Part (Optional)

Other stuff-

  • Trolly Nylon wheels 7inch Dia x 2
  • Nuts and Bolts as per requirement
  • DC Gear Motor x 2
  • 12V Battery pack
  • Arduino Nano
  • Custom PCB
  • MPU6050
  • L298N Motor Driver

Step 1: Basic Structure

The First step of building this Robot was to prepare a basic body/structure.

  • I modeled the Gear DC Motors in Fusion360 and then prepared a base body around the motors which hold both Gear motors.
  • then I modeled nylon wheels and made a Bush or coupling to connect the wheel with the motor shaft.
  • the bush part that I prepared will be the same for both wheels.
  • lithium battery pack goes on top and then it is held in its place by the battery holder part.
  • For increasing the height of this robot, I added four threaded rods on the battery holder and then added yet another 3D printed part which is the upper level.

(Fusion360 file along with STL files are provided so you can edit this structure of mine according to your preference)

By adding Rods and connecting the upper-level part, the height of the robot increases and we can add a bunch of stuff to it.

In the end, I 3D Printed all the parts and then assembled the robot.

I used PLA for these but you can use ABS. I used 30% gyroid Infill for these parts.

Next is the electronics wiring!

Step 2: Electronics

Electronics At the bottom side, I mounted electronics components which include the Arduino board, MPU6050, and the L298N motor driver board.

The wiring connection of this project is basically this-

MPU6050 connection with Arduino board

  • VCC to 3.3V
  • GND to GND
  • INT to D2
  • SCL to A5
  • SDA to D4

L298N Motor Driver connection with Arduino board

  • 5V to Vin
  • GND to GND
  • ENA to D3
  • ENB to D5
  • IN1 to D10
  • IN2 to D11
  • IN3 to D6
  • IN4 to D9
  • 12V to Battery +ve
  • GND to Battery -ve

Now let’s look at the main components of this project which is the MPU6050.

the MPU is a MEMS-based 6 axis motion tracking device that has an On-chip Gyro and accelerometer sensor along with a temperature sensor. it uses an I2C interface for communication and required operating the voltage between 3.3 to 5V.

MPU6050 uses this library from adafruit –

For Connecting MPU6050 with Arduino Nano, I used a PCB from an OLD Project which was provided by JLCPCB.

I added some wires to connect the PCB with the L298N in the above-mentioned configuration.

L298N is an H bridge motor driver commonly used for making robot projects as it can drive 12V DC Motor with 2A Output Max which is enough for this small project.

Step 3: PID

PIDPID or Proportional integral derivative is basically a feedback system in which the output is controlled by readings taken and edited by the PID system.

  1. Proportional control refers to an adjustment that is proportional to how much the error is. Proportional control produces offset in its correction due to disturbances.
  2. The Integral controller has the ability to remove this offset and bring back the error to zero.
  3. Derivative control deals with the rate of change of the error. If integral control looks at the history of the error, derivative control predicts the error.

This was PID in Nutshell, now for a practical example, you can check out the PID Library for Arduino which lets you control a bunch of stuff.…

Step 4: CODE

Main Code for this project is attached above.

Before using this code, you need to download the following libraries-

  1. MPU6050 library-
  2. L motor controller library-…
  3. PID Library-…

I first installed the MPU6050 library and uploaded one of its example sketches which was a plotter sketch.

by moving the robot up/down, the graph changes, this means the MPU6050 is working properly.

Now upload the main code but before uploading, remember to change Motor pinouts according to your connection.

also, after uploading the sketch, your robot might not balance itself properly, so we have to tweak few values which are these

  • To increase the stability of the robot, KP will make the robot fall
  • KD will increase the oscillation!

by editing these values, your robot will become more stable, but by putting the wrong values, your robot might become more unstable.

anyways, just upload this code to your robot and watch the magic of MPU6050 and PID

Step 5: Result

Step 6: Further Improvements

Self Balancing Part of this robot is working properly but it still requires a lot of changes which includes getting rid of unnecessary movement in the wheel when the motor is at a stationary position, and the Heigh of the Robot can increase by adding the Upper level with threaded rods.

Read more: Arduino Based Self Balancing Bot

Leave a Comment

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