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
Wiring

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

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


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

2.Add Widgets

3.Link Variable


4.Remember to click Done

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

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.

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
}
