MCU Simulator Robotic and Embedded System Laboratory
← Open the simulator
Documentation

Writing sketches

The editor, compiling and running, compiler errors, the serial monitor and how fast a sketch runs.

Sketches are written like in the Arduino IDE: one .ino file with setup() and loop().

The editor

The sketch.ino tab is the Monaco editor (the editor of VS Code), running in your browser:

  • C++ colours, matching brackets, the light or dark look of the app;
  • autocomplete of Arduino and ESP32 functions, of the methods of an object (type mqtt. to list the PubSubClient methods), and of #include names;
  • snippets: type wifi-connect, mqtt-reconnect or every-ms and pick it from the list to insert a ready piece of code;
  • short help when you hover a function;
  • Ctrl+Enter runs the sketch, Ctrl+S saves the project.

If the editor cannot load (a very old browser, a blocked script), a plain text box with line numbers is used instead; a simple editor chip next to the tabs says so.

Compiling and running

▶ Run sends the sketch to the simulator server, which compiles it for the selected board with the simulator's copy of the Arduino core and libraries. As in the Arduino IDE, you do not need to declare functions before using them.

  • The first compilation of a sketch takes a few seconds; a sketch that was compiled before (same code, same board) starts at once.
  • The server compiles at most 5 sketches per minute for you, and stops a compilation after 60 s.
  • Changing the board compiles again: LED_BUILTIN, SDA/SCL and macros like ESP8266 or CONFIG_IDF_TARGET_ESP32S3 follow the board.

Compiler errors

When the code does not compile, the error is underlined in red at its line in the editor (hover it for the message), the serial monitor tells you compilation failed, and Compiler output below the editor has the full log. Line numbers refer to your sketch.ino.

Common causes: a missing ; or }, a library that is not available (see Libraries), or a header of the other chip family: WiFi.h on an ESP8266 board (use ESP8266WiFi.h) or ESP8266WiFi.h on an ESP32. The message names the header to use.

While the sketch runs

  • Time runs at real speed: delay(1000) takes one second, millis() counts real milliseconds. A busy loop runs fast, but not as fast as a 240 MHz chip.
  • Serial.begin() needs no matching speed in the monitor; everything printed appears in the Serial monitor. Text typed there arrives at Serial.
  • Inputs (buttons, sliders of sensors) can be changed while it runs, in the part cards, the Diagram or the Schematic.
  • ESP.restart() starts the sketch again from setup(); what it stored in the flash stays.
  • ■ Stop stops it; ▶ Run starts it again from the beginning.

Tips

  • Print what is happening with Serial.println(); the serial monitor is the first place to look.
  • Use millis() instead of long delay()s when the sketch must react to buttons or MQTT messages (snippet every-ms).
  • Call mqtt.loop() often: without it the board receives no messages and the connection drops, as on a real ESP32.