PROJECT Nº 5: Traffic light with crossing gate

Learn to build and program a traffic light with a crossing gate using the components of the Maker Control Kit and Maker Kit 3.

Create a traffic light that lifts the crossing gate and lets you through while the light is green, and that lowers the gate and stops the flow of traffic when the light is red. It’s that easy!

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 60 min.

MATERIALS:

  • 1 Servomotor
  • 1 Green LED
  • 1 Yellow LED
  • 1 Red LED
  • 1 4in1 Build&Code board
  • 1 USB – Micro USB cable
  • 1 Computer
  • Multicolored wooden sticks
  • Adhesive or tape

What is a servomotor?

An electric motor is a machine that converts electrical energy into continual, rotating mechanical energy. A servomotor is a direct current motor that, instead of making a continual turn, is designed to make it turn a certain number of degrees in response to a control signal and stay fixed in that position.

BUILDING THE STRUCTURE:

To assemble the traffic light and crossing gate structure, you will use multicolored wooden sticks. With the sticks, you will make the base and the pole, to which the LEDs, the servomotor, and the crossing gate, which is fastened to the shaft of the servomotor, will be attached. Attach the sticks and the electronic components with tape or adhesive.

Download the quick assembly guide and follow the steps given to set up the structure.

Once the structure is assembled, connect all the electronic elements to the 4in1 Build&Code board as shown in the section on connections found below.

CONNECTIONS:

  1. Connect the green LED to digital port 4 on the 4in1 Build&Code board.
  2. Connect the yellow LED to digital port 3 on the 4in1 Build&Code board.
  3. Connect the red LED to digital port 2 on the 4in1 Build&Code board.
  4. Connect the servomotor to digital port 9 on the 4in1 Build&Code board.

Look at the colors of the cables and the colors of the terminals on the 4in1 Build&Code board to guide you. Each cable should be connected to its color.

PROGRAMMING CODE:

The program consists of developing an on and off sequence of the LEDs, imitating the sequence of traffic lights.

The sequence consists of turning on the green LED for 5 seconds, while the other LEDs are off. After this time is up, the yellow LED will turn on while the rest of the LEDs stay off for 1.5 seconds. Finally, the red LED will turn on while the rest of the LEDs will stay off.

The crossing gate acts in two ways: when the green LED and the yellow LED are on, the gate will be up, and when the red LED is on, the gate will be down.

The two sequences will repeat as long as the whole system has a power supply.

You can do this activity by using Arduino and Bitbloq software, as well as other compatible block programming software. Below you will find the programming code needed for each software.

Arduino Code

  1. Download the Arduino software and go through the installation process.
  2. Open the Arduino program, and once in, copy the following program:
    #include <Servo.h>
    Servo motor2; // DECLARE SERVOMOTOR
    int LEDRed = 2, LEDYellow = 3, LEDGreen = 4; // DIGITAL PORTS OF CONNECTION FOR THE LEDS.
    void setup() {
      // put your setup code here, to run once:
      motor2.attach (9); // DIGITAL PORT PWM 9 WHERE THE SERVOMOTOR IS CONNECTED
      // CONFIGURE THE DIGITAL PORTS OF THE LEDS AS OUTPUT SIGNAL.
      pinMode(LEDRed, OUTPUT); 
      pinMode(LEDYellow, OUTPUT);
      pinMode(LEDGreen, OUTPUT);
    }
    
    void loop() {
      // Put your main code here, to run repeatedly:
      // TRAFFIC LIGHT GREEN AND GATE UP FOR 5 SEC
      digitalWrite (LEDRed, LOW);
      digitalWrite (LEDYellow, LOW);
      digitalWrite (LEDGreen, HIGH);
      motor2.write (45); // GATE UP (OPEN)
      delay(5000);
      // TRAFFIC LIGHT YELLOW FOR 1.5 SEC
      digitalWrite (LEDRed, LOW);
      digitalWrite (LEDYellow, HIGH);
      digitalWrite (LEDGreen, LOW);
      delay(1500);
      // TRAFFIC LIGHT RED AND GATE DOWN FOR 3 sec
      digitalWrite (LEDRed, HIGH);
      digitalWrite (LEDYellow, LOW);
      digitalWrite (LEDGreen, LOW);
      motor2.write (160); // GATE DOWN (CLOSE)
      delay(3000);
    }
    
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

Code for compatible block programming software

  1. Download the software and go through the installation process.
  2. Open the program, and once in, copy the following code:
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

BitBloq Code

  1. Get the BitBloq software.
  2. Open the BitBloq program, and once in, copy the following code:
    • Hardware
    • Software
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

RESULT OF THE EXERCISE:

The functioning of the lights of this traffic light is the same as that of a standard traffic light. The crossing gate acts with the traffic lights: when the green and yellow lights are on, the gate will be up, and when the light is red the gate will be down. Now you have your own traffic light with a crossing gate!

1 0

0