agsdix-fas fa-child

Traffic Light

With this activity, you will learn to automate the operation of a traffic light.

info outline icon
Basic Activity; Main Loop; Timing.

What are we going to do?

Are you a teacher?

Lesson Plan


  • Grades 3-12
  • Courses 3rd grade – 12th grade


  • Cell phone, tablet, or computer
  • Internet connection


In this activity, students will have the opportunity to experiment with Protobject, a visual programming tool. They will learn programming and computer science concepts in a safe and fun way.

Educational Objectives

  • Understand the concept of the Main Loop.
  • Create a technological object (prototype) using a device.
  • Identify relationships between technology and the surrounding environment.
  • Evaluate their own and others’ work, both individually and in a team.
  • Engage in dialogues and reflections to propose improvements.

Beginning (10 minutes)

Welcome the students and introduce the day’s activity.

Today, we will learn how to prototype a traffic light.

We will start the class by providing technical knowledge to the students for meaningful learning of the practical activity.

Begin the class with a question:

Do you remember how to time a LED?

Allow students to respond and then provide the correct answer. If necessary, show the previous code.

“When we wanted to time a LED, we assigned it a time to turn on and off. Now, we will assign colors!”

You can explain the order of the traffic light colors (red, yellow, and green) or challenge them to research it themselves by observing a real traffic light or searching online.

Next, present a challenge:

“What if we want the traffic light to operate for 1 hour?”

Let the students explore this question and then provide the answer.

“If we wanted to repeat this code every second for an hour, we would have to do it 3600 times. That would be a lot of code. This is where the concept of the ‘Main Loop’ comes into play.”

Explain what a “Main Loop” is:

“The ‘Main Loop’ consists of repeating the entire process indefinitely until we decide to stop it.”

Finally, to kickstart the prototyping process and make them feel like the protagonists of their learning, ask them:

Do you want to learn how to prototype a traffic light?

Development (20-30 minutes)

Connect the devices to the Protobject platform so that students can interact with the LEDs on the screen through programming commands.

Follow the steps in the “Prototyping” section on the platform. Ensure that you have experimented beforehand to anticipate students’ questions.


Closure (5-10 minutes)

Ask students how a traffic light works in real life.

“How many traffic lights are used at an intersection?”

“What is the main purpose of a traffic light?”

Explain that a pedestrian traffic light shows the red color to stop cars and then the green color to allow people to cross.

Finish by exciting them for the next exercise:

“In the next exercise, you will be the creators of your own traffic light!”

agsdix-fas fa-book-reader


What can we do with an LED lamp?

What happens if we make an LED lamp change to green, yellow, and red? We create a traffic light!

To do this, we need to time the lights and assign them colors!

But the code stops!

By executing the blocks in order, we will make the traffic light go from green to yellow and then red, finishing the program there. If you don’t remember how to change colors, you can check the previous activity.

To have the process repeat, we would need to start repeating the sequence so that after red comes another green, then yellow, red, green, yellow, red, and so on…

But what if we want the traffic light to operate for an hour? We would have a lot of repeated code!

What if we want the entire process to repeat on its own?

We use the main loop!

Here, the concept of the Main Loop comes into play!

This allows us to repeat all the program code until the user terminates it.

In this way, we can keep a sequence of blocks running indefinitely!

For example, using the Main Loop, we can continuously repeat the code that controls the traffic light.

Let’s get to work!

We are going to create a prototype of a traffic light by placing the code inside a main loop. To achieve this, we will program the traffic light to switch on in the colors green, yellow, and red, with a 6-second delay between the green and red lights, and a 1-second delay between the yellow and red light.


Let’s create our traffic light!


Press Add Device and select Lamp.


Scan the QR code.


Remember that if you don’t have a smartphone to scan the QR codes, you can press Open in this window to open the components on the same computer.

We are ready to start prototyping!

Code composition

Click on question circle icon to open the comments explaining the code.

The loops are located in the category Loops



Now that you’ve created a traffic light, ask yourself:

How does a traffic light work in real life?

How many traffic lights are used at an intersection?

What is the purpose of a traffic light?

In the next activity we will learn how to create a more advanced traffic light.