Traffic Lights (Arduino)

by LoganPaxton in Circuits > Arduino

99 Views, 0 Favorites, 0 Comments

Traffic Lights (Arduino)

Screenshot 2024-10-08 1.01.29 AM.png

Hey! Welcome back to another tutorial! Today, we are going to be making a traffic light using a few LEDs! Also, my Arduino Uno R4 Wifi has come today (10/8/24) so, if you want to see some stuff using it, leave a favorite on this Instructable! Let's get started.

Supplies

The supplies needed for this tutorial are as follows:

  1. Arduino Uno R3: https://a.co/d/0lki3hk (x1)
  2. Jumper Wires: https://t.ly/nh7CW (x7-10)
  3. 100 Ω Resistors (Check resistance before buying!): https://t.ly/zd5ru (x3)
  4. Small breadboard: https://t.ly/L4lhf

Note: None of the above links are sponsored, or earn me any money. Also, the t.ly links are to an aliexpress shop, I decided to shorten the 100+ char links.

Wiring

Screenshot 2024-10-08 12.42.08 AM.png

Let's start wiring! This step should be fairly simple, as we are just wiring up 3 LEDs with some resistors thrown in. First, you want to connect the GND pin to a negative rail on your breadboard. Next, put 3 LEDs on the breadboard do not put them stacked put them beside each other (left to right)! Now, attach 3 wires to your breadboard to their each of their pins (2, 3, 4). Now, we should have a mess. I mean we should connect resistors to the wires and then connect the wires to their respective LED, the anode of their LED. Now, it's time of coding!

Note: I'm sorry about the explanation, I put a picture for reference :)

Coding

Screenshot 2024-10-08 1.02.06 AM.png

Now that we have finished wiring, we can now code! Like always, I have provided the .ino file for you :). Here is the code for this project.

IMPORTANT: If you use the enableColor function, set the color value as a lowercase string (Eg. Red=red)

#define GREEN 2 // Set to your green led pin
#define YELLOW 3 // Set to your yellow led pin
#define RED 4 // Set to your red led pin

void setup() {
// No need to change any of this
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
}

void enableColor(String color) {
// No need to change any of this either
if (color.equals("red")) {
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(GREEN, LOW);
} else if (color.equals("yellow")) {
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
} else if (color.equals("green")) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(YELLOW, LOW);
}
}

void loop() {
// You can change the delay
enableColor("red");
delay(2000); // This is not 2000 seconds rather 2000 ms (2 seconds)
enableColor("green");
delay(2000);
enableColor("yellow");
delay(1000);
}

Well, this is the end of this tutorial. If you enjoyed this tutorial, please favorite it!


Need Help?

  1. Have you tried copying and pasting the code?
  2. Have you confirmed your wiring is correct?
  3. Have you reset your board?
  4. If you have done all of the above, please send me a message!

Downloads