Arduino Pedestrian Crossing Traffic Lights

Arduino Pedestrian Crossing Traffic Lights © CC BY-SA

Arduino based traffic lights using car lights delay to enable safe pedestrian crossing.

  • 5,785 views
  • 0 comments
  • 21 respects

Components and supplies

Apps and online services

About this project

This is an educational project for beginners. It’s a traffic light, but definitely not a simple one. This project features a pedestrian traffic light and a regular traffic light working together. There is also a display showing the pedestrian when the lights will turn red again. A push button was added to receive a request when the pedestrian wants to cross the street. Here is the tricky part. If a pedestrian makes a request and crosses the street, the car lights will turn red. Only 30 seconds later, if a pedestrian makes another request, the pedestrian lights will turn green again.

Code

Arduino Pedestrian Traffic Lights CodeArduino
//leds for both traffic lights
int sinal_pedestre[2] = {A1, A0};
int sinal_carros[3] = {A2, A3, A4};
//pushbuttonpin
byte botao = 12;
//auxiliar time variables
unsigned long t_0;
unsigned long t_1;
unsigned long t_aux;
unsigned int safe_time;
//Waiting time for a request just after the traffic lights of the cars open
unsigned int t = 4000;

//matrix of the display of 7 segments
int disp[10][8] = { { 0, 1, 1, 1, 1, 1, 1, 0 }, // = Digit 0
  { 0, 1, 0, 0, 1, 0, 0, 0 }, // = Digit 1
  { 0, 0, 1, 1, 1, 1, 0, 1 }, // = Digit 2
  { 0, 1, 1, 0, 1, 1, 0, 1 }, // = Digit 3
  { 0, 1, 0, 0, 1, 0, 1, 1 }, // = Digit 4
  { 0, 1, 1, 0, 0, 1, 1, 1 }, // = Digit 5
  { 0, 1, 1, 1, 0, 1, 1, 1 }, // = Digit 6
  { 0, 1, 0, 0, 1, 1, 0, 0 }, // = Digit 7
  { 0, 1, 1, 1, 1, 1, 1, 1 }, // = Digit 8
  { 0, 1, 1, 0, 1, 1, 1, 1 }  // = Digit 9
};

void setup() {

  //PinMode of display pins
  for (int i = 2; i < 9; i++)
  {
    pinMode(i, OUTPUT);
  }
  //PinMode of leds pins
  for (int i = A0; i < A5; i++)
  {
    pinMode(i, OUTPUT);
  }
  ////PinMode of pusbutton pin
  pinMode(botao, INPUT);
  t_0 = millis();
}

void loop() {

  //Show the S of stop at the beginning
  number_display(5);

  //conting the minimum time that the traffic light will be green for the cars
  t_1 = millis();
  if (t_1 - t_0 < t) {
    safe_time = t_1 - t_0;
  }

  //green sign for cars and red for pedestrians
  digitalWrite(sinal_carros[2], HIGH);
  digitalWrite(sinal_pedestre[0], HIGH);
  //while no one makes a request, the signal will not change.
  if (digitalRead(botao) == HIGH)
  {

    delay(8000 - safe_time);

    //red light for cars goes out and yellow turns on for 2.5 seconds
    digitalWrite(sinal_carros[2], LOW);
    digitalWrite(sinal_carros[1], HIGH);

    delay(2500);

    //Pedestrians sign opens
    digitalWrite(sinal_carros[1], LOW);
    digitalWrite(sinal_carros[0], HIGH);
    digitalWrite(sinal_pedestre[0], LOW);
    digitalWrite(sinal_pedestre[1], HIGH);

    //Countdown to the pedestrian signal close
    t_0 = millis();
    t_aux = 0;
    while (t_aux < 5000) {
      idle_display();
      t_aux = millis() - t_0;
    }

    for (int k = 9; k > 0; k--) {
      number_display(k);

      delay(1000);
    }
    //the signals are turned off for the cycle to start again
    digitalWrite(sinal_carros[0], LOW);
    digitalWrite(sinal_pedestre[1], LOW);

    t_0 = millis();
  }

}
//function that shows a number in the 7 segment display

void number_display(int m) {

  for (int j = 2; j < 9; j++) {
    digitalWrite(j, disp[m][j - 1]);
  }

}

void idle_display() {
  turnOf_display();
  digitalWrite(3, 1);
  delay(150);
  digitalWrite(3, 0);
  digitalWrite(8, 1);
  delay(150);
  digitalWrite(8, 0);
  digitalWrite(6, 1);
  delay(150);
  digitalWrite(6, 0);
  delay(150);

}

void turnOf_display() {
  for (int i = 2; i < 9; i++) {
    digitalWrite(i, 0);
  }
}

Schematics

Fritzing
sinal_OomdLukQin.fzz

Comments

Similar projects you might like

Fade Lights and Push Button

Project tutorial by Phuong Vo

  • 4,531 views
  • 3 comments
  • 11 respects

Traffic Lights Controlled by Arduino

Project showcase by lmsousa

  • 6,639 views
  • 2 comments
  • 19 respects

Traffic Monitor- Monitors traffic on the Go

Project tutorial by Patel Darshil

  • 5,594 views
  • 0 comments
  • 11 respects

2-Way Intersection with Pedestrian Walk Cycle

Project showcase by FalcomDigital

  • 3,859 views
  • 2 comments
  • 8 respects

Railroad Crossing Lights

Project showcase by eldo85

  • 3,474 views
  • 5 comments
  • 7 respects
Add projectSign up / Login