Introduction: State Machine on Arduino - a Pedestrian Traffic Light

About: Software Engineer - doing a lot of stuff with state machines.

Hey there!
I'm going to show you how to program a pedestrian traffic light for Arduino in C++ with a finite state machine by using YAKINDU Statechart Tools. This will demonstrate the power of state machines and can be used as a blueprint for further projects.


You only need an Arduino, some pushbuttons, LEDs, Jumper and resistors.


  • 2x Pushbutton
  • 2x Traffic Lights or you can use LEDs with 220 Ohm resistors
  • 2x 10k Ohm resistors
  • Breadboard
  • Jumper
  • Arduino Uno/Mega (or any other, that got at least 8 GPIOs)


Step 1: Define How It Works: Pedestrian Traffic Light

At first, we need to specify how the pedestrian traffic light should work. I've tried to summarize the bullet points:

  • Two traffic lights are used - one for the cars, the other for the pedestrians
  • Traffic light can be turned on by using a button
  • Pedestrian can request crossing the street by pressing a button
  • Turned off traffic light is indicated by blinking both yellow LEDs
  • After turning on, the traffic light waits for 10 seconds in safe mode
  • After safe mode, cars always have a green phase until a pedestrian starts a request
  • Pedestrian request for crossing is indicated by toggling yellow LED

Additionally, there are some time events depending on the way of how a traffic light works.

Step 2: Build the Circuit

Let's start setting up the circuit. In my example, as you can see on the preview image, I've used an Arduino Mega 2560, but every other Arduino with at least eight GPIOs should be fine. Additionally, I've bought some 5V traffic lights at Amazon. Just search for Traffic Light Arduino. Otherwise, you can simply use six different LEDs. Furthermore, you need two push buttons and two 10k Ohm resistors.

The three left LEDs are used for the car traffic and the three right LEDs for the pedestrians, where the yellow one can indicate a request.

Step 3: Creating the State Machine

So, as you can see there are two main states with composite members - state on and state off. You can switch between both states by using the event onOff, which is wired to the ON/OFF button. In the initial state, the off state, both yellow LEDs start blinking every second. Once the traffic light has been turned on, it starts in the Safe
state. After 10 seconds the typical behaviour of the traffic lights will be handled. The red and yellow LED will be turned on in the StreetPrepare state and the traffic light turns green after 2 more seconds. From now on, the state machine is waiting for the pedestrianRequest event, which is wired to the second button. After sending the event the request will be indicated by toggling the yellow LED every second in the PedWating state. 7 seconds later the traffic light will be switched to yellow first and then to red until the pedestrians get the signal to go in the PedestrianGreen state. After this, the PedestrianRed is activated and the cycle repeats. This can only be interrupted by turning the traffic light off again.

Step 4: Running the Example

Step 5: Generated C++ Code

The generated C++ code is a classic switch-case to handle the logic of the state machine. This is just a code snippet of how it looks like:

void TrafficLightCtrl::runCycle()
{ clearOutEvents(); for (stateConfVectorPosition = 0; stateConfVectorPosition < maxOrthogonalStates; stateConfVectorPosition++) { switch (stateConfVector[stateConfVectorPosition]) { case main_region_on_r1_StreetGreen : { main_region_on_r1_StreetGreen_react(true); break; } case main_region_on_r1_PedWaiting_r1_waitOn : { main_region_on_r1_PedWaiting_r1_waitOn_react(true); break; } default: break; } clearInEvents(); }

Step 6: Get the Example

The whole example is ready to compile & run. It comes with YAKINDU Statechart Tools, which is free for non-commercial use. After downloading, you can import the example directly in the tool:

File -> New -> Example -> YAKINDU Statechart Examples -> Next -> Traffic Light (C++) for Arduino

>> HERE you can download YAKINDU Statechart Tools <<

You can start with a 30 days trial. Afterwards, you must get a license, which is free for non-commercial use!