QP-nano active object framework for Arduino
Author:  Quantum Leaps
Contact: www.state-machine.com


QP-nano for Arduino QP-nano for Arduino


 5.6.2 2016-04-06: Updated for QP-nano 5.6.2 and Arduino-1.6.x
 4.5.03 2013-06-02: Updated for QP 4.5.03/QM 2.3.2 and Arduino-1.5.2
 4.5.03 2013-06-02: Updated for QP 4.5.03/QM 2.3.2 and Arduino-1.0.4
 4.5.02 2012-12-20: Added video "Getting started with QP/QM on Arduino"
 4.5.02 2012-08-25: Updated for QP 4.5.02/QM 2.2.02 and Arduino-1.0.1
 4.3.00 2011-12-16: Updated for QP 4.3.00 and Arduino-1.0
 4.2.04 2011-09-25: Updated for QP 4.2.04 and QM 2.0.00
 4.1.06 2011-02-04: Initial Release for Arduino


QP-nano is a lightweight, open source, active object (actor) framework for microcontrollers such as Arduino. QP-nano enables you to build well-structured, responsive, and truly concurrent event-driven programs as systems of concurrently executing active objects (active objects, a.k.a. actors). You can think of QP-nano as a modern real-time operating system (RTOS) specifically designed for executing concurrent state machines. The QP-nano framework does everything you can expect from a traditional RTOS plus many things an RTOS can't do at all.

QP-nano is also an excellent target for automatic code generation. To this end, QP-nano is supported by the the free graphical QM modeling tool, which can automatically generate complete Arduino sketches from state diagrams. Thus QP-nano is your entry into graphical programming for Arduino.

QP-nano improves productivity, because your programs stay responsive to new events as you keep adding them. You also no longer need to struggle with convoluted if-else logic, because this "spaghetti" code is replaced with elegant state machines. And you get a powerful multitasking support, without worrying about semaphores and other such low-level mechanisms typically found in RTOSes. Instead, you can work at a higher level of abstraction of events, state machines, and active objects.


Traditionally, Arduino programs are written in a sequential manner, which means that whenever an Arduino program needs to synchronize with some external event, such as a button press, arrival of a character through the serial port, or a time delay, it explicitly waits in-line for the occurrence of the event. Waiting in-line means that the Arduino processor spends all of its cycles constantly checking for some condition in a tight loop (called the polling loop).

Although this approach is functional in many situations, it doesn't work very well when there are multiple possible sources of events whose arrival times and order you cannot predict and where it is important to handle the events in a timely manner. The fundamental problem is that while a sequential program is waiting for one kind of event (e.g., a button press), it is not doing any other work and is not responsive to other events (e.g., characters from the serial port).

For these and other reasons experienced programmers turn to the long-know design strategy called event-driven programming, which requires a distinctly different way of thinking than conventional sequential programs. All event-driven programs are naturally divided into the application, which actually handles the events, and the supervisory event-driven infrastructure (framework), which waits for events and dispatches them to the application. The control resides in the event-driven framework, so from the application standpoint, the control is inverted compared to a traditional sequential program.

It turns out that the QP-nano active object framework beautifully complements the Arduino platform and provides everything you need to build responsive, robust, and power-efficient Arduino programs based on modern hierarchical state machines. Enjoy!

Download and Installation

Download latest version of QP-nano for Arduino from SourceForge.net.

Installation of QP for Arduino consists of simply expanding the ZIP archive into your Arduino Sketchbook directory.

The followig YouTube video shows how to download, install, and run the code on the Arduino UNO board.


The qpn-5.6.2_arduino-1.6.x.zipp file contains the following documentation in the folder doc\.

Application Note: Event-Driven Arduino Programming with QP-nano and QM describes the main concepts and how to build QP applications for Arduino.


You can build, upload, and run the Arduino projects directly from the QM tool, without the need to launch the standard Arduino IDE (although the standard Arduino software must be installed, because the compiler, linker, and other tools from it are used in the build process.)

Blinky Example

The Blinky example demonstrates a simple 2-state state machine to blink the LED of Arduino UNO board once a second.

The Blinky QM model file is located in <Arduino_Sketchbook>\library\qpn_avr\examples\blinky\blinky.qm.

PEdestrian LIght CONtrolled (PELICAN) Crossing

The PELICAN crossing example demonstrates a non-trivial hiererchical state machine.

The PELICAN QM model file is located in <Arduino_Sketchbook>\library\qpn_avr\examples\pelican\pelican.qm.

To see the output from the PELICAN crossing example, you need to launch a serial terminal program, which runs outside the QM tool. The tools pre-configured in the provided examples, use the TeraTerm serial terminal, which can be downloaded freely from the Internet.

The PELICAN crossing example requires you to inject events to the application by sending key strokes via the Serial Monitor:

  • 'p' for PEDS_WAITING,
  • 'f' for OFF (to switch the crossing OFF for maintenance),
  • 'o' for ON (to switch the crossing back online).

Dining Philosopher Problem (DPP) Example

The DPP example demonstrates multiple active objects (concurrent state machines) that collaborate with each other by exchanging events.

The DPP QM model file is located in <Arduino_Sketchbook>\library\qpn_avr\examples\dpp\dpp.qm.

More Information and Resources

Last Modified: April 07, 2016, at 12:48 PM
By: Miro Samek