4.11) Case Study: Traffic Light Controller using ATmega16

posted by Hamid Sayyed • November 14, 2025 0 Comments

Traffic light controllers are one of the most practical examples of real-time embedded systems used in cities across the world. They help regulate vehicle movement, reduce congestion, and improve road safety by following a proper red–yellow–green sequence. Using the ATmega16 microcontroller, we can build a simple and reliable traffic light controller to understand how timing, sequencing, and output control work in embedded applications. This experiment helps students learn how to configure I/O pins, generate accurate delays, and design a complete state-based sequence that mimics real traffic operation. By studying this system step-by-step, beginners get a clear idea of how embedded controllers manage real-time tasks, and how similar concepts are applied in more advanced smart traffic and automation systems

Understanding Traffic Light System

A typical intersection uses three main lights:

  • Red — Stop
  • Yellow — Wait / Ready
  • Green — Go

The controller must ensure:

  • No two directions get green at the same time.
  • Each light follows the order: Green → Yellow → Red.
  • Each state has a fixed delay duration.
Note: In real traffic systems, sensors, timers, emergency vehicle detection, and pedestrian crossings are also included. But for basic embedded design, we focus on fixed timing control.

Hardware Requirements

  • ATmega16 microcontroller
  • 9 LEDs: Red, Yellow, Green for each road
  • 220Ω resistors (for current limiting)
  • Power supply 5V
  • Breadboard and jumper wires

Pin Configuration

In this project, we use PORTA and PORTB to drive the LEDs.

LED SignalATmega16 Pin
Road A RedPA0
Road A YellowPA1
Road A GreenPA2
Road B RedPB0
Road B YellowPB1
Road B GreenPB2

Traffic Light Timing Logic

The system follows four main states:

  • State 1: Road A = Green, Road B = Red
  • State 2: Road A = Yellow, Road B = Red
  • State 3: Road A = Red, Road B = Green
  • State 4: Road A = Red, Road B = Yellow
Typical Delays:
Green = 5 seconds
Yellow = 2 seconds
Red = Opposite road's green + yellow

C Program for ATmega16 — Traffic Light Controller

#include <avr/io.h>
#include <util/delay.h>

void roadA_green() {
    PORTA = (1<<PA2); // Green ON
    PORTB = (1<<PB0); // Road B Red
}

void roadA_yellow() {
    PORTA = (1<<PA1); // Yellow ON
    PORTB = (1<<PB0);
}

void roadB_green() {
    PORTB = (1<<PB2); // Green ON
    PORTA = (1<<PA0); // Road A Red
}

void roadB_yellow() {
    PORTB = (1<<PB1); // Yellow ON
    PORTA = (1<<PA0);
}

int main(void)
{
    DDRA = 0x07;  // PA0,PA1,PA2 as output
    DDRB = 0x07;  // PB0,PB1,PB2 as output

    while(1)
    {
        // State 1: Road A Green
        roadA_green();
        _delay_ms(5000);

        // State 2: Road A Yellow
        roadA_yellow();
        _delay_ms(2000);

        // State 3: Road B Green
        roadB_green();
        _delay_ms(5000);

        // State 4: Road B Yellow
        roadB_yellow();
        _delay_ms(2000);
    }
}
  

Animated Traffic Light Diagram

The following animated diagram shows how the ATmega16 controls two road signals. As the program runs, both traffic lights switch between Red, Yellow and Green exactly like a real intersection.

Applications

  • Real-time traffic signal design
  • Smart city automation
  • Mini-projects and engineering labs
  • AVR based embedded system learning

Conclusion

Designing a traffic light controller using ATmega16 helps beginners understand real-time embedded logic, state machines, I/O programming, and timing functions. The same concept can be extended to more roads, sensors, pedestrian crossing systems, or adaptive traffic management systems. With this foundation, learners can step into building more advanced real-world smart automation projects.

Comments

Post a Comment

Subscribe to Post Comments [Atom]