Hey Habr! We already about the LEGO MINDSTORMS Education EV3 platform. The main objectives of this platform are learning by practical examples, the development of STEAM skills and the formation of engineering thinking. It can be used for laboratory work on the study of mechanics and dynamics. LEGO brick lab benches and data logging and processing utilities make experiments even more interesting and visual, and help children better understand physics. For example, students can collect melting point data and use the app to organize it and present it in a graph. But that's just the beginning: today we'll show you how to add the MicroPython programming environment to this kit and use it to teach robotics.

Learn to Program with EV3
Modern schoolchildren want to see a colorful result. Yes, they are bored if the program displays numbers on the console, and they want to look at color graphs, diagrams and create real robots that move and execute commands. The usual code is also too complicated for children, so it is better to start learning with something easier.
The EV3 Core Programming Environment is based on the LabVIEW graphical language and allows you to set algorithms for the robot visually: commands are presented in the form of blocks that can be dragged and connected.

This method works well when you need to show how algorithms are built, but it is not suitable for programs with a large number of blocks. As the scenarios become more complex, it is necessary to switch to programming with code, but it is difficult for children to take this step.
There are several tricks here, one of which is to show that the code performs the same tasks as the blocks. In the EV3 Environment, this can be done through MicroPython integration: children create the same program in the basic programming environment with blocks and in Python in Microsoft's Visual Studio Code. They see that both methods work in the same way, but it is more convenient to solve complex problems with code.
Switching to MicroPython
The EV3 Environment is based on the ARM9 processor, and the developers deliberately left the architecture open. This decision made it possible to roll out alternative firmware, one of which was an image for working with MicroPython. It allows you to use Python for EV3 programming, making working with the kit even closer to real life tasks.
To get started, download to any microSD card, insert it into the EV3 Brick and turn it on. Then you need to install for Visual Studio. And you can get to work.
Programming the first robot in MycroPython

Our there are several lessons to master the basic concepts of robotics. EV3 models introduce kids to the basics of self-driving cars, factory assembly robots, and CNC machines.
We will take as an example a drawing machine that can be taught to draw patterns and geometric shapes. This case is a simplified version of adult welding or milling robots and shows how EV3 can be used with MicroPython to teach students. Also, a drawing machine can mark holes in a printed circuit board for dad, but this is another level that requires mathematical calculations.
For work we will need:
- LEGO MINDSTORMS Education EV3 Core Set;
- a large sheet of checkered paper;
- colored markers.
The assembly of the robot itself is in , and we will consider a programming example.
First, initialize the EV3 Brick Library:
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Color, ImageFile
from pybricks.tools import wait
We set up a platform that rotates the handle like a motor in port B. Set the gear ratio of the two-stage gear with the number of teeth 20-12-28, respectively.
turntable_motor = Motor(Port.B, Direction.CLOCKWISE, [20, 12, 28])
Set up the handle lift as a motor in port C:
seesaw_motor = Motor(Port.C)
Set up a gyroscope that measures the angle of the handle in port 2:
gyro_sensor = GyroSensor(Port.S2)
Set up a color sensor in port 3. The sensor is used to detect white paper under the drawing machine:
color_sensor = ColorSensor(Port.S3)
Set up the touch sensor in port 4. The robot starts drawing when the sensor is pressed:
touch_sensor = TouchSensor(Port.S4)
Define the functions that raise and lower the handle:
def pen_holder_raise():
seesaw_motor.run_target(50, 25, Stop.HOLD)
wait(1000)
def pen_holder_lower():
seesaw_motor.run_target(50, 0, Stop.HOLD)
wait(1000)
We define a function to rotate the handle to a given angle or to a certain angle:
def pen_holder_turn_to(target_angle):
if target_angle > gyro_sensor.angle():
If the target angle is greater than the current angle of the gyro sensor, continue moving clockwise at a positive speed:
turntable_motor.run(70)
while gyro_sensor.angle() < target_angle:
pass
elif target_angle < gyro_sensor.angle():
If the target angle is less than the current gyro sensor, then move counterclockwise:
turntable_motor.run(-70)
while gyro_sensor.angle() > target_angle:
pass
Stop the rotating platform when the target angle is reached:
turntable_motor.stop(Stop.BRAKE)
Set the initial position of the handle to the top position:
pen_holder_raise()
Now comes the main part of the program - an infinite loop. First, EV3 waits for the Color Sensor to detect white paper or a blue starting square, and for the Touch Sensor to be pressed. Then he draws a pattern, returns to the starting position and repeats all over again.
When the device is not ready, the LEDs on the controller turn red and the LCD screen displays a "thumbs down" image:
while True:
brick.light(Color.RED)
brick.display.image(ImageFile.THUMBS_DOWN)
We wait until the color sensor reads blue or white, set the color of the LEDs to green, display the βthumbs upβ image on the LCD screen and report that the device is ready for use:
while color_sensor.color() not in (Color.BLUE, Color.WHITE):
wait(10)
brick.light(Color.GREEN)
brick.display.image(ImageFile.THUMBS_UP)
We wait for the touch sensor to be pressed, assign the angle value 0 to the gyro sensor and start drawing:
while not touch_sensor.pressed():
wait(10)
gyro_sensor.reset_angle(0)
pen_holder_turn_to(15)
pen_holder_lower()
pen_holder_turn_to(30)
pen_holder_raise()
pen_holder_turn_to(45)
pen_holder_lower()
pen_holder_turn_to(60)
Raise the pen holder and return it to its original position:
pen_holder_raise()
pen_holder_turn_to(0)</i>
Here we have such a simple program. And now we launch it and look at the drafting robot in action.
What are these examples

EV3 is a career guidance tool for STEM careers and an entry point into engineering careers. Since it can solve practical problems, children gain experience in technical development and the creation of industrial robots, learn to simulate real situations, understand programs and analyze algorithms, master basic programming constructs.
MicroPython support makes the EV3 platform suitable for high school education. Students can try themselves as programmers in one of the most popular modern languages, get acquainted with professions related to programming and engineering design. EV3 kits show that writing code is not scary, prepare you for serious engineering challenges and help you take the first step towards mastering technical specialties. And for those who work at school and are connected with education, we have prepared and educational materials. They describe in detail what skills are formed in the performance of certain tasks, and how the acquired skills correlate with training standards.
Source: habr.com
