Back to Home

Traffic Light on ESP8266 and CH32V003: Code and Schematic

Educational room traffic light project on ESP8266 and CH32V003 demonstrates basic work with GPIO and timings. Full code for Arduino IDE and WCH SDK, pin connection schematics provided. Suitable for studying embedded programming.

DIY traffic light: ESP8266 vs CH32V003 with code
Advertisement 728x90

Build a Room Traffic Light with ESP8266 and CH32V003

Connect an RYG traffic light module to an ESP8266 or CH32V003 and implement a basic cycle: red for 5 seconds, yellow for 1 second, green for 5 seconds with flashing. This project shows GPIO setup, delays, and loops without external libraries. Perfect for your first steps in embedded development.

ESP8266 uses GPIO 13 (red), 12 (yellow), 14 (green). CH32V003 uses ports PC5, PC6, PC7. The code is streamlined, mimicking a real traffic light with transition prep.

Setting Up GPIO on ESP8266

Define the pins and set them as outputs. Use current-limiting resistors to protect the LEDs.

Google AdInline article slot
const byte redLedPin = 14;
const byte yellowLedPin = 12;
const byte greenLedPin = 13;

void setup() {
  pinMode(redLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
}

In loop(), run this sequence:

  • Red HIGH for 5000 ms.
  • Yellow HIGH for 1000 ms, then LOW for both.
  • Green HIGH for 5000 ms.
  • Three flash cycles: HIGH 500 ms, LOW 500 ms.
  • Yellow HIGH 1000 ms, then LOW.
void loop() {
  // red
  digitalWrite(redLedPin, HIGH);
  delay(5000);

  // yellow
  digitalWrite(yellowLedPin, HIGH);
  delay(1000);

  digitalWrite(redLedPin, LOW);
  digitalWrite(yellowLedPin, LOW);
  
  // green
  digitalWrite(greenLedPin, HIGH);
  delay(5000);

  for (byte i = 0; i < 3; i++) {
    digitalWrite(greenLedPin, HIGH);
    delay(500);
    
    digitalWrite(greenLedPin, LOW);
    delay(500);
  }

  // yellow
  digitalWrite(yellowLedPin, HIGH);
  delay(1000);
  digitalWrite(yellowLedPin, LOW);
}

Connect via USB with CH340 drivers and Arduino IDE with the ESP8266 board package. Flash as usual.

Switching to CH32V003

A compact RISC-V microcontroller for low-power tasks. Needs a WCH LinkE programmer: USB power for the board, SWD for flashing. Wire the traffic light to PC5 (red), PC6 (yellow), PC7 (green).

Google AdInline article slot
/* Global define */
#define RED_LED_PIN     GPIO_Pin_5 
#define YELLOW_LED_PIN  GPIO_Pin_6
#define GREEN_LED_PIN   GPIO_Pin_7

Logic stays the same: delay_ms instead of delay, GPIO_WriteBit. Initialization:

  • RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  • GPIO_InitTypeDef for OutputPushPull.

The loop() is adapted for the WCH SDK: state sequence with timers. Full code uses standard CH32V003FUN functions to minimize dependencies.

Platform Comparison

| Parameter | ESP8266 | CH32V003 |

Google AdInline article slot

|----------|---------|----------|

| Core | Tensilica L106 | RISC-V2A |

| Pins | GPIO12-14 | PC5-7 |

| Power | USB | USB + programmer |

| Flashing | Arduino IDE | WCH LinkE + IDE |

| Price | Medium | Low |

ESP8266 is beginner-friendly thanks to USB and the Arduino ecosystem. CH32V003 saves resources but requires a programmer.

Key benefits:

  • Minimal code, no libraries needed.
  • Easy to tweak timings.
  • Scales to buttons or sensors.

Key Tips

  • GPIO Wiring: Always use 220–330 Ohm resistors to limit current.
  • Timings: delay() blocks the CPU; for advanced projects, use millis() or timers.
  • Debugging: Monitor Serial for state logs.
  • Ports: Stick to PCx on CH32V003 for simplicity—no PWM at first.
  • Scaling: Add a finite state machine (FSM) for states instead of a linear loop.

This project is easy to replicate: grab an RYG module and breadboard it. Ideal for showcasing basic embedded coding.

— Editorial Team

Advertisement 728x90

Read Next