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!
Robot Snake with Multiple 51515 Inventor Sets
This 51515 Robot Snake model has a multitude of possibilities! With a single Robot Inventor, you can just let the Snake roam your living room. You’ll be amazed at how agile the Robot Snake avoids chair and table legs! With two sets of 51515 Robot Inventor, you can either double the length of the snake or build a remote control. Add more sets for even more variety and fun!
With three kits, the …
Running the robot python Python snake
Using these steps, you can make your Robot Inventor snake crawl forward.
- 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.
- 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.
- 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.
- 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 rc_multi_snake_seg_1.py. If you have only one extra segment, leave the program as it is. Just copy it inside a new LEGO MINDSTORMS Python Program.
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.