Arduino - Traffic light controller


This easy project will show how to make traffic light controller based on Arduino Pro Mini.
Hardware needed are some LED and resistors
Hardware:
1. Arduino Pro Mini
2. LED: green, red, yellow
3. Resistor




Step 1. Make circuit
Simple circuit with some LED, resistor connected to Arduino Pro Mini like this:


Step 2. Code works
Main purpose of code is light on RED, hold it in delay time (this example set 3s) -> then off RED, on GREEN, hold it -> then GREEN off, on YELLOW
Some people don't know why they need YELLOW traffic light. I guess YELLOW light is used for those who last seeing GREEN light can go out of street.

const byte pin_green = 7;   // light green
const byte pin_yellow = 8;  // light yellow
const byte pin_red = 9;     // light red

void setup() {
  pinMode(pin_green,OUTPUT);
  pinMode(pin_yellow,OUTPUT);
  pinMode(pin_red,OUTPUT);
  // start serial port at 9600 bps:
  Serial.begin(9600);
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  digitalWrite(pin_red,1);
  digitalWrite(pin_green,0);
  digitalWrite(pin_yellow,0);
  delay(3000);  //delay 3s
  digitalWrite(pin_red,0);
  digitalWrite(pin_green,1);
  digitalWrite(pin_yellow,0);
  delay(3000);  //delay 3s
  digitalWrite(pin_red,0);
  digitalWrite(pin_green,0);
  digitalWrite(pin_yellow,1);
  delay(1000);  //delay 1s
}

Step 3. Test run video











0 Comments: