Introduction: Self Balancing Robot With Arduino

In this instructable I will show you how to build a self-balancing robot with Arduino, as well as some points to be ware of.

This is how the robot works: MPU6050 module measures the angle, it serves as a feedback to the Arduino microcontroller. The microcontroller gets the difference between the desired angle and the measured angle, does some calculation (PID control algorithm), then outputs to the DC motors. A remote controller controls the movement of the robot. Two nRF24L01+ 2.4GHz modules are used for data transmission between the robot and the remote controller.

Step 1: List of Materials

Hardware Components:

I bought almost everything in Taobao, the largest online shop in China.

Remote Controller Parts:

  • One Arduino Nano microcontroller

  • One nRF24L01+ low power version module.

  • One LCD screen.

  • One joystick.

  • Four pushbuttons.

  • Five 220 ohm resistors.

  • One potentiometer.

  • One breadboard.

  • Some jumper wires.

Robot Parts:

  • One Arduino Nano microcontroller.

  • One nRF24L01+ low power version module.

  • One MPU6050 module, which has an accelerometer and a gyroscope.

  • One L293D motor driver IC.

  • Two gear motrs

  • One 12V AAA battery pack.

  • One power bank.

  • Some M3 bots and nuts.

  • Three small breadboards.

  • Some jumper wires.

Some Notes:

  • Four pushbuttons are used for PID tuning and setpoint adjustment.
  • The potentiometer is used for the adjustment of LCD's contrast.
  • The power bank is used to power the Arduino. I plan to replace it with a voltage regulator in the next version.

Software Components:

  • Arduino IDE. It's better to have the latest version, because the newest one has a tool called serial plotter. It's more comfortable to view the angle of the robot in graphic than numbers in serial monitor.
  • CorelDRAW. It's used with a laser cutter to cut the frame.
  • Sketchup. An open source software from google for 3D design.
  • The source code and schematics can be found in GitHub.

Step 2: Design the Frame

The frame of the robot consists of four parts: layers, wheels, motor brackets, some M3 nuts and bolts.

I used a Zing 6030 laser cutter and CorelDRAW software to cut the layers and wheels. The material is 3mm HDF.

The motor brackets are designed using Sketchup software, produced by an ultimaker+ 3D printer.

There are four layers in the robot. In order to get a better control of the system, mass center should be kept as high as possible, and in the middle. Battery pack is normally the heaviest part, so it should be placed on the top layer of the robot. MPU6050 MUST be place in the middle of the layer! Otherwise It would be hard to control.

Step 3: Test MPU6050

The photo above shows the angle of the MPU6050 in Arduino's serial plotter.

The robot consists of a uC, a sensor module, a motor driver chip, and a transceiver. The remote controller consists of a uC, a LCD, push buttons, a joystick and a transceiver. It's better to test them one by one, than just assemble all the parts together and it simply doesn't work.

The MPU6050 has a 3-axis accelerometer and a 3-axis gyroscope. Accelerometer measures the acceleration. Accelerometer alone can be used to measure angle while it's kept still, because the gravity, always points to the center of the earth. Gyroscope measures the angle speed. The robot's angle is calculated from the acceleration and angle speed via complementary filter.

Step 4: Test Motor

Motor should have enough torque to drive the robot. I tried with two small high speed motors. However, them failed to move the robot because the torque is not big enough. Then I bought two gear motors, which have relatively large torque: 2kg/cm on average, low speed: 200 rpm without load. The speed can be compensated by using large wheels.

This motor requires 12v. H293D is used for powering the motors. H293D is a H-Bridge. The directions of the motors are controlled by the Arduino. Arduino also controls the speed of the motors via PWM.

Step 5: Test NRF24L01+

There are mainly two versions of nRF24L01+ modules. If you use a high power version, pay attention: there are some problems with the high power version module: one is the SPI clock speed. If you use RF24 library and the high power module, replace the clock divider from SPI_CLOCK_DIV2 with SPI_CLOCK_DIV4. Another one is the power issue. Some modules are very sensitive to voltage change. Someone solved the problem by using a capacitor AND a voltage regulator. I did't verify the second issue.

Some options in the RF24 library:
To achieve the maximum range:

  • set power amplifier to RF24_PA_MAX
  • set data rate to RF24_250KBPS
  • set payload size to be as small as possible

To achieve throughput:

  • set power amplifier to RF24_PA_MAX
  • set data rate to RF24_2MBPS
  • set payload size to be <= 32 bytes

Besides, interrupt, acknowledge and CRC checksum calculation are enbaled by default.

Step 6: Assemble

After testing of each individual part, now it's time to assemble them together and make system tests.

The connections are shown above. The drawback of using breadboard is that the connections are not very good. Some parts may work well one minute ago, but then doesn't work at all due to connection problem. I plan to replace the breadboards with a printed circuit board in future.

Step 7: PID Tuning

This is the most important part. The goal is that the robot should have relatively high stability and very small overshoot.

Too large integral parameter results in instability and large overshoot; Too large derivative parameter results in very high frequency jitter.

Here’s how to tune the PID parameters:

  • Step 1: Reset all parameters. Start to increase P parameter from zero, until the robot is fast enough to balance itself in the other direction.
  • Step 2: Keep P unchanged. Increase I parameter gradually. If the robot starts to behave violently then decrease I parameter.
  • Step 3: Keep P and I unchanged. Increase D parameter gradually. If the frequency of the jitter is very high then decrease D parameter.
  • Step 4: Restart from step 1 if it doesn’t meet the goal.

Below is the source code of PID calculation.

pPart = pidValue.proportionalValue * angleErrorNew;
iPart += pidValue.integralValue * angleErrorNew; 
dPart = pidValue.derivativelValue * (angleErrorNew - angleErrorOld);

Step 8: Add Commands to Control the Robot

Robot can not only stand, but also walk. By changing the setpoint of the robot, the robot can move forward or backward. By changing the rotation of one wheel, the robot can turn clockwise or counterclockwise.


JacSjoerd made it! (author)2017-07-06

Congrats on your nice successful project!!! Segway robots are awesome!

Next challenge: put 3 wheels in triangle and put it on a ball to create your own BB-8.

penguin007 made it! (author)penguin0072017-07-06

Thanks for the suggestions. The idea is cool, but it's much more challenging. I'll focus on the two wheel robot and improve it continuously, such as use a larger wheel to increase stability, replace breadboard with a homemade PCB, add encoders for the motors.

jrbums made it! (author)2017-07-06

Cool robot. Can you upload some video of the robot balancing will moving forward or backwards? Thanks!

penguin007 made it! (author)penguin0072017-07-06

Currently it's still not robust enough with moving back and forth. I'm trying to optimize the system. As soon as the robot is stable, I'll upload a video.

Swansong made it! (author)2017-07-06

Great first instructable! The robot came out pretty well :)

penguin007 made it! (author)penguin0072017-07-06

Thanks. There are many improvements to be done. Hopefully I will update this instructable or publish a new one.

About This Instructable




More by penguin007:Self Balancing Robot With Arduino
Add instructable to: