Arduino Nano 33 IoT - Traffic Light

In this lesson, we will learn how to use the Arduino Nano 33 IoT board to control a traffic light kit. Specifically, we will cover:

Arduino Nano 33 IoT traffic light

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Traffic Light Module
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Traffic Light Module

Pinout

A traffic light module has 4 pins.

  • GND pin: This is the ground. Connect it to the ground (GND) on your Arduino Nano 33 IoT.
  • R pin: This pin controls the red light. Connect it to a digital output on your Arduino Nano 33 IoT.
  • Y pin: This pin controls the yellow light. Connect it to a digital output on your Arduino Nano 33 IoT.
  • G pin: This pin controls the green light. Connect it to a digital output on your Arduino Nano 33 IoT.
Traffic Light Pinout

How It Works

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT traffic light

This image is created using Fritzing. Click to enlarge image

How To Program For Traffic Light module

  • Set up the pins on the Arduino Nano 33 IoT board as digital outputs using the [pinMode()] function.
pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);
  • Program to switch on the red light using the digitalWrite() function.
digitalWrite(PIN_RED, HIGH); // Activate the red LED digitalWrite(PIN_YELLOW, LOW); // Deactivate the yellow LED digitalWrite(PIN_GREEN, LOW); // Deactivate the green LED delay(RED_TIME); // Wait for the red LED to remain active for the specified duration

Arduino Nano 33 IoT Code

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-traffic-light */ #define PIN_RED 3 // The Arduino Nano 33 IoT pin connected to R pin of traffic light module #define PIN_YELLOW 4 // The Arduino Nano 33 IoT pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The Arduino Nano 33 IoT pin connected to G pin of traffic light module #define RED_TIME 4000 // RED time in millisecond #define YELLOW_TIME 4000 // YELLOW time in millisecond #define GREEN_TIME 4000 // GREEN time in millisecond void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // The loop function repeats indefinitely void loop() { // red light on digitalWrite(PIN_RED, HIGH); // turn on digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, LOW); // turn off delay(RED_TIME); // keep red light on during a period of time // yellow light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, HIGH); // turn on digitalWrite(PIN_GREEN, LOW); // turn off delay(YELLOW_TIME); // keep yellow light on during a period of time // green light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, HIGH); // turn on delay(GREEN_TIME); // keep green light on during a period of time }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Nano 33 IoT.
  • Look at the traffic light module.

Remember that traffic lights may work differently depending on their design and the technology used in different areas and road intersections. The ideas above give a basic understanding of how traffic lights control traffic and help keep roads safe.

The code above shows how to control each light one at a time. Now, let's improve the code so it works better.

Arduino Nano 33 IoT Code Optimization

  • Let's make the code better by adding a function to control the lights.
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-traffic-light */ #define PIN_RED 3 // The Arduino Nano 33 IoT pin connected to R pin of traffic light module #define PIN_YELLOW 4 // The Arduino Nano 33 IoT pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The Arduino Nano 33 IoT pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // The loop function repeats indefinitely void loop() { // red light on trafic_light_on(RED); delay(times[RED]); // keep red light on during a period of time // yellow light on trafic_light_on(YELLOW); delay(times[YELLOW]); // keep yellow light on during a period of time // green light on trafic_light_on(GREEN); delay(times[GREEN]); // keep green light on during a period of time } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Let's make our code better by using a for loop.
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-traffic-light */ #define PIN_RED 3 // The Arduino Nano 33 IoT pin connected to R pin of traffic light module #define PIN_YELLOW 4 // The Arduino Nano 33 IoT pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The Arduino Nano 33 IoT pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = {PIN_RED, PIN_YELLOW, PIN_GREEN}; const int times[] = {RED_TIME, YELLOW_TIME, GREEN_TIME}; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // The loop function repeats indefinitely void loop() { for (int light = RED; light <= GREEN; light ++) { trafic_light_on(light); delay(times[light]); // keep light on during a period of time } } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i ++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Let's make our code better by using the millis() function rather than delay().
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-traffic-light */ #define PIN_RED 3 // The Arduino Nano 33 IoT pin connected to R pin of traffic light module #define PIN_YELLOW 4 // The Arduino Nano 33 IoT pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The Arduino Nano 33 IoT pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; unsigned long last_time = 0; int light = RED; // start with RED light void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); trafic_light_on(light); last_time = millis(); } // The loop function repeats indefinitely void loop() { if ((millis() - last_time) > times[light]) { light++; if (light >= 3) light = RED; // new circle trafic_light_on(light); last_time = millis(); } // TO DO: your other code } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!