Self balancing robot first tests

I have built a self balancing robot, and here I want to post some notes regarding problems that I encountered. It was (as usual) more difficult than I anticipated, but I guess every hacker know this feeling.


Frame is made of 8mm aluminium pipes, 2 sheets of 2mm solid transparent polycarbonate, and aluminium angle bars. The pieces are screwed together with threaded rods and self locking nuts.  Making the frame was the easiest part, so I won’t write much about it.


Motors are labeled “Dagu DG02S 48:1”, and I am disappointed, because they have very low torque at low RPM, and are of low quality I think (their low price also suggests that). The consequences are, that they are unable to move my robot gently and precisely, but instead they start all of a sudden with couple of RPMs. At very low PWM duty cycle they are unable to move wheels at all, even with no load. The lowest RPM i can get out of them is probably a couple of RPMs, which sounds OK, but I think, that many of my problems arise from them being unable to gently move the wheels few millimeters forth and back. Such gentle movement is in my opinion crucial for steady (nearly stationary) operation, that you have on certain YouTube videos. So next tests (if there will be any) will be performed with steppers or geared brushless motors. Oh, and there are no encoders neither.


If motors are the brawl, electronics are the brain. Main board is STM32F4-DISCO and is connected to the battery via custom per-boards with connectors. On the top of the robot there is a single 16850 Samsung cell paired with cheap LiPo charger, and switch. I chose it, because it is a lot cheaper per mAh than those silverish ones. Battery sits inside bracket ordered on AliExpress. As of accelerometer and gyroscope, the super popular MPU 6050 does the job (also AliExpress). Motors are driven by Pololu DRV8835, and last but not least nRF24L01+ is used for connectivity with the robot, which is crucial to tune PID controller without dangling cables which would degrade stability, and make center of mass unstable. Like shooting to a moving target.

Oh, and I wanted to control the robot using cheap CX-10 remote, but It failed completely. I based my code on deviation and others work, but after many hours I gave up. Then I’ve got Syma X5HW for Christmas, and with its remote I finally had success (after first try, like 10 minutes of coding, not everyday something like this happens. But of course I only had to modify parameters for my CX-10 code). At first I confused the binding channel number (I set 0 instead of 8), and I was still able to receive data, but only from approx 5 cm apart. Then after setting it correctly to channel 8 range increased dramatically.


With electronics and motors on its place, there comes programming, the most difficult part. First I made peripherals of the robot to work (Arduino library called i2cdevlib was very helpful), so I was able to read raw data from MPU 6050, send basic commands via nRF, and spin the motors. Then, the most challenging part was to implement:

  • pitch angle calculation (,
  • fusion of gyroscope and accelerometer data (this was helpful :,
  • PID tuning (though implementing a PID is dead simple, tuning it is completely other story). Most helpful for me in this regard was probably the Wikipedia article on PID controller. In my case, the most (positive) impact came from integral part, where D part seems to have minimal influence.

I’ll try to force myself to get into detail on the topics above, as I only scratched the surface.

2 comments for “Self balancing robot first tests

  1. Robert
    October 6, 2018 at 3:03 am

    I’m impressed with your work and detailed documentation. I’m trying to build self balancing robot with almost the same components as you did, but i think of applying accelerometr and giroscope build in STM32F411E-DISCO and put this board on the top horizontally. Have you tried it before using MPU5060?
    May I see complete circuit diagram of your construction, I don’t know how connect the motor drivers and encouters.

    • admin
      October 7, 2018 at 5:15 pm

      No, I haven’t used STM32F411E-DISCO, but it has an accelerometer, gyro and magnetometer, so it’s even better than my setup. The fusion algorithm I used ( works with magnetometers which I haven’t tried, but It would be interesting. ST usually provide libraries for all the chips included on the discovery boards, so you should find it without any problems in the STM32CubeF4. In my case I found one of the libraries for your chips here : STM32Cube_FW_F4_V1.16.0/Drivers/BSP/Components/l3gd20

      Of course cube 1.16.0 is quite old, so you should check out the newest one.

      As for drivers I used something like this (not exactly, but I can’t find the board) : . There are example schematics there. I used them with brushed motors and even with stepper motor, but for steppers there are dedicated ones.

Leave a Reply

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