This is Our Blog

July 15, 2015
|

Traffic Signal (Arduino)

In this tutorial we are going to create a simple Traffic Signal project. We will configure circuit first and programming in unity later.

Configure Circuit

Requirement

  • One Arduino uno board
  • One prototyping breadboard
  • Three LEDs – Red, Yellow and Green
  • Six Prototyping wires
  • Three Resistors (330 or 220 OHM)

We will use three output pins on UNO Board to control all three LEDs.

Pin 13 – Red LED

Pin 12 – Yellow LED

Pin 8 – Green LED

Let us learn how to build a LED in breadboard and UNO board. We will build Red Led to Pin 13.

Building LED to breadboard and UNO board

  1. LED longer pin is (+)


  2. Add Resistor to (+) point of LED in breadboard.


    Note: Always put Resistor on (+) Positive point of LED. As shown in above image.

  3. Connect prototyping wires :

    LED (-) Negative point – Ground (GND) Pin

    LED (+) Positive point – Output Pin (13)


In above image, White wire is connected to GRD Pin in uno board and (+) Point of LED in breadboard. Red Wire is connected to Pin13 in uno board and Resistor input point in bread board.

Now you build circuit with one LED.

Repeat all steps of “Building LED to breadboard and UNO board” for rest two LEDs Yellow and Green. Use

Pin 12 – Yellow LED

Pin 8 – Green LED

Programming in unity

Note: – If you are newer to Arduino Unity, please read my blog first “How to setup Arduino board in unity”

http://wordpress-devinit-resources-india.azurewebsites.net/how-to-setup-arduino-board-in-unity/


  1. Create a new Unity project and Import the Uniduino package into your project. You can buy asset from https://www.assetstore.unity3d.com/en/#!/content/6804
  2. Create a new folder inside Asset named “Traffic Signal”.

  3. Create a new scene. Save it to “Traffic Signal” folder with “scnTraffic” name.

    Add the Uniduino prefab from the Uniduino/Prefabs folder.

    Select Uniduino object à Arduino (Script) à Enter Port Name = COM4.


    Note: – Verify that the port name is the same as the one you selected in the IDE.

  1. Create a C# script inside “Traffic Signal” named scrTraffic.

    Double click on script to open in Editor. Add new a namespace to your class.

    using Uniduino;
    

    Replace your class body with below codes.

    public Arduino arduino;
    public int red_Pin; //13
    public int green_Pin; //8
    public int yellow_Pin; //12
    // Use this for initialization
    void Start()
    {
        arduino = Arduino.global;
        arduino.Setup(ConfigurePins);
        StartCoroutine(BlinkLoops());
    }
    void ConfigurePins()
    {
        arduino.pinMode(red_Pin, PinMode.OUTPUT);
        arduino.pinMode(green_Pin, PinMode.OUTPUT);
        arduino.pinMode(yellow_Pin, PinMode.OUTPUT);
    }
    private string lightStatus = "Red";
    IEnumerator BlinkLoops()
    {
        while (true)
        {
            switch (lightStatus)
            {
                case "Red":
                    {
                        arduino.digitalWrite(red_Pin, Arduino.HIGH);
                        arduino.digitalWrite(yellow_Pin, Arduino.LOW);
                        arduino.digitalWrite(green_Pin, Arduino.LOW);
    
                        lightStatus = "Yellow";
                        yield return new WaitForSeconds(2f);
                        break;
                    }
                case "Yellow":
                    {
                        arduino.digitalWrite(red_Pin, Arduino.LOW);
                        arduino.digitalWrite(yellow_Pin, Arduino.HIGH);
                        arduino.digitalWrite(green_Pin, Arduino.LOW);
    
                        lightStatus = "Green";
                        yield return new WaitForSeconds(1f);
                        break;
                    }
                case "Green":
                    {
                        arduino.digitalWrite(red_Pin, Arduino.LOW);
                        arduino.digitalWrite(yellow_Pin, Arduino.LOW);
                        arduino.digitalWrite(green_Pin, Arduino.HIGH);
    
                        lightStatus = "Red";
                        yield return new WaitForSeconds(4f);
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
    
    1. Attach scrTraffic script to your Main Camera Game object.

    Assign Values to scrTraffic scipt from unity scene.

red_Pin = 13

green_Pin = 8

yellow_Pin = 12


  1. Now hit the Play button.

Now your circuit should be flashing LEDs.

First Red LED then Yellow LED then Green LED, will repeat unlimited times while your scene runs.


Thank you for reading my blog. If you have any query, you can write me mail on neeraj.singh@devinit.ch , it would help us a lot.

Tags:


0 Comments


Would you like to share your thoughts?

Would you like to share your thoughts?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.