Teaching Research and Publishings Speaking Engagements Professional Development About Me

EV3 Programming Resources


EV3 Colour Detection Project


Step 1: Setup

In groups, choose one laptop to be used for coding and a second laptop for research. Close all other laptops.

Download and install Visual Studio Code and the LEGO MINDSTORMS EV3 MicroPython extension.

Connect to the local wifi:


Step 2: Buld the Car

The instructions are available here:

Colour Detection Instructions

Connect the sensors and motors using the three cables:


Step 3: Code the Parking Gate

Start a new project. Open up main.py and use this code as a starting point:

#!/usr/bin/env pybricks-micropython

from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
                             InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
                             SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase

import time

# Start the program with a beep
brick.sound.beep()

# Define which motors and sensors are connected
motorLeft = Motor(Port.A)
motorRight = Motor(Port.B)
colorSensor = ColorSensor(Port.S1)

# Fetch current color
print("Current Color: "+str(colorSensor.color()))

# Turn left
motorLeft.run(100)
motorRight.run(0)

# Wait three seconds
time.sleep(3)

# Turn right
motorLeft.run(0)
motorRight.run(100)

# Wait three seconds
time.sleep(3)

# Move forward
motorLeft.run(100)
motorRight.run(100)

# Wait three seconds
time.sleep(3)

# Move backwards
motorLeft.run(-100)
motorRight.run(-100)

# Wait three seconds
time.sleep(3)

# Stop
motorLeft.run(100)
motorRight.run(100)

Read the starting code line by line and all the comments. As a group strive to understand every line of code.

Your bin will have two names, the LEGO Brick will use the name on the red tape. Use Visual Studio Code to the LEGO Brick:

Push F5 on a PC or FN+F5 on a Mac to test your code.

The colour sensor will provide a number representing which color it is currenlty sensing:

  1. Color.BLACK
  2. Color.BLUE
  3. Color.GREEN
  4. Color.YELLOW
  5. Color.RED
  6. Color.WHITE
  7. Color.BROWN

Follow a Line

Using the provided electrical tape, create a line on your desk resembling something like this:

Code your LEGO car to follow the line.

Stay in a Box

Using the provided electrical tape, create a shape on your desk resembling something like this:

Code your LEGO car stay within your shape.


Step 4: Submitting

Submit both of your your main.py files and a list of group members that were present in Blackboard.