How to Program a MINDSTORMS Robot Snake in Python


Three-segment robot Snake with 51515 LEGO Robot Inventor

When I first created the LEGO MINDSTORMS Robot Inventor Snake, I programmed it with Word Blocks (Scratch). However, a robot snake BEGS to be programmed in Python, and I couldn’t resist making a Python python. The project took me months – rather more time than I expected. And now you can get all code free with this article!

The Robot Python Snake construction

The code I’m sharing below just expects four medium motors connected, so the motor axle is on the hinge. They are ports C through F. A snake model like that is straightforward to build for yourself. However, if you want a finished, detailed model with instructions, you can buy and download my snake model. The model below is modular. You can build with one Robot Inventor set or with as many as you can buy. I have three, and I connected them all!

Running the robot python Python snake

Using these steps, you can make your Robot Inventor snake crawl forward.

  1. Build a robot snake

    You can use my robot snake building instructions or connect four motors and put wheels underneath. Connect the front-most motor to port C, the next one to D, then E and F.

  2. Install mpy-robot-tools

    To run the program, you need some extra Python libraries. I made them all free and easy to install. Copy the installer code on github and paste it inside an empty Python project in the LEGO MINDSTORMS app. Then run it once.
    If your robot is connected through USB, unplug the cable when it reboots.

  3. Run the snake script

    Next, copy the snake head script from github. You can paste it over the installer script in the LEGO MINDSTORMS App. Download it to the hub and run it. The snake should start crawling around the room now. It uses the Distance Sensor to avoid obstacles.

  4. Optional: Connect a remote control device

    If you want to take control, you can build a remote control with LEGO MINDSTORMS or use my MINDSTORMS RC Ble app for Android. If you scan and then connect, the snake stops. You can now control the movement with the virtual sticks!

Expanding the MINDSTORMS Robot Snake

Once you have the snake running, you can build more segments! I have three Robot Inventor kits, and I could use them all in one long snake. If you used my building instructions, you will be able to expand your snake too.

The program you should run on the consecutive is called If you have only one extra segment, leave the program as it is. Just copy it inside a new LEGO MINDSTORMS Python Program.

Three-segment robot Snake with 51515 LEGO Robot Inventor
Multi-segment LEGO MINDSTORMS Robot Snake

If you have more than one extra robot snake segment, be sure to change the SEGMENT = 1 on line 16 to whatever segment the code runs on.

Finally, you can incorporate the Robot Inventor color sensor in the snake. What would you do with it? Maybe you can make it follow lines. Or you can make the snake breathe fire!

See the python Python snake in action on youtube

Here’s a full video of me showing how the snake crawls using the Python code in this article. Remember to subscribe while watching the video!

If you’re interested, you can also watch the original video, where I programmed the snake using LEGO MINDSTORMS Word Blocks – a Scratch 3.0-like block programming language.

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

Leave a Reply

Item added to cart.
0 items - 0.00