Drawing with MicroPython (STEM Alliance competition)


    We have recently discovered the possibility of working with the EV3 with a general purpose and high level computer language as Python, which is used for developing the complex application like scientific and numeric application, and for both desktop and web applications.

    If you go to the official Lego website for the EV3, you can obtain the firmware and the documentation about how to program your EV3 brick. To get started you need:

    1. Download and flash the EV3 MicroPython image onto a micro SD card
    2. Insert your micro SD card into the SD card slot on the EV3 Brick and turn it on
    3. Download, install, and launch the free Visual Studio Code editor on your computer 
    4. Install and activate the LEGO Education EV3 extension
    5. Connect the EV3 Brick to your computer and start to code

    In our activity in Spain, we have decided to create a DRAWER ROBOT by joining the Robot Educator and a small motor which has a special support for the pen (inspired in one of the task from the Space Challenge), as you can see in this photo:


    The basic program for our robot was to draw an square:

    #!/usr/bin/env pybricks-micropython
    from pybricks import ev3brick as brick
    from pybricks.ev3devices import Motor
    from pybricks.parameters import Port
    from pybricks.tools import wait
    from pybricks.robotics import DriveBase
    # Initialize two motors and a drive base
    center = Motor(Port.A)
    left = Motor(Port.B)
    right = Motor(Port.C)
    robot = DriveBase(left, right, 56114)
    # Create a variable for the number of sides
    sides = 4
    # Repeat the movements to create a polygon
    for x in range(sides):
    # Run the motor up to put down the pen a target angle of 30 deg
    # Drive forward at 100 mm/s for two seconds
    # Run the motor up to put up the pen a target angle of -30 deg    
    # Turn at 45 deg/s for three seconds
        robot.drive_time(0360/sides, 1000)

    After that, we adapt the program to experiment drawing with different geometrical forms:

    If you want to draw more complex geometrical figures as a mosaic made by

    repeating a polygon after an advance or a turn, you need to include a second

    for function inside the first one, and the code could be like this:

    # Create a variable for the number of sides and polygons

    sides = 7

    polygons = 4

    for x in range (polygons):

       for x in range(sides):

         # Turn and drive back previously



         # Run the motor to put down the pen to draw



         # Run the motor to put up the pen

         center.run_target(50, -30)


      # Turn and drive forward before to repeat



    We can see some results in this video:

    A different strategy is to create a variable (called timer) which could
    be increased each time that the program runs and we can create a spiral
    # Create a variable for the repetitions
    sides = 4
    # Create a variable for going forward
    time = 0
    # increasing the value of the time
    def timer():
      global time
    # Repeat the movements to create a form
    for x in range(sides):
        robot.drive_time(1000, time) 
        center.run_target(50, -30)
        robot.drive_time(0360/sides, time/2)
    And this is how it works:
    NOTE: This is our contribution to the european competition organized
    by the STEM Alliance.