TRAFFIC LIGHT

PIPER MAKE EDUCATOR RESOURCES SERIES

To do this project, you will need a Piper Make Starter Kit. Get yours here:

Build and code the timing for all three LED colors of a traffic light.

To get started, head to Piper Make and hit this icon:

Time: 30 minutes

Age Range: 8+

Difficulty: Intermediate

In this project, students build three LED lights and program them to light up in a sequential order similar to a traffic stoplight.

Note: There are step by step instructions for the students to follow in the tutorials included in each project on Piper Make. These provide directions both for writing code and for building the electronic circuits. The tutorials are well-defined and most students will be able to follow them with little assistance required.

LEARNING OBJECTIVES

Students will:

 

  • Practice breadboarding and wiring
  • Review and understand computational concepts of:
  • loops: running the same sequence multiple times.
  • sequence: identifying a series of steps for a task
  • Demonstrate computational thinking core concepts, including:
    • Algorithm Design by creating an ordered series of instructions for solving similar problems or for doing a task, such as turning a light off and on in the right order.
    • Simulation by developing a program to imitate the real-world process of a stoplight.
  • Create programs that include events, loops, and conditionals.
  • Decompose problems into smaller, manageable tasks which may themselves be decomposed.)
  • Test and debug a program or algorithm to ensure it accomplishes the intended task.
  • Perform different roles when collaborating with peers during the design, implementation, and review stages of program development.

STANDARDS ALIGNMENT

CA Computer Science Standards

3-5.AP.12: Create programs that include events, loops, and conditionals.

3-5.AP.13: Decompose problems into smaller, manageable tasks which may themselves be decomposed.

3-5.AP.14: Create programs by incorporating smaller portions of existing programs, to develop something new or add more advanced features.

LANGUAGE OBJECTIVES

CCSS.ELA.L.W.3.8: Recall information from experiences or gather information from print and digital sources; take brief notes on sources and sort evidence into provided categories.


CCSS.ELA.L.W.3.10: Write routinely over extended time frames (time for research, reflection, and revision) and shorter time frames (a single sitting or a day or two) for a range of discipline-specific tasks, purposes, and audiences.


CA ELD.3.C.11: Supporting own opinions and evaluating others’ opinions in speaking and writing


CA ELD.3.C.12: Selecting and applying varied and precise vocabulary and language structures to effectively convey ideas

CONCEPTS

In this project, students will expand their understanding of the concept of sequences in loops by creating code to make three LEDs turn on and off in a repeat forever loop. They will then stretch to form algorithms and automation, while practicing testing and debugging.

PARTS

Raspberry Pi Pico, breadboard, charging cable, M2M Green Jumper Wire, M2M Yellow Jumper Wire, M2M Red Jumper Wire, M2M Black Jumper Wire, Yellow LED, Red LED, Green LED, 330 Ω Resistor

GPIO SETUP

Tactile Button GP12, Green LED GP13, Yellow LED GP14, Red LED GP15

OVERVIEW OF STEPS

Step 1: Puffin crossing

Let’s build a traffic light!

You likely know the intersection rules:
- RED means stop
- YELLOW means slow
- GREEN means go

You can build your own traffic light using the Pico, LEDs, and a push-button!

Microcontrollers like your Pico can be found in most of the electronic items you use on a daily basis, including traffic lights. A traffic light controller is a specially built system that changes the lights on a timer, watches for pedestrians looking to cross, and can even adjust the timing of the lights depending on how much traffic there is, by talking to nearby traffic light systems to ensure traffic in all directions keeps flowing smoothly.

Fun fact, in the UK, some traffic lights are called "Puffin" crossings because they are pedestrian-operated user-friendly intelligent crossings.

Click NEXT.

Step 2: Gather your electronics

Grab the parts shown below:

We can make the picture above bigger by clicking the icon in the top-right corner of the picture.

Once you've gathered your parts, click NEXT.

Step 3: Digital View Double-check

Remember to click the Digital View tab on the bottom left of your workspace to see GPIO pin numbers and the GROUND pins.

The Digital View is a great tool to check your work!

Ready? Click NEXT.

Step 4: Build your LEDs

Let’s start by building a simple traffic light system using a red, a yellow, and a green LED.

We'll start with the red LED.

Remember that LED circuits need a resistor to control the current flowing into the LED.

Using your parts, build the circuit shown in the image below:

Remember from the Blink tutorial that LEDs only work when they are put in the correct way.

The long lead should go into the side that is connected to GP15, and the short lead/flat side of the LED should connect to the resistor.

Click NEXT.

Step 5: Staying Grounded

Remember a basic circuit is a continuous flow of charge from a POWER source to GROUND.

For POWER, we can either use the 5V or 3.3V power pins (the V stands for "volts") for "always on" current, or we can use the GPIO pins to turn the power on and off.

All circuits also have to be connected to GROUND to be complete. In this circuit, we'll create a GROUND rail by connecting a GROUND pin to the (-) blue column on the edge of the breadboard, as shown in the picture below:

Click NEXT.

Step 6: Double check it!

Double-check the wiring. Make sure that there is a complete circuit. The animation below shows the path that the electrical current will follow.

Click NEXT.

Step 7: Yellow and green

The red LED says "Stop!", but what about "slow down" and "go"?

Let’s connect the yellow and green LEDs!

Expand the image below to see how to connect the rest of your wires, LEDs, and resistors.

The yellow LED will be connected to GP14, and the green LED will be connected to GP13.

Remember that LEDs only work when they are connected in the right direction. The long leads should connect to GP13/GP14, and the flat edge/short leads connect to the resistors.

When you have finished building your circuit, click NEXT.

Step 8: Time to Code!

Let’s think about what we want each LED to do. In a traffic light, the RED light, for example, will turn on, stay on for a while, and then turn off, right?

So let’s drag the following blocks from the Chip menu. Connect them together. Now, make sure turn pin 13 is ON, mess with the values in the wait block, and then turn pin 13 OFF. Hit START to test it out!

Did it work? click NEXT.

Step 9: All the LEDs!

Yellow and green lights in a traffic light basically do the same thing, so let's copy that code and change the pins based on which ones are connected to the LEDs.

We can right-click any block and select duplicate to make a copy of it.

After we have made copies of the blocks, change the pin numbers to light up the green and yellow LEDs:

GP13 = Green
GP14 = Yellow
GP15 = Red

Then, click NEXT.

Step 10: Make It Repeat Forever

Traffic lights don’t stop after just going through once! To make the code repeat, use a repeat block.

To do this, drag the first turn pin block away from the Start block.

Drag out a repeat while true block from the Loops menu and connect it below the start block.

Drag the blocks we disconnected before to the inside of the repeat while true block.

Click NEXT.

Step 11: Check please!

Double check the variables to make sure the pins are correct. Be sure each light turns on, waits, and then turns off before the next light does the same.

In what order do the lights turn on and off in a traffic light? If the traffic light starts on green, which light is the next in the sequence? Usually, the sequence is green to yellow to red, and then back to green!

Does the code reflect that? If yes, click START to run the code.

Use the DIGITAL VIEW to see if the code is sending current to the right GPIO pins.
When everything is working, click NEXT.

Step 12: Wait times

Traffic lights don’t always stay on red, yellow, and green for the same amount of time, right?

The red light is usually the longest, then it goes to green to let cars pass, then the yellow light comes on briefly as a warning to slow down before the red light tells us to stop.

Now that we have our LEDs and a basic program in place, let's make it work properly by adjusting the wait times as follows:

Green = 5 seconds
Yellow = 1 seconds
Red = 8 seconds

When you finish adjusting the wait times, click NEXT.

 

Step 13: Run your program!

Click START and watch the LEDs. If we were driving, we would stop at a red LED and wait there while it was on. Then we'd go on the green, and (maybe at the next light) slow down when we see yellow before it switches to red for us to stop again. Then the loop repeats!

The pattern will loop until we press the Stop button because it forms an infinite loop. It’s based on the traffic light pattern used in real-world traffic control systems, but sped-up. Giving cars just five seconds to pass through wouldn’t let many cars go through!
Troubleshooting
Use the Digital View to troubleshoot the code if the LEDs are not working. We should see the GPIO pins light up as our code calls to them. If the code is working but the LEDs are not lighting up, check the wiring of the hardware by going back to the first steps.

Once everything is working, click NEXT.

Step 14: Plan for Traffic

Pedestrians also rely on the traffic lights to cross the street safely. In many intersections, we might see a button on the traffic light pole that people can push to let the system know they want to cross the street.

These buttons are especially useful late at night, when some traffic lights are always set at a green light until the pedestrian cross button is pressed.

To create this crossing, we’ll need to add a push-button to our circuit and code!

Click NEXT.

Step 15: Time the Lights

Let’s add a push-button to our circuit.

Make sure the input is wired to GP12 and that there is a connection to the ground to complete the circuit.

Once you have your parts, click NEXT.

Step 16: Button press

The first step to our late-night traffic light is ensuring that we know when the button is pressed!

Normally, when we use a wait block, it's because we want to wait a certain amount of time. Instead of waiting for a certain amount of time, we want our button to wait for a button press.

There is another block that waits for something to be true or false. This is the wait until block from the Chip menu. Go ahead and drag that block out and connect it under the turn pin 13 ON block:

In coding, when something has a value of true or false, it is referred to as a boolean variable.

We can check a button using the is pin block from the Chip menu. Our button is connected to GP12, so drag out an is pin block and place it into the wait until block. Change the pin variable to 12.

Finally, change the time in the wait block under the wait until block to 1 second:

Click NEXT.

Step 17: Big intersection

Try it out!

Click CONNECT, then click START. If it's working correctly, when you press the button, the green LED will stay on for 1 more second, then the yellow LED will glow, then the red LED.

Imagine that the two intersecting streets each have many lanes! If that’s the case, it would take even longer for the pedestrian to cross the street, right? Adjust the wait time for the RED LED block to experiment and find the perfect time to ensure that the pedestrian crosses the street safely!

Try experimenting with different wait times, or by moving the wait until block to different places in your code.

When you are finished, click NEXT.

Step 18: Piperbot and Pip crossing

Congratulations! You have created a prototype that simulates how a traffic light works late at night!

Challenge: Can you build a crosswalk signal?

In the United States, a crosswalk signal is usually a walking person for a bit of time, then a flashing red hand, and then finally a solid red hand. Using the red and green to represent the hand and walking person symbols, can you modify your circuit and code to build this crosswalk signal?

Click NEXT.

Step 19: You Finished!

Click EXIT to return to the menu and start your next coding challenge.