Skip to content


Repository files navigation

mruby on the ESP32

This is an ESP-IDF project template running mruby on the ESP32 microcontroller.

To get started, you need to install the ESP-IDF, by following the instructions here, for your operating system.

This has been tested on macOS and Ubuntu Linux, using ESP-IDF 5.1.


Recursively clone this repo to ensure the mruby (3.2.0) submodule gets downloaded:

git clone --recursive

The makefile configuration is in main/ The entry point source file is mruby_main.c. Once that starts, it looks for storage/main.rb and runs it in mruby. You can change the expected filename in mruby_main.c, or simply save your scripts as main.rb inside the storage subfolder.

First Build build -p $(YOUR_SERIAL_PORT) flash monitor

Your ESP32 should write 2 lines of numbers to the console.


The folder main/examples includes simple scripts demonstrating functionality.

Once you are familiar with the build process, try them with:

cp main/examples/$(EXAMPLE_FILENAME) main/storage/main.rb build -p $(YOUR_SERIAL_PORT) flash monitor

Replace EXAMPLE_FILENAME with one of the following:

  • simplest.rb - Prints two strings
  • system.rb - Demonstrates most system APIs
  • gpio.rb - GPIO blink example
  • wifi_connect.rb - Connects to WiFi
  • wifi_socket.rb - Connects to WiFi and makes a TCPSocket connection
  • mqtt_publish.rb - Connects to WiFi and publishes to MQTT broker
  • filesystem.rb - Write/append/read a file on the virtual filesystem
  • ledc_breathe.rb - Gradually fades the brightness of an LED up and down
  • ledc_buzzer.rb - Plays a melody on a piezo-electric buzzer
  • ledc_servo.rb - Controls position of a 180 degree hobby servo motor

Note: Edit GPIO numbers to match ones you are connected to, insert your WiFI credentials, customize MQTT settings etc.

Build Customization

There are multiple gems that can be turned on and off via the mruby configuration file found in components/mruby_component/esp32_build_config.rb:

To get gem changes to reflect in the build, fullclean the previous build, then build again: fullclean build

All gems are enabled by default, so you can try out the examples, but it's a good idea to disable ones you don't need.


Everything works on:

  • Original ESP32: set-target esp32

Everything except gpio gem works on:

  • ESP32-S2: set-target esp32s2
  • ESP32-S3: set-target esp32s3

If you followed the IDF installation instructions correctly for your chip, you can switch the project target with the corresponding command above.

You will not be able to build again until the partition table is reset to partitions.csv. To do this: menuconfig
# Partition Table -> Partition Table (1st option) -> Custom partition Table CSV (Last options)
# Enter to select. Q to exit. Y to save

You may also want to manually change other settings like CPU clock speed, watchdog timers, stack sizes etc. Changing the target resets everything to default.


The following files and folders are safe to delete when trying to solve build issues:

  • build
  • components/mruby_component/build
  • components/mruby_component/esp32_build_config.rb.lock
  • managed_components
  • dependencies.lock