LEGO MINDSTORMS Education EV3 Core Set

Line Detection

Design ways to improve driving safety by helping to prevent drivers from falling asleep and causing an accident.

45-90 min.
Intermed.
Grades 6-8
Line Detection
Teacher Support

Students will:
Explore the concept of the Loop.
Understand the concept of a switch and how to use it for true and false operations.

This lesson works on the following operating systems:
device_logodevice_logodevice_logodevice_logodevice_logo

NGSS
MS-ETS1-1. / MS-ETS1-2. / MS-ETS1-3. /MS-ETS1-4.

CSTA
2-A-2-1 / 2-A-7-2 / 2-A-7-3 / 2-A-7-4 / 2-A-5-5 / 2-A-5-6 / 2-A-3-9 / 2-A-6-10 / 2-C-7-11 / 2-C-4-12 / 2-D-5-16 / 2-I-1-20

Student worksheet

Download, view or share the student worksheet, either as a an online HTML page or a printable PDF

Share with:

Line Detection

Connect

Video preview

Ignite a classroom discussion around the following questions:

  • Can autonomous cars react to different traffic light signals?
  • What can happen if a driver falls asleep while driving?
  • How can we detect when a driver is falling asleep?

Allow the students to select the tool(s) they find most appropriate for capturing and sharing their ideas. Encourage them to document their thoughts using text, videos, images, sketchnotes, or another creative medium.

Explain to the students that they will once again be using the Color Sensor. They will extend their understanding of how this sensor reacts to light by using reflected light intensity to create a program that will drive their wheeled robot along a given track.

Tell them that they will use the Color Sensor to make their wheeled robot move more autonomously in order to simulate how an autonomous car might respond to traffic lights.
They will create a program that will make their wheeled robot drive around a given course or track.

Construct

Build
Students will construct the Robot Educator base model, and then they will add the Color Sensor pointing down.

Have the students perform the following building check before they program their robots:

  • Are the wheels rotating freely?
  • Are the wires correctly connected from the Color Sensor to port 3?

Program
Have the students begin a new project in the EV3 programming environment.
The students will begin exploring the function of the Color Sensor that recognizes LEGO® brick colors by programming their wheeled robot to drive along a path and stop at a red brick.
Have the students simulate a vehicle’s behavior at traffic lights by having their wheeled robot respond to a series of green and red signals. Placing their code inside a loop allows for the possibility of multiple “traffic lights” along a track.

Allow the students to select the tool(s) they find most appropriate for capturing and sharing their pseudocode. Encourage them to use text, videos, images, sketchnotes, or another creative medium.

Note
Refer students to the Robot Educator Tutorials for further assistance.
In the EV3 Software :
Robot Educator > Basics > Straight Move
Robot Educator > Basics > Curved Move
Robot Educator > Hardware > Color Sensor - Color
Robot Educator > Beyond Basics > Loop
Robot Educator > Basics > Stop at Line
Robot Educator > Beyond Basics > Switch

Students will need to use the Wait Block to do this. Point out that the Wait Block can be configured to be triggered by multiple colors, or just one. In this case, students will create a program that uses the Color Sensor to stop the motors when it detects the color red.
Point out to the students that they will need to make sure all other colors are deselected for the Color Sensor to respond most effectively to the colors they choose (red and green).
Explain also that they will be exploring how switches work, and how to incorporate these into their programs.
Explain that they will be exploring how loops work and how to incorporate these into their programs.

Setup
Use the technic beams available in the EV3 core set to simulate green and red lights. Place the beams on the table so the Color Sensor can detect them while rolling over them.

POSSIBLE SOLUTION

RED LIGHT DETECTION

  1. Start the program.
  2. Start motors B and C (drive forward).
  3. Wait for the Color Sensor to detect the color red.
  4. Stop the motors.
DOWNLOAD SAMPLE PROGRAM
(16 KB, Requires EV3 Desktop Software)

POSSIBLE SOLUTION

RED AND GREEN LIGHT DETECTION IN LOOP

  1. Start the program.
  2. Start motors B and C (drive forward).
  3. Wait for the Color Sensor to detect the color red.
  4. Stop the motors.
  5. Wait for the Color Sensor to detect the color green.
  6. Repeat steps 2 to 5 forever.
DOWNLOAD SAMPLE PROGRAM
(16 KB, Requires EV3 Desktop Software)

Contemplate

Students should use the same function of the Color Sensor to recognize when the robot is crossing a line. Use a thick (approx. 2 cm or 3/4 in) dark line if you have a white or light surface, or a white line if you have a dark surface.
Have the students simulate alarm signal for the driver if the robot is crossing this line. This feature is often available in new cars.

POSSIBLE SOLUTION
FILENAME: CODING-04.EV3 (Tab: 3)

LINE DETECTION IN LOOP

  1. Start the program.
  2. Start motors B and C (drive forward with a curve toward the line).
  3. Wait for the Color Sensor to detect the color black, then start tasks 1 and 2.

TASK 1
4. Play sound “Horn 1”.

TASK 2
5. Start motors B and C (drive forward with a curve away from the line).
6. Wait for the Color Sensor to detect the color white.
7. Repeat steps 2 to 6 forever.

DOWNLOAD SAMPLE CODE
(16 KB, Requires EV3 Desktop Software)

Differentiation Option
The students will create an automated, driverless vehicle that can follow a line.
Have the students explore how an automated vehicle might be guided along a road or track.
The students will need to be introduced to the Switch Block, which will operate inside a loop.
Explain that the Switch Block can be used to automate a program that allows the wheeled robot to operate autonomously.
Also explain that the Switch Block can be used to control the flow of a program and that the default Switch Block, using the Touch Sensor, is a classic example of Boolean logic.
Demonstrate how to change the Switch Block to the Color Sensor, and explain that the trigger point is used to create the true/false statement (looking at the Switch Block, note how the program flows above the trigger point to do one thing, or below it do another).
Point out that in order to create the line-following program, they will need to “wiggle” the wheeled robot along the line. In other words, the wheeled robot will turn left and then right depending on whether the line (i.e., trigger) has been crossed. Find a suitable video online to demonstrate an example of this to the students. Point out that the Move Steering Blocks used in this challenge need to be set to “On”, not “On for” (e.g., Seconds, Degrees, or Rotations).
Once the wheeled robot is following the line, can it be improved to behave more like a car (i.e., move in a straight line rather than a wiggle)?

POSSIBLE SOLUTION
FILENAME: CODING-04.EV3 (Tab: 4)

LINE FOLLOWING IN LOOP

  1. Start the program.
  2. Start motors B and C (drive forward with a curve toward the line).
  3. Wait for the Color Sensor to detect the color black.
  4. Start motors B and C (drive forward with a curve away from the line).
  5. Wait for the Color Sensor to detect the color white.
  6. Repeat steps 2 to 5 forever.
DOWNLOAD SAMPLE CODE
(16 KB, Requires EV3 Desktop Software)

Note
Students will once again use the Color Sensor, but this time they will need to program it so that it responds to reflected light intensity. They will need to take reflected light intensity readings from the Port View in order to gauge what value to input into the Wait Block.
This will work best using black or blue tape on a very light (or white) surface.
You will need to spend some time explaining the concept of a switch and how it is an example of Boolean logic.
A possible extension from here would be to add a second Color Sensor, and combine the line-follow and traffic light programs to simulate automated passenger services, such as a train system in an airport.

Share
Allow the students to select the tool(s) they find most appropriate for capturing and sharing their creations, unique thinking, and learning process. Encourage them to use text, videos, images, sketchnotes, or another creative medium.
This lesson has covered a lot of new concepts and introduced several new blocks from the EV3 Software. Use this time to recap this information and ensure that the students understand it.
Ask one or two groups to demonstrate their programs.
Ask the students to share what they expected to happen vs. what actually happened with their programs.
Ask them whether anything about the results of their programs surprised them.

Assessment Opportunity
Specific rubrics for assessing computational thinking skills can be found under 'Assessment'.

Continue

Using text-based Programming
Have the students explore text-based programming solutions so they can compare different programming languages.

MakeCode - POSSIBLE SOLUTION

Program with Blocks or JavaScript in your web browser using Microsoft MakeCode editor for LEGO® MINDSTORMS® Education EV3.
You can find the Line Detection Activity Here

Swift Playground - POSSIBLE SOLUTION

ev3.move (left Port: .b, rightPort: .c, leftPower: 20, rightPower: 20)
ev3.WaitForLightColor (on: .three, color: .red)
ev3.stopMove(leftPort: .b, rightPort: .c)

ROBOT C: POSSIBLE SOLUTION

#pragma config(Sensor, S3, colorSensor, sensorEV3  Color, modeEV3Color_Color)
#pragma config(Motor, motorB, leftMotor, tmotorEV3  Large, PIDControl, driveLeft, encoder)
#pragma config(Motor, motorC, rightMotor, tmotorEV3  Large, PIDControl, driveRight, encoder)

/*
Create a program that drives the robot forward until the Color Sensor
sees red.
The robot then stops.
*/

task main()
{
	//Set motor speed at 20% (Drive Forwards).
	setMotorSpeed(motorB, 20);
	setMotorSpeed(motorC, 20);
	
	//Loop while the Color Sensor does see red.
	while(getColorName(colorSensor) == colorRed)
		{
		//Keep driving while the Color Sensor does see red.
		sleep(10);
		}
		
	//Set motor speed to 0% (Stop).
	setMotorSpeed(motorB, 0);
	setMotorSpeed(motorC, 0);
}
DOWNLOAD SAMPLE CODE
(2 KB, Requires ROBOTC Software)
Warning! This may be dangerous!

The following is a possible solution using the text-based programming language ROBOTC. You may choose to use any other LEGO MINDSTORMS Education EV3 compatible text-based programming languages.
LEGO Education has no ownership of the ROBOTC platform and does not provide any support or guarantee of the quality of the user experience and technology used. All required set up information is provided by ROBOTC at robotc.net. We recommend always to reinstall the official LEGO MINDSTORMS EV3 Brick firmware when you finish using other programming languages.

Guide to reinstalling EV3 Brick

Other Program Solutions

Swift Playground - POSSIBLE SOLUTION

While true 
{
ev3.move(leftPort: .b rightPort: .c, leftPower: 20, rightPower: 20)
ev3.waitForLightColor(on: .three, color: .red)
ev3.stopMove(leftPort : .b, rightPort : .c)
ev3.WaitForLightColor(on : .three color: .green)
}

ROBOT C: POSSIBLE SOLUTION

DOWNLOAD SAMPLE CODE
(2 KB, Requires ROBOTC Software)

Swift Playground - POSSIBLE SOLUTION

/*
Sound file used in this example are not the same as RobotC example 
*/

While true 
{
ev3.move(leftPort: .b, rightPort: .c, leftPower: 20, rightPower: 5)
ev3.waitForLightColor(on: .three, color: .black)
ev3.playSound(file: .errorAlarm, atVolume : 100, withStyle: .)
ev3.move(leftPort: .b, rightPort: .c, leftPower: 5, rightPower: 20)
ev3.waitForLightColor(on: .three, color: .white)
}

ROBOT C: POSSIBLE SOLUTION

DOWNLOAD SAMPLE CODE
(2 KB, Requires ROBOTC Software)
Teacher Support

Students will:
Explore the concept of the Loop.
Understand the concept of a switch and how to use it for true and false operations.

This lesson works on the following operating systems:
device_logodevice_logodevice_logodevice_logodevice_logo

NGSS
MS-ETS1-1. / MS-ETS1-2. / MS-ETS1-3. /MS-ETS1-4.

CSTA
2-A-2-1 / 2-A-7-2 / 2-A-7-3 / 2-A-7-4 / 2-A-5-5 / 2-A-5-6 / 2-A-3-9 / 2-A-6-10 / 2-C-7-11 / 2-C-4-12 / 2-D-5-16 / 2-I-1-20

Student Material
Student worksheet

Download, view or share the student worksheet, either as a an online HTML page or a printable PDF

Share with:

The maximum quantity of an item that can purchased in each transaction is 99.

To inquire about purchasing more than 99 of one item, please call 800-362-4308.

To inquire about purchasing more than 99 of one item, please call 800-362-4738.