Transcript
Exercise 2: Traffic Lights
Parts
Do this
Part | Image | Description |
---|---|---|
Mehackit Board (or Arduino UNO) | A minicomputer you can program to control light, sound and motors. Connect sensors or switches to make interactive devices. | |
USB cable (A/B) | You upload the program you have written on the computer to Arduino through the USB cable. Arduino also gets power through it. | |
Jumper wires | You'll need plenty of thin wires of different colors when working with Arduino! | |
Breadboard | Breadboards are great for making temporary circuits. | |
3 x LED: Red, yellow and green | LEDs conduct electricity to one direction only. The longer leg is connected towards the 5V pin or later on a programmable digital pin. | |
3 x Resistor 330Ω (or 220Ω) | Resistors resist the flow of an electric current. The value of the resistor is measured in ohms (Ω). The resistance value is coded into the colored stripes (330Ω: orange, orange, brown and gold). You can replace the 330Ω resistor with a 220Ω one. |
Circuit
Do this
Connect the longer legs of the LEDs to pins 5, 6, and 7.
Connect the shorter legs of the LEDs through a 330-ohm resistor to ground (GND).
Programming
Do this
Write the example code from below to Arduino IDE. Start adding new commands to make the traffic lights work.
- Upload the code to your board after you have written a new line or two - testing your code often is a good idea!
- Use comments to remind yourself which colors are connected to each pin.
- Use the pin numbers 5, 6 and 7 with
digitalWrite
commands to turn the LEDs on and off - Use
delay
command to time the traffic lights - Code your traffic lights to turn on and off just like real ones would!
void setup() {
pinMode(5, OUTPUT); // green
// set the other two pins as outputs, too!
}
void loop() {
digitalWrite(5, HIGH); // green light on
delay(2000);
digitalWrite(5, LOW); // green light off
// continue the program to control the other lights, too
}
Educator notes
As the students learn more, they may continue the traffic lights project by making it react to nearing vehicles or pedestrians using e.g. distance sensors and pressure or reed switches.
If some of the students do not complete this exercise but want to connect and control multiple LEDs later on, refer to this exercise as a source of useful information.
If the students need help with their code, it is a good practice to first encourage to solve the problem by themselves. Try asking questions and troubleshooting collaboratively.
Questions:
- Have you checked the console - are there error messages when you upload the code? What do the messages say?
- Have you checked you circuit, could there be bad connection there?
- Can you describe what you were trying to do? Describe the problem in as much detail as you can! (Often a problem gets solved just by explaining it to another person - a classmate or the teacher!)
- Do you remember an earlier task or exercise where you learned about the part you're struggling with? Have you tried looking at those earlier examples?
- Has anyone else in the classroom run into the same problem? Could they explain how they solved it? (Peer learning is a powerful experience both for the one who's instructing - the person who already managed to solve the problem on their own - and the one who's receiving instructions. More powerful than simply hearing the right answer from the teacher!)
If none of these tips are successful, look at the student's code and try to troubleshoot together with them. Show parts of the ready-made code as the last means.
NOTE: the students' code may look a bit different - the timing of the traffic lights, whether the lights start from red or green... In this example, red and yellow are lit at the same time when the light is about to turn green - as it is the case with real traffic lights. When you assess a submission, it may be enough to mention this functionality in your feedback if it's missing from a student's code.
Example Code
void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
digitalWrite(5, HIGH);
delay(4000);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(2000);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(4000);
digitalWrite(6, HIGH);
delay(2000);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
}