Arduino Project

Traffic Light (IOT)

Course Introduction

This Arduino project uses Arduino IoT Remote to control Traffic Light.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

Please follow the steps in the tutorial below to complete the binding and setup of Arduino Cloud and the Arduino WiFi board.
1.4 Arduino IoT Cloud

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Wifi

1

BUY

2

USB Type-C cable

1

×

3

Breadboard

1

4

Wires

Several

5

Traffic Light LED

1

Wiring

traffic_light_iot.webp__PID:3689f4c9-cb81-4823-956c-66e7ad84e364

Common Connections:

Traffic light LED
R: Connect to 3 on the Arduino.
Y: Connect to 5 on the Arduino.
G: Connect to 6 on the Arduino.
GND: Connect to breadboard’s negative power bus.

Create a New IoT Project
After configuring the Arduino Cloud and the Arduino WiFi board, follow the steps below to complete the Arduino Cloud project setup

1.webp__PID:c6495e14-62b1-495b-81b6-ce255495c34a

Edit Value (4Variables:cmdGreen,cmdYellow,cmdRed,currentState)

2.png__PID:489957fa-8ebf-4459-9eb4-b5d689d5910a12.webp__PID:78413679-5f4c-4cb5-9a61-8c30e2329d0c

Follow the steps below to configure the dashboard.
1.Create New Dashboard

6.webp__PID:da293eac-eee8-44a0-a339-f878cbcacd32

2.Add Widgets

13.png__PID:ec450f9c-d030-40d3-92ed-6e86b09ab63b

3.Link Variable

13.webp__PID:cedf2463-7e32-4de4-8424-6390fb2bac6e14.png__PID:659fe758-1c2d-4f16-b517-61a5a6978678

4.Remember to click Done

15.png__PID:1591ea39-f699-4aa3-b874-fdc850f4a762

After completing the above configuration, return to the Things page and open the sketch.

15.webp__PID:8a4132d0-c32b-4089-af88-a78b2bd36a5c

When you have completed the configuration of the Things and Dashboard, as well as the connection and network setup of the Arduino WiFi board, the thingProperties.h and Sketch Secrets files will be generated automatically. If Sketch Secrets is not generated, please manually enter the connected SSID and OPTIONAL_PASS

Copy this code into Arduino Cloud.

111.webp__PID:95140091-b504-439c-9203-f7078c4d2368

Don’t forget to select the board(Arduino UNO R4 WIFI) and the correct port before clicking the Upload button.


#include "thingProperties.h"

// ========== Hardware configuration ==========
const uint8_t PIN_RED    = 3;
const uint8_t PIN_YELLOW = 5;
const uint8_t PIN_GREEN  = 6;

// Common cathode (to GND) → true; Common anode (to 5V) → false
const bool ACTIVE_HIGH = true;
// ============================================

enum LightState : int { RED = 0, YELLOW = 1, GREEN = 2, ALL_OFF = 3 };

inline void writeLamp(uint8_t pin, bool on) {
  if (ACTIVE_HIGH) digitalWrite(pin, on ? HIGH : LOW);
  else             digitalWrite(pin, on ? LOW  : HIGH);
}

void setLights(LightState s) {
  writeLamp(PIN_RED,    s == RED);
  writeLamp(PIN_YELLOW, s == YELLOW);
  writeLamp(PIN_GREEN,  s == GREEN);
  currentState = static_cast(s);  // Report back to cloud
}

void setup() {
  Serial.begin(115200);
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_YELLOW, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);

  // Turn all off at startup
  setLights(ALL_OFF);

  // Connect to IoT Cloud
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
}

// ========== Cloud button callbacks ==========
void onCmdRedChange() {
  if (cmdRed) {
    setLights(RED);
    // Reset all three buttons to avoid keeping them “on” in the dashboard
    cmdRed = false; cmdYellow = false; cmdGreen = false;
  }
}

void onCmdYellowChange() {
  if (cmdYellow) {
    setLights(YELLOW);
    cmdRed = false; cmdYellow = false; cmdGreen = false;
  }
}

void onCmdGreenChange() {
  if (cmdGreen) {
    setLights(GREEN);
    cmdRed = false; cmdYellow = false; cmdGreen = false;
  }
}

/*
  Since Angle3 is READ_WRITE variable, onAngle3Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onAngle3Change()  {
  // Add your code here to act upon Angle3 change
}
/*
  Since CurrentState is READ_WRITE variable, onCurrentStateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onCurrentStateChange()  {
  // Add your code here to act upon CurrentState change
}