EV3 Balancing Robot Program in Progress

Last night I started translating my my NXT-G balancing robot program to the LEGO EV3 software, and here’s some progress. I didn’t bother building a proper robot yet, but it works! This program should also work with Gyroboy – more on that later.

I’m using the HiTechnic Gyroscope for now, but I’ll add support for the LEGO Gyroscope, and I’ll also try to make it work with the Dexter Industries dIMU.

I’ll post the code to this blog when it’s ready, so stay tuned!

About the author

46 Responses
      1. Paul

        Dear Laurence, have you had time to try out the EV3 Gyroboy with the Lego gyro sensor please? Have you managed to get it to work at all?

    1. That’s up to you, or anyone who builds it. I designed the program in such a way that others can adapt it without worrying about the balancing task that runs in the background.

      Maybe a self balancing Christmas tree, then?

  1. Brian Pugh

    We bought the Lego Mindstorms EV3 for our twin boys’ Christmas. I’ve been looking — unsuccessfully — for programs written for it and for building instructions that can be used other than the 5 that come with the kit.

    Can you advise, please?

    Many thanks!


    1. Hi Brian,

      To begin, you can build the 12 bonus projects. Three of them are featured on the back of the EV3 packaging. I designed the RAC3 TRUCK model. In the EV3 software start screen, there is a tab ‘more robots’, which is where you’ll find them.

      In addition, I’m working on a book that covers building and programming with the EV3 kit, as well as instructions for several projects you can build with the set. It will be out in June – hope you can wait until then 🙂

  2. Stephane


    I bought the giro from the education set to add it to my home set and then tried to make the same kind of simple robot to balance using it.

    While I think I am not so far from a working balancing robot, I have a problem with this LEGO part: it drifts a lot!!
    Since it’s probably the same with other sensors, I wonder how you do get the angle of the robot. The drift seams to me not constant, no drift at the beginning and it begins to drift after some movements. Can it be related to my batteries or I am completely wrong somewhere else?


  3. Tom D

    I am also very interested in seeing your code on Home » Development » Programming » EV3 Balancing Robot Program in Progress

    The math is too much for my brain so please document your logic. I am sure the anyway easy to build robot will become legendary esp with ev3 gyros thank you Laurens.

  4. steve

    i guess you are making EV3 Gyro boy and will release the code soon.
    Am i right??
    so i have a question..
    Are you going to make it with C or just EV3 software??

  5. fredrikvegar

    I cant wait for the balance code, I have made the NXT 2.0 balance by using an color sensor, but I cant do the same with the ev3, I cant wait to see the balance code

  6. Ian

    It looks like there has been some attempt to use the new EV3 Gyro to make balancing robots, according to this site below, it looks like the LEGO Ed Gyroboy program is attached in this blog along with some other solutions that seem to be much better. I haven’t opened or tried anything from the site yet. I have been wondering if investing in the HiTech Gyro was a better choice, and from reading this blog it seems that there are issues with the EV3 Gyro. Maybe Laurens has some insight on the EV3 sensor vs the HiTech sensor.


    1. Fatima

      Nice post. I was checking colntastny this blog and I’m impressed! Extremely useful info specially the last part I care for such information much. I was looking for this particular info for a very long time. Thank you and good luck.

    1. Hi Ian,

      I’m a bit behind on responding to comments due to personal circumstances (good ones – nothing to worry about). I need to manually approve posts with URL’s in them in order to prevent spam. I just went back to approve your post 🙂

      I’ve made some more progress on this robot as well. Maybe I’ll just go ahead and share my program and write the tutorial later.



      1. Lan Heng

        I am hoping that you will release your program soon so I can learn how to use the gyro sensor to balance a segway.

  7. kokani sumbuko

    Hi, Laurens

    Am so much pleased with what your doing. My question is, “How do I make the Ev3 31313 say something” like… “Am LEGO MINDSTORM READY TO SHOW YOU WHAT AM CAPABLE OF DOING, WELCOME”


  8. Eric

    Hi Laurens,
    I have tried the same thing with EV3 official Gyro sensor. It’s really hard to make it stable. But I don’t have any other gyro sensor which is compatible with EV3. Have you tried official Gyro sensor to balance?

    1. Hey Eric,

      Yeah, it works with the EV3 Gyro too. At the moment, though, all the time I have for MINDSTORMS is devoted to finishing my new EV3 book 🙂

      When it’s off to the printer in a few weeks time, I hope I can get to upload this program. Hope you can wait until then 🙂


      1. Eric

        Thanks for positive answer, Laurens. I hope that I can figure out how to make LEGO gyro working in the balance robot. It’s fun to make it work on my own without peaking your solution. 🙂

  9. Scott

    This is awesome! Very impressive demo. Very stable. I hope you are able to post the code soon. I have started writing my own ev3 balancing PID controller, but I’ve had troubles calibrating the ev3 gyro upon initialization. It looks like you use a simple averaging schema that I may try to incorporate. Good idea.

    Do you have any future plans for automating the PID tuning to account for different physical parameters? I would like to play around with a modified Ziegler–Nichols method. Your robot is already so stable, that you must have the tuning parameters very dialed in. I doubt that automated PID tuning could make it much better.

    I’ve been using the lego mindstorms home ev3 software. I would very much benefit from being about to examine logged data. Are there any ways to pull a log from the ev3 with the home edition? Will need to flash the firmware with ev3 firmware v1.03e.bin instead of firmware v1.03h.bin?

    What undertaking in underactuated robotics will you experiment with next?

    I am so excited to see the ported ev3 code! Can’t wait. Thanks for fighting the good fight!

    1. Hi Scott,

      Thanks for your comments. Yeah, feel free to exlore the NXT-G and/or RobotC programs on this blog. The EV3 program will be similar. The RobotC program may be a bit easier to read, but the NXT-G program will give you some hints to apply that code with the graphical programming language.

      For the NXT robot, I created a PC application that let me tune the controller manually while it was running, giving instant feedback. I’ve tuned this one manually by changing values and then running it each time.

      You don’t need the Education firmware or software for data logging. You can write data to a text file and process it on your computer as you like.

      1. Scott

        Hey Laurens,

        Thanks for your feedback. I have written an ev3 segway program with the mindstorms home edition software. I’ve accounted for the angular position and angular velocity of the wheels, and the angle and rate of the gyroscope in the error. I am using this term in a PID controller. There are 7 constants that need to be tuned; one for each of the metrics in the error term, and then three for the PID. Tuning has been quite difficult. As you suggested, I am logging data to a file on the ev3, pulling it to my machine, plotting the results and then adjusting the inputs. I have been unable to find a stable set of constants.

        How would you recommend I tune the 7 constants? Could you provide the constants you are using, and some sample data/values for the quantities in your controller? Here’s some sample data from my program (theta is gyro, phi is wheels):

        th, dth_dt, ph, dph_dt
        -2, -5, 10, 41.6667
        -2, -13, 7.5, -66.6667
        -2, 25, 6, -158.3333
        -2, 2, 3.5, -241.6667
        -2, 26, -0.5, -300
        -1, 53, -4, -300
        0, 49, -9.5, -325
        0, 52, -11.5, -316.6667
        1, 28, -11, -283.3333

        I’m not sure where to begin tuning. How did you start?

        In your controller, it seems like the integral is the dominate term. Can you speak to this? When do you think you’ll be able to post the code? Thanks!

        1. Thomas

          Hey very cool web site!! Man .. Beautiful .. Amazing .. I’ll bookmark your web site and take the feeds also…I am happy to find a lot of useufl information here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .

  10. Alexey

    Hi Laurens

    I wonder how you can use “HiTechnic Gyroscope” with EV3, which is declared at their website as “NXT Gyro Sensor” with no statements about EV3 compatibility.

    Also wonder why did you prefer Gyro variant but not “NXT Acceleration / Tilt Sensor” by HiTechnic, which seems to be more relevant for balancing. At least they published a video for NXT based balancing robot, which uses “NXT Acceleration / Tilt Sensor”

  11. Ethan

    Laurens could you make a gyro bot and program out of just #31313 and a ev3 gyro sensor? It would be greatly appreciated!!!

  12. German boy

    Hello Laurens, I’m German and so i can’t very good English. I have bought a book from you about lego mindstorms nxt 2.0 and it was very good! And of course i have watched many videos from you wich were very good, too. Now i have the ev3 set and the ev3-gyrosensor not the HI-tech senor like you. I have tested a familiar segway robot like you, but after one or two seconds it fall down. I don’t know if you can help me with the ev3 gyro-sensor but it were really nice, thanks a lot.

    (Sorry, when this is very bad Englisch)

  13. Ian

    Hi Laurens,

    I have been using your NXT-G version program for a while now in my class room settings, and the kids are amazed that a robot can stand on only 2 wheels and move, great way to talk about how humans balance and how robots balance, and that the robot is more like a child just learning to balance and walk and if they go fast they fall over.

    I have decided to use your program this summer with my camps, but with my own robot frame that can be used for other robot designs to show the kids how the robot frame can easily be used for other things.

    As I started getting things ready, I noticed an interesting thing when I starting setting up my Hitech gyro sensors, the calibration value it was giving me was anywhere from -3 to +3 which didn’t make sense from the last time I did this, after about an hour of trying to figure out what was going on and doing some internet research I decided to type in 593 to see what would happen, things started to work and after some trial and error I got the right number and things now work, trying to understand why the calibration number is so low.

    I am now trying to understand the program in more detail so I can easily use other sensors instead of the ultra sonic sensor, I have partially got program running with the light sensor but it is still not doing what I want it to do. I am trying to keep it inside a box that is out lined using black tape, do you have some hints, I have changed the ultra sonic sensor to a light sensor to be triggered by the black tape and have tried adjusting the times in timers for the backup and turn part of the program.

Leave a Reply


EV3 Discovery

Discover the many features of the EV3 set, and learn to build and program your own robots! Learn more

Start Building Robots


Website Maintenance

Robotsquare is currently being updated, which means that it may look a little different (and not very polished) for a while. All the content and pages should still be there, though. It should be back and fully operational soon. Thanks for your patience!