Using Simulated Arduino on Wokwi
Authors
This lesson introduces how the Arduino programming framework works and how its behavior can be simulated using C++ and online tools like Wokwi. It begins with an explanation of Arduino’s two key functions — setup() and loop() — which replace the traditional main() function in standard C++ programs. The setup() function initializes the system once, while the loop() function runs repeatedly to control tasks such as blinking LEDs or reading sensors. Learners first recreate this framework manually in C++ to understand how Arduino manages timing, digital outputs, and repeated execution. In the next part, the focus shifts to understanding the physical side of Arduino by simulating a simple LED Blink circuit on Wokwi, connecting an LED and resistor to the board, and observing how setup() and loop() control the blinking pattern. Together, these activities build both conceptual and practical understanding of how Arduino code interacts with hardware and how such behavior can be modeled programmatically.
Section 1: Understanding the Arduino Framework
Arduino uses a framework that controls how programs run. Instead of writing a main() function, the framework automatically calls two special functions — setup() and loop(). The setup() function runs once when the device starts, and the loop() function runs continuously afterward, repeating as long as the device is powered. This structure allows Arduino to handle repeated actions, like blinking an LED or checking a sensor, without the programmer having to manually manage the main execution flow.
In C++, the same behavior can be recreated by writing setup() and loop() functions and then calling them from a small main() function. The setup() function can handle initialization, such as printing “Device is on,” while the loop() function performs actions repeatedly with short pauses using functions like delay() or digitalWrite(). Understanding this structure helps explain how Arduino organizes and runs user code inside its built-in framework.
| Code / Concept | Description |
|---|---|
void setup() |
Runs once when the program starts, used for initialization (similar to Arduino’s setup()). |
void loop() |
Runs repeatedly forever after setup() completes (similar to Arduino’s loop()). |
<thread> |
Provides tools to control program execution timing and threads. |
<chrono> |
Provides time-based utilities for measuring and creating time intervals. |
std::this_thread::sleep_for(chrono::milliseconds(x)) |
Pauses program execution for the specified number of milliseconds. |
printf("%s\n", message) |
Prints text to the terminal, similar to Arduino’s Serial.print(). |
fflush(stdout) |
Write buffered output to the screen immediately |
bool |
A data type that can hold two values: true or false, often used for pin states (HIGH/LOW). |
digitalWrite(int pin, bool value) |
Simulates controlling a digital output pin by printing whether it is HIGH or LOW. |
while (true) |
Creates an endless loop that repeats continuously, imitating Arduino’s automatic looping behavior. |
#include "arduino.h" |
Imports user-defined functions (delay, print, digitalWrite) just like including Arduino libraries. |
In this section, we’ll recreate the basic structure of the Arduino framework and library in C++. The goal is to simulate how Arduino repeatedly runs code to control hardware like LEDs.
Exercises
Example: Create the file arduino.h with the following functions to simulate Arduino’s built-in features:
-
void delay(int millisecs)– pauses execution for the given number of milliseconds. -
void print(char[] message)– prints a message to the terminal. -
void digitalWrite(int pin, bool value)– prints a message describing the pin’s state:- If
pinis 5 andvalueistrue, print"Pin 5 is HIGH". - If
pinis 5 andvalueisfalse, print"Pin 5 is LOW".
- If
// content of arduino.h
#include <cstdio>
#include <thread>
#include <chrono>
using namespace std;
void delay(int millisecs) {
this_thread::sleep_for(chrono::milliseconds(millisecs));
}
void print(const char message[]) {
printf("%s\n", message);
fflush(stdout);
}
void digitalWrite(int pin, bool value) {
if (pin == 5 && value == true) {
printf("Pin 5 is HIGH\n");
} else if (pin == 5 && value == false) {
printf("Pin 5 is LOW\n");
} else {
printf("Pin %d is %s\n", pin, value ? "HIGH" : "LOW");
}
}Exercise: Create the file my_project.h with #include "arduino.h" and the two key functions that make up the Arduino framework:
void setup()– runs once at the start of the program and prints"Device is on.".void loop()– callsdigitalWrite(), printing"Pin 3 is HIGH"and"Pin 3 is LOW"with a 0.5-second delay between each message.
Solution
// content of my_project.h
#include "arduino.h"
void setup() {
print("Device is on.");
}
void loop() {
digitalWrite(3, true);
delay(500);
digitalWrite(3, false);
delay(500);
}Exercise: Create the file main.cpp with a main() function that:
- Calls
setup()once to perform initialization. - Continuously calls
loop()inside an infinite loop to simulate Arduino’s repeated behavior. Then, execute the cell below to compile and run the code (with a timeout after 5 seconds)
Solution
// content of main.cpp
#include "my_project.h"
int main() {
setup();
while (true) {
loop();
}
}!g++ main.cpp -o main.out && timeout 5 ./main.outDevice is on.
Pin 3 is HIGH
Pin 3 is LOW
Pin 3 is HIGH
Pin 3 is LOW
Pin 3 is HIGH
Pin 3 is LOW
Pin 3 is HIGH
Pin 3 is LOW
Pin 3 is HIGH
Pin 3 is LOWSection 2: Getting Familiar with the Arduino
Arduino is a small programmable device that interacts with the physical world through sensors and actuators. Before focusing on code, it is important to understand how its basic circuits are connected. The simplest and most common first project is the LED Blink, often called the “Hello World” of Arduino.
In this mini project, the goal is to explore how an Arduino circuit is wired using an online simulator and to see how the board’s pins interact with components such as LEDs and resistors.
| Tool / Concept | Description |
|---|---|
| Arduino Uno | A microcontroller board used to connect and control electronic components. |
| LED (Light-Emitting Diode) | Emits light when current flows through it; often used as a basic output indicator. |
| Wokwi | An online Arduino simulator that allows building and testing circuits without physical hardware. |
| Digital Pin | A numbered connector on the Arduino that can be programmed to send or receive signals. |
| Ground (GND) | The reference point in the circuit for current flow. |
Exercises
Open the online Arduino simulator: Wokwi Arduino Uno.
Exercise: Follow the Adafruit tutorial: Arduino Blink Example to write a program that lets an LED attached to the arduino blink. Then, save that program in the sketch.ino tab of the Wokwi simulator.
Solution
// content of sketch.ino
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}Exercise: Use the point-and-click interface to
- Place an LED on the breadboard.
- Connect one side of the LED to digital pin 13 on the Arduino.
- Connect the other side to GND through a 220 Ω resistor.
Inspect the content of the diagram.json tab that stores the configuration of the board.
Solution
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": -0.6, "attrs": {} },
{
"type": "wokwi-led",
"id": "led1",
"top": -99.6,
"left": 157.4,
"attrs": { "color": "red" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": -72.85,
"left": 48,
"attrs": { "value": "220" }
}
],
"connections": [
[ "led1:C", "r1:2", "green", [ "v-9.6", "h-57.2" ] ],
[ "r1:1", "uno:GND.1", "green", [ "v0" ] ],
[ "led1:A", "uno:13", "green", [ "v0" ] ]
],
"dependencies": {}
}Exercise Run the program by clicking on the green arrow and observe how the LED turns on and off. Modify the delays in the loop() function to change the blik frequency.