Self Balancing Robot – V4

It’s been almost 4 years since I started playing with self balancing robots posting about the very first version, then the 2nd really unsuccessful one and finally a really jittery / impossible to control 3rd version

Now it’s finally time to show you something that actually works !

And even people at the PUB enjoy playing with it πŸ™‚



Go to my Github page to find the relevant code and try to run it yourself.

Also here areΒ  a couple of additional videos


Give me more details…

18 months ago, I started once more from scratch, replacing the DC motors with steppers and the controller with a Espruino Pico board as well as using a 3D printed frame:

Then did some preliminary testing:

Test electronics and motor on breadboard

After which I worked hard to install everything nicely on a protoboard, including the Espruino Pico, the MPU6050, the DRV8825 stepper drivers and the ESP8266 for an eventual future Wifi enablement:

All looking pretty good:

All set up

And here’s the schematic for reference:

Schematic done in Fritzing

And you can obviously see the Javascript code for the Espruino, on my Github account.

The only problem was that, again, it wasn’t balancing properly…

So I thought “it must be the Espruino” and its interpreted Javascript, and I wanted to play with the new ESP32 anyhow, so once again I updated the robot, and even added a wireless serial module so that I can control it remotely.

Actually I simply took the remote control system from this post.

As usual do let me know in the comments below if you found this project interesting…

11 Responses to Self Balancing Robot – V4

  1. gunfrocomGunnar says:

    I have put together this V4, finally.
    Not working that well to be honest, I just checked the video You posted with Your V3 and mine acts something like that, but it tips over after about 10-15 seconds 😦
    Using ESP32 Dev board, mpu6050, “Big easy driver” A4988 and steppers.
    The code is practicallty unchanged except for some minor detalis like MAX_SPEED.
    Dont know what to do at this point, any help is appriciated.

    Regards Gunnar

    • trandi says:

      Hi Gunnar,

      Great to hear you managed to build your own version, and believe me I understand how frustrating it can be when it doesn’t balance properly… It took me several years and 4 versions to get something roughly stable ! πŸ™‚

      Some pictures of your version would be appreciated, but in general I would say you’ll definitely have to tune the PID coefficients to match your build characteristics.
      So play with the 3 coefficients
      float BASE_Kp = 100.0, BASE_Ki = 5.0, BASE_Kd = 130.0;

      The recommended approach is to make the integral (Ki) one 0 and then tune the other 2.
      I personally start (make the other one 0 too) with the proportional one (Kp) and find a value which seems the best “compromise”. Yes, I know it’s not an exact science πŸ™‚
      Then increase the derivative one (Kd) as much as possible until just before the robot becomes jittery.

      And then Ki at the very end, it shouldn’t make much difference but just add some overall stability and avoid drift…

      Hope this helps and good luck !

      • gunfrocomGunnar says:

        Hi Dan
        Thanks for a quick reply and going to work on the 3 coefficients as soon as possible.
        Just some questions first:
        1. The step drivers, MS1,2 and 3 seems not connected in Your schematic, would suggest no microstepping, from what I gathered, is that so? I use 32x microstepping.
        2. I added the “!” in the code below, when I realized that the steppers were going the wrong way, would that influence anything? Both left and right.
        void forwardR(bool orBack) { digitalWrite(MOT_R_DIR, !orBack);

        I uploaded all to
        Some pictures will be uploaded soon.


      • trandi says:

        Hi Gunnar,

        Thanks for the pictures, nice clean design I see !

        Re your questions:
        1. I can’t remember the details to be honest. I would say with or without microstepping it should still work. Microstepping will make the movement smoother but will have less torque. You’ll have to update the PID constants anyhow but if you’re using a different microstepping mode then do also update {{#define MAX_SPEED 20000}}.
        If I remember correclty I got this 20k value empirically by playing with the driver/motor and observing at which max speed (frequency really as this is the number of steps per second I think) the motor stops turning

        2. No, it should work, as long as BOTH are going in the *same* direction every time.
        You could change the below 2 lines to use “>” probalby easier to follow the code, but it would achieve the exact same thing.
        boolean backwardL = sL < 0;
        boolean backwardR = sR < 0;

      • gunfrocomGunnar says:

        Hi Dan.
        Working with Kp, Ki and Kd and cant get anything to work any better.
        Well I notice some differences but not in the extent that it would work any better sadly.
        I uploaded a video if You got time to look at it and maybe figure something out.


      • trandi says:

        Hi Gunnar,

        Thanks for the video!
        It seems to me that the Kp, the proportional coefficient is too high and/or Kd, the derivative bit is too low in comparison…

  2. Pingback: A Robot That Can Still Keep Its Balance After A Night In The Pub

  3. Pingback: A Robot That Can Still Keep Its Balance After A Night In The Pub – High Tech Newz

  4. Pingback: A Robot That Can Still Keep Its Balance After A Night In The Pub | Hackaday

  5. MinnJohn says:

    Nice clean build! It seems more stable than most. Do you feel that the stepper motors made a big difference for stability?

    • trandi says:

      Re the steppers, it’s hard to measure, but yes I feel they do make a difference.
      Mainly to do with the slight backlash due to having to use gearing with normal brushed DC motors.
      That backlash is around the “0” position, exactly where all of the action happens on a self balancer.
      Also, the steppers allow you to ensure the same speed/distance in both wheels and hence keep the balancing straight.
      But I’ve of course seen many working self balancers using DC motors.
      Also the steppers, or at least those that I’ve used, have their own inconvenient: big / heavy and using more current….

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: