How to Make a Simple Arduino Traffic Light Controller With Infrared Sensors

Introduction

Making a traffic light controller with an Arduino is a fun electronics project that allows you to learn about microcontrollers, circuits, and programming. In this guide, I will show you how I built a simple Arduino traffic light using infrared sensors to detect cars and trigger the lights to change.

What You Will Need

To build this project, you will need:

I used common through-hole 5mm LEDs along with the TSOP1838 infrared receiver and transmitter pair. Any similar components will also work.

The Circuit

The circuit for this project is relatively simple.

The infrared LED transmitter is connected to Arduino pin 3.

The infrared receiver is connected to Arduino pin 2. The output pin of the receiver connects to Arduino pin 4.

The red, yellow and green LEDs connect through 220 ohm resistors to Arduino pins 5, 6 and 7 respectively.

Here is a schematic showing how everything is connected on the breadboard:

How to Make a Simple Arduino Traffic Light Controller With Infrared Sensors

Schematic diagram for the Arduino traffic light controller.

The Code

The Arduino code uses the infrared sensors to detect when a car is waiting at the intersection and triggers the LEDs to change like real traffic lights.

It starts by setting pins 2 and 4 as inputs to read the infrared receiver module. The LED pins are outputs.

In the loop(), it first checks if the receiver detects the transmitter signal. If it does, it means a car is waiting and it runs the changeLights() function.

cpp
if (digitalRead(receiver) == HIGH) {
changeLights();
}

The changeLights() function runs through the sequence to change the LEDs from green to yellow, yellow to red, and then back to green. It uses delays to mimic real traffic light timing.

```cpp
// Green to yellow
digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, HIGH);
delay(3000);

// Yellow to red
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, HIGH);
delay(1000);

// Red to green
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
delay(5000);
```

The full code can be found on GitHub.

Putting It Together

Once you have everything wired up on the breadboard and the code uploaded to the Arduino, its time to test it out!

Place the infrared transmitter and receiver on opposite sides of the breadboard. The transmitter needs to face the receiver for the signal to be detected.

When power is applied to the Arduino, the green LED should turn on to indicate the traffic light is working.

Now take the infrared transmitter module and power it by briefly connecting the Vcc and ground pins. This simulates a car arriving at the intersection.

You should see the lights transition from green to yellow to red, and then back to green!

To simulate additional cars, power the IR transmitter on and off. Each time it detects the signal, the lights sequence through their cycle.

Going Further

Here are some ways you can build on this simple traffic light controller:

The Arduino is a very versatile platform and this project can serve as the foundation for all kinds of more advanced traffic simulations. Be creative and have fun expanding the capabilities!

Summary

Creating an Arduino controlled traffic light is an enjoyable electronics project that teaches useful skills. This guide showed you how to build a simple system using an Arduino Uno, infrared transmitters/receiver, LEDs and basic code.

With the information in this article, you should now be able to construct your own intersection traffic light controller and customize it in any way you like. The Arduino has tons of possibilities so put your imagination to work!