0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for Traffic Light Controller

An Embedded C program for a traffic light controller uses GPIO pins to turn on red, yellow, and green lights in sequence with delays, for example: RED ON for 5s, GREEN ON for 5s, YELLOW ON for 2s in a loop.
📋

Examples

InputRun traffic light sequence once
OutputRed light ON for 5 seconds Green light ON for 5 seconds Yellow light ON for 2 seconds
InputRun traffic light sequence continuously
OutputRed light ON for 5 seconds Green light ON for 5 seconds Yellow light ON for 2 seconds (repeats)
InputChange timing to Red=3s, Green=4s, Yellow=1s
OutputRed light ON for 3 seconds Green light ON for 4 seconds Yellow light ON for 1 second
🧠

How to Think About It

To design a traffic light controller, think of three lights: red, yellow, and green. Each light turns ON for a fixed time, then turns OFF before the next light turns ON. Use a loop to repeat this cycle forever. Use delay functions to keep each light ON for the right time.
📐

Algorithm

1
Initialize the pins connected to red, yellow, and green lights as outputs
2
Turn ON the red light and keep it ON for the red light duration
3
Turn OFF the red light and turn ON the green light for the green light duration
4
Turn OFF the green light and turn ON the yellow light for the yellow light duration
5
Turn OFF the yellow light
6
Repeat the cycle from step 2
💻

Code

embedded_c
#include <stdio.h>
#include <unistd.h> // for sleep function

void red_light_on() { printf("Red light ON\n"); }
void red_light_off() { printf("Red light OFF\n"); }
void green_light_on() { printf("Green light ON\n"); }
void green_light_off() { printf("Green light OFF\n"); }
void yellow_light_on() { printf("Yellow light ON\n"); }
void yellow_light_off() { printf("Yellow light OFF\n"); }

int main() {
    while(1) {
        red_light_on();
        sleep(5); // red for 5 seconds
        red_light_off();

        green_light_on();
        sleep(5); // green for 5 seconds
        green_light_off();

        yellow_light_on();
        sleep(2); // yellow for 2 seconds
        yellow_light_off();
    }
    return 0;
}
Output
Red light ON Red light OFF Green light ON Green light OFF Yellow light ON Yellow light OFF Red light ON Red light OFF Green light ON Green light OFF Yellow light ON Yellow light OFF ...
🔍

Dry Run

Let's trace one full cycle of the traffic light controller through the code

1

Turn on red light

Red light ON printed, program waits 5 seconds

2

Turn off red light

Red light OFF printed

3

Turn on green light

Green light ON printed, program waits 5 seconds

4

Turn off green light

Green light OFF printed

5

Turn on yellow light

Yellow light ON printed, program waits 2 seconds

6

Turn off yellow light

Yellow light OFF printed

StepLight StateActionDelay (seconds)
1Red ONPrint 'Red light ON'5
2Red OFFPrint 'Red light OFF'0
3Green ONPrint 'Green light ON'5
4Green OFFPrint 'Green light OFF'0
5Yellow ONPrint 'Yellow light ON'2
6Yellow OFFPrint 'Yellow light OFF'0
💡

Why This Works

Step 1: Control lights with functions

Each light has ON and OFF functions that simulate turning the light on or off by printing messages.

Step 2: Use delays to keep lights on

The sleep() function pauses the program to keep each light on for the correct time.

Step 3: Repeat cycle forever

The while(1) loop repeats the sequence endlessly to simulate continuous traffic light operation.

🔄

Alternative Approaches

Using timer interrupts
embedded_c
#include <stdio.h>
// Pseudocode: Setup timer interrupt to change lights
// ISR changes light state every fixed interval
int main() {
  // Initialize timer and lights
  while(1) {
    // Main loop does nothing, ISR handles lights
  }
  return 0;
}
This method uses hardware timers and interrupts for precise timing but is more complex to implement.
Using state machine
embedded_c
#include <stdio.h>
#include <unistd.h>
enum State {RED, GREEN, YELLOW};
int main() {
  enum State state = RED;
  while(1) {
    if(state == RED) { printf("Red ON\n"); sleep(5); state = GREEN; }
    else if(state == GREEN) { printf("Green ON\n"); sleep(5); state = YELLOW; }
    else { printf("Yellow ON\n"); sleep(2); state = RED; }
  }
  return 0;
}
Using a state machine makes the code clearer and easier to extend for more complex logic.

Complexity: O(1) time, O(1) space

Time Complexity

The program runs in an infinite loop with fixed delays; no input size affects runtime, so time complexity is constant O(1).

Space Complexity

Uses a fixed number of variables and no dynamic memory, so space complexity is constant O(1).

Which Approach is Fastest?

All approaches run in real time with delays; using interrupts can be more efficient for CPU usage but more complex.

ApproachTimeSpaceBest For
Simple loop with delaysO(1)O(1)Easy to understand and implement
Timer interruptsO(1)O(1)Precise timing and efficient CPU use
State machineO(1)O(1)Clear logic and easy to extend
💡
Use a loop with delays to cycle through lights and keep timing simple.
⚠️
Beginners often forget to turn OFF the previous light before turning ON the next one, causing multiple lights ON simultaneously.