Document 7
Document 7
Aim:
To Create IoT-Based Traffic Light System with Timing Using Embedded C
program.
Apparatus Required:
• Arduino Uno Microcontroller.
• Traffic Lights (LEDs).
• Pedestrian Button.
• Wiring Components.
• USB cable (for Arduino, ESP, or Raspberry Pi Pico).
Connection Diagram:
Procedure:
• Connect the LEDs (red, yellow, green) to the microcontroller's GPIO pins
through appropriate resistors, following the connection diagram.
• Install the necessary IDE (Arduino IDE, Platform IO, etc.) and libraries for your
chosen microcontroller.
• Observe the traffic lights: They should cycle through green, yellow, and red
according to the defined timings.
• Set up an IoT platform or MQTT broker to manage the data.
• Test the system thoroughly.
Program:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
// Define GPIO pins (replace with your actual pin assignments) #define
RED_LIGHT_1 2 // Example: GPIO pin 2 #define YELLOW_LIGHT_1 3 #define
GREEN_LIGHT_1 4 #define RED_LIGHT_2 5 #define YELLOW_LIGHT_2 6
#define GREEN_LIGHT_2 7 #define PEDESTRIAN_BUTTON 8 // Example:
GPIO pin for pedestrian button
return 0; }
void initialize_gpio() { // Initialize GPIO pins as output (for lights) and input (for
button) // Replace with your microcontroller's GPIO initialization code // Example
(using pseudo-code): // set_pin_mode(RED_LIGHT_1, OUTPUT); //
set_pin_mode(YELLOW_LIGHT_1, OUTPUT); //
set_pin_mode(GREEN_LIGHT_1, OUTPUT); // set_pin_mode(RED_LIGHT_2,
OUTPUT); // set_pin_mode(YELLOW_LIGHT_2, OUTPUT); //
set_pin_mode(GREEN_LIGHT_2, OUTPUT); //
set_pin_mode(PEDESTRIAN_BUTTON, INPUT); }
void set_light(int pin, bool state) { // Set the specified GPIO pin to the given state
(true = on, false = off) // Replace with your microcontroller's GPIO output code //
Example (using pseudo-code): // write_pin(pin, state); if(state){ printf("Pin %d
HIGH\n", pin); } else { printf("Pin %d LOW\n", pin); } }
bool read_button(int pin) { // Read the state of the specified GPIO pin (for the
button) // Replace with your microcontroller's GPIO input code // Example (using
pseudo-code): // return read_pin(pin); // This example simulates a button press
every 10 cycles. static int cycleCount = 0; cycleCount++; if(cycleCount % 10 ==
0){ return true; } return false; }
void pedestrian_override() {
Thus the IoT-Based Traffic Light System with Timing project has been done
and tested Successfully.