A Four Way Intersection Traffic Light Using STM32

Designed traffic lights for 4 intersections using simulation with STM32 microcontroller

BeginnerShowcase (no instructions)3 days282
A Four Way Intersection Traffic Light Using STM32

Things used in this project

Hardware components

Traffic Light LED
×1
STM32F401CB
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Printed Circuit Board
×1
Traffic Light Poles
×1

Software apps and online services

Proteus 8 Professionals
Keil uVision5

Hand tools and fabrication machines

Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter
Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Drill / Driver, Cordless
Drill / Driver, Cordless

Story

Read more

Schematics

Circuit Diagrams

Schematics

Code

A Four Way Intersection Traffic Light Using STM32 Code

C/C++
The code can be copied and pasted into Keil uVision5 software
#include "stm32f4xx.h"
#include <stdint.h>

// Preset delay based on clock
#define DELAY_1S 400000U    // clock speed 40MHz
#define D20 (20 * DELAY_1S) // 20 seconds delay for green
#define D05 (5 * DELAY_1S)  // 5 seconds delay for yellow transition
#define D1 (1 * DELAY_1S)   // 1 second safety delay

void delay(volatile uint32_t time) {
    while(time--);
}

void GPIO_Init(void) {
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // For GPIOA AND GPIOB
    GPIOA->MODER |= 0x555555; // PA0-PA11 output
}

void All_Off(void) {
    GPIOA->ODR &= ~(0xFFF); // Clear bits output 1-11
}

void SetLight(uint8_t red, uint8_t yellow, uint8_t green, char state) {
    GPIOA->ODR &= ~((1 << red) | (1 << yellow) | (1 << green));
    if (state == 'R') GPIOA->ODR |= (1 << red);    // Red
       else if (state == 'Y') GPIOA->ODR |= (1 << yellow); // Yellow
       else if (state == 'G') GPIOA->ODR |= (1 << green);  // Green
}

void SetAll(char Traffic_1, char Traffic_2, char Traffic_3, char Traffic_4) {
    SetLight(0, 1, 2, Traffic_1); // For Traffic light 1: PA0, PA1, PA2  ( southbound lights )
    SetLight(3, 4, 5, Traffic_2); // For Traffic light 2: PA3, PA4, PA5  ( westbound  lights )
    SetLight(6, 7, 8, Traffic_3); // For Traffic light 3: PA6, PA7, PA8  ( eastbound  lights )
    SetLight(9,10,11, Traffic_4); // For Traffic light 4: PA9, PA10,PA11 ( northbound lights )
	
	  // 0,3,6,9 for red light , 1,4,7,10 for yellow light, 2,5,8,11 for green light.
}

int main(void) {
    GPIO_Init();

    while (1) {
        // STEP 1: Traffic_1 green, others red
        SetAll('G','R','R','R'); 
        delay(D20);          // Traffic_1 stays green for 20s
        
        // Transition for Traffic  light_1 directly to red, Traffic_2 to yellow
        SetAll('R','Y','R','R');
        delay(D05);          // Traffic_2 yellow for 5s
        
        // STEP 2: Traffic_2 green, others red
        SetAll('R','G','R','R');
        delay(D20);          // Traffic_2 stays green for 20s
        
        // Transition for Traffic light_2 directly to red, Traffic_3 to yellow
        SetAll('R','R','Y','R');
        delay(D05);          // Traffic_3 yellow for 5s
        
        // STEP 3: Traffic_3 green, others red
        SetAll('R','R','G','R');
        delay(D20);          // Traffic_3 stays green for 20s
        
        // Transition for Traffic light_3 directly to red, Traffic_4 to yellow
        SetAll('R','R','R','Y');
        delay(D05);          // Traffic_4 yellow for 5s
        
        // STEP 4: Traffic_4 green, others red
        SetAll('R','R','R','G');
        delay(D20);          // Traffic_4 stays green for 20s
        
        // Transition for Traffic_4 directly to red, Traffic_1 to yellow
        SetAll('Y','R','R','R');
        delay(D05);          // Traffic_1 yellow for 5s
    }
}

Credits

Hastina Prayuda Wardani
1 project • 8 followers
Goldy Amsana Rachmanio
1 project • 5 followers
Electrical engineering student exploring IoT & cybersecurity, started with Arduino projects, now learning and building new skills.
Ramadhanil Saputra
1 project • 5 followers
M Zacky Alqifahry
1 project • 7 followers
Electrical engineering student..
M.Tanuja Hasar
1 project • 3 followers
shasha Lucyani
1 project • 3 followers
Feby Aulia
1 project • 3 followers
Fikri Ikhsan
1 project • 1 follower

Comments