Picar-X Lesson 9 Color Detection

Basic Teaching Information

Suitable School Age

9-12 years old


90 minutes



Teaching Objectives

1. Learn the role of color detection in automatic drive and different properties of the detected object.

2. Take a quick look at how the color detection of PiCar's camera works.

3. Learn about pixel and resolution.

4. Know how to test color detection, how to use color property and the horizontal & vertical rotation block for camera.

5. Judge whether an object in a corresponding color appears according to the condition“the width of the object > 50”.

6. Understand the role of the values of RGB primary colours.

Important and Difficult Points of Teaching

Teaching Key Points

The different usage of the properties of the detected color.

The control method of the camera servo.

Teaching Difficult Points

Students understand the method that using width as the condition to judge whether there appears a color object.

Teaching Form

Give a lecture, cooperative group learning.

Teaching Media

Teaching Grouping and Teaching Aid

Teaching Group

Work in groups of one person.

Teaching Aid

PiCar (each student has one), tablet computer (each student has one)

pen, and programming growth manual, and a number of standard red, orange, yellow, green, blue and purple cards.

Teaching Procedure

Teaching Stage

Teaching Content


1 Lead in

Talk about the usage scenario of color detection of image detection in automatic drive (traffic light detection, the detection of the vehicle ahead)


Explain what the different properties of a color can be used to detect: for example, the height and width can be used to detect the types of vehicles. 

Introduce how the color detection of PiCar's camera works.

(Here, the teacher uses the pallet in the Drawing & Painting Apps to explain RGB color values. Firstly write the RGB value range of red color into the code. When we need to detect colors, the color shot by the camera will be compared with the RGB value range set before. If the RGB color values of the shot color are within the set range, the shot color is detected successfully; otherwise, the shot color fails to be detected.)

15 minutes

2. Objective

The teacher and students will use cards in different colors to direct PiCar to do different things. Each student has at least three cards in different colors. Finally every student should tell a story about the tasks that PiCar has accomplished.


The teacher can prepare a program in advance. Red means stopping; green means going forward; yellow means shaking its head.

Story: One day, PiCar was driving on his own to a yellow light. (put the yellow card in front of PiCar) at that moment, PiCar shook his head, telling us not to pass. Then the traffic light turned green (put the green card in front of PiCar), and PiCar went forward.  

At the next intersection, the traffic light turned red, so PiCar stopped.

5 minutes

3. Program Test

Aroused students' interest in learning via the previous story, the teacher explains how to use the horizontal & vertical rotation block for camera, the color detection block and helps students know the meaning of every property of a color.  

Task 1: we saw a lot of little red dots being detected as red cards. How do we make PiCar not detect red dots, but only red cards? We need to change the judgment condition to width > 50 pixels, so that only when about one-sixth of the range of the screen appears red, the red card can be detected, which avoids the mistake of detecting red dots as red cards.

After finishing task 1, PiCar can detect the red card and say “this is red” and nods.

Explain the problem through the phenomenon of task 1: In our environment, like walls, like the ground, there might be little red dots that PiCar's camera detects.

So what do we do to avoid this kind of error? Now it’s time for the teacher to introduce pixel and resolution. The teacher explains how high the resolution of the video monitor is and then proposes the solution: the color is only detected when its width is > 50.

Task 2: Use a red card to direct PiCar to retreat at a 100% speed for 2 seconds and a green card to make it go forward at a 100% speed for 2 seconds.

With this question, explore how to make PiCar detect multiple colors at the same time. Because the code in our loop block is executed repeatedly and very quickly, the teacher and students enable the red color detection at first. If the red is detected, PiCar goes back at a 100% speed for 2 seconds.

Then the teacher and students enable green color detection. If the green is detected, PiCar goes forward at a 100% speed for 2 seconds. In this way, PiCar is constantly switching the colors that need detecting and it switches very quickly. When the camera detects a required color, PiCar takes the action that needs to be performed by judging the condition. Give a 100ms delay before turning on a detection of a new different color to avoid the confusion of color detection.

30-45 minutes

4. Read the program flow, complete tasks and optimize them.

Read the flow chart of the program, edit the final task program independently, and complete the task.

Guide the students to add various expansions according to their own ideas on the basis of completing the task (teachers can teach students according to their aptitude in this process, and the learning difficulty of students with better ability can be increased).

Task 3: Use cards in 3 different colors to direct PiCar to do different things. Finally students should tell a story about the tasks that PiCar has accomplished.

In this process, the teacher helps the children to be creative and make the game more fun.

15-30 minutes

5. Summary and Reflection


1. How does color detection work?

2. What are pixels and resolution?

3. How does PiCar judge whether a color card is present?


What did we ask PiCar to accomplish today? What difficulties did we encounter and what did we learn from this process?

10 minutes

6. Homework

Program and set up scenarios. Use cards in 6 different colors to control PiCar.