Python & Scratch Programming

MINDSTORMS Programming

Master Python and Scratch programming with LEGO MINDSTORMS Robot Inventor. Build intelligent robots with sensors and motors.

51515-1
Set Number
949
Pieces
10+
Ages
5
Robots to Build

Python Tutorials

Learn to program your MINDSTORMS hub with Python

Beginner

Hello World

Your first MINDSTORMS program. Display text on the LED matrix and play sounds.

from mindstorms import MSHub
hub = MSHub()
hub.light_matrix.write("HI")
Python 10 min
Beginner

Motor Control Basics

Learn to control motors with run_for_degrees, run_for_seconds, and speed control.

from mindstorms import Motor
motor = Motor('A')
motor.run_for_degrees(360)
Python 20 min
Intermediate

Sensor Integration

Read color sensor and distance sensor values to make robot decisions.

color = color_sensor.get_color()
if color == 'red':
  motor.stop()
Python 30 min
Intermediate

Line Following Robot

Build a robot that follows a black line using proportional control (PID basics).

error = brightness - target
steering = KP * error
drive(steering)
Python 45 min
Advanced

Gyro-Based Navigation

Use the built-in gyroscope for precise turns and straight-line driving.

yaw = hub.motion_sensor.get_yaw_angle()
turn_to_angle(90)
Python 60 min
Advanced

Pybricks Firmware

Install Pybricks firmware to run the same Python on Robot Inventor and SPIKE Prime hubs.

from pybricks.pupdevices import Motor
from pybricks.parameters import Port
motor = Motor(Port.A)
motor.run_target(200, 360)
Pybricks 90 min

Featured Projects

Build these robots to practise the techniques from the tutorials above.

Autonomous Race Car

Skills: Line following, gyro steering, lap timing, speed optimization

Build a race car that navigates a track on its own. Use color sensors for line detection and the gyro for smooth cornering, then track lap times and tune for speed.

Python 2-3 hours Intermediate

Color Sorting Machine

Skills: Color detection, decision logic, automation, state machines

Create a factory-style sorter that detects LEGO brick colors and drops them into bins, coordinating a conveyor belt with several sensors.

Python 3-4 hours Intermediate

Rescue Robot

Skills: Maze navigation, object detection, gripper control, autonomous decision making

Program a robot to navigate a maze, find objects, and carry them to a goal zone. Good preparation for WRO and FIRST LEGO League challenges.

Python 4-5 hours Advanced

Web-Controlled Robot

Skills: Web Bluetooth API, JavaScript, real-time control, custom UIs

Drive your MINDSTORMS robot from a custom web page with a joystick, sliders, and buttons that talk to the hub over Bluetooth.

JavaScript 2-3 hours Advanced
Read the Web Bluetooth guide
📚

Official Resources

💬

Community & Support

🤖

Ready to Start Programming?

Get MINDSTORMS Robot Inventor and build intelligent robots

Shop MINDSTORMS