Blog

micro-ROS Explained: Bringing a Microcontroller Into Your ROS 2 Graph

Tom Cosgrove,

Reading Room: The Fictionlab Blog

TL;DR: micro-ROS is a ROS 2 client framework for microcontrollers with kilobytes of RAM instead of gigabytes. It lets an MCU publish topics, subscribe, offer services, and, when configured with a parameter server, expose parameters as a ROS 2 node, so your motor controller or sensor board can appear in ros2 node list alongside everything running on the main computer. It works by using Micro XRCE-DDS, a client/agent protocol where the MCU is a thin client and a micro-ROS agent process on the host computer bridges it into the full DDS network. Transport is usually UART serial or UDP over Ethernet/Wi-Fi. You need micro-ROS when you want deterministic low-level control (PWM loops, encoder counting, IMU sampling at a fixed rate) without writing and maintaining a custom serial protocol. You do not need it when a simple fixed-format serial frame would do, or when your MCU has less than roughly 32 KB of usable RAM.

What is micro-ROS and why does it exist?

Typical ROS 2 deployments assume a general-purpose computer running an operating system such as Linux, with a full DDS implementation, threads, dynamic memory allocation, and typically hundreds of megabytes of RAM available. That assumption holds for a Raspberry Pi, an Intel NUC, or a Jetson. It does not hold for an STM32F4 with 192 KB of SRAM, an ESP32, or a Teensy sitting on a motor driver board.

Yet that small chip is exactly where you want certain work to happen. Reading quadrature encoders at 10 kHz, closing a PID loop at 1 kHz, generating PWM with microsecond jitter, debouncing limit switches – none of this belongs on a Linux host where the scheduler can delay your callback by milliseconds without warning.

The traditional answer is a custom serial protocol. You define a framing format, write a parser on both ends, add a checksum, and spend the next two years fixing edge cases in it. micro-ROS replaces much of that work. It is a ROS 2 ecosystem project that brings the ROS 2 client API down to resource-constrained hardware.

Concretely, micro-ROS gives your microcontroller firmware a C API based on rcl and rclc, the lightweight C convenience layer for ROS 2. You create a node, create a publisher, allocate a message, fill it, and publish. On the other side of the wire, a ROS 2 node sees a normal topic with a normal message type. No custom application-level message format to document.

How micro-ROS differs from running ROS 2 directly

The key architectural difference is the middleware. Full ROS 2 commonly uses DDS, which performs automatic discovery, maintains participant state, and can allocate memory dynamically for endpoints. That is too heavy for a small MCU.

micro-ROS instead uses Micro XRCE-DDS (eXtremely Resource Constrained Environments DDS), maintained by eProsima. The MCU runs a lightweight client. A separate process called the micro-ROS agent runs on a Linux machine, speaks Micro XRCE-DDS toward the MCU and full DDS toward the rest of the ROS 2 graph. The agent creates and destroys DDS entities on behalf of the client.

The practical consequence: your MCU never participates in DDS discovery and never holds full DDS participant state. It holds a session with one agent. This asymmetry is what makes the memory footprint viable.

How does the micro-ROS architecture fit together?

A working micro-ROS setup has three pieces. Understanding the boundaries between them saves a lot of debugging time later.

  • Firmware on the MCU. Your application code plus the micro-ROS client library, built for your target. It runs on bare metal, FreeRTOS, Zephyr, NuttX, or as an Arduino library, depending on the port you choose.
  • Transport. The physical and logical link between MCU and agent. Serial (UART or USB CDC), UDP over Ethernet or Wi-Fi, or custom transports you implement yourself against a small four-function interface.
  • micro-ROS agent. A process on the host, available as a Docker image or built from source into your workspace. It listens on the chosen transport and bridges to DDS.

The agent is not optional and it is not a convenience. Without it, the MCU’s messages never reach the ROS 2 graph. In a simple setup, start the agent before initializing the board. Production firmware should explicitly detect agent loss and implement reconnection handling rather than assuming that every client configuration reconnects automatically.

Choosing a transport

Transport choice is the decision that most affects your latency and your wiring. Here is how the common options behave in practice.

  • UART serial at 115200 to 921600 baud. Simplest, most reliable, and the default for boards physically mounted next to the host computer. Throughput is the limiting factor: at 115200 baud you get roughly 11 KB/s of raw payload bandwidth before framing overhead, which is plenty for joint states and odometry but not for raw IMU bursts at high rates.
  • USB CDC on MCUs with native USB. Potentially higher throughput than a low-baud UART connection and a single cable, but USB enumeration adds a reconnect path you have to handle when the host reboots.
  • UDP over Ethernet or Wi-Fi. Best for boards that are physically distant from the host, and the usual choice for ESP32 targets. Wi-Fi introduces variable latency, so avoid it for anything inside a control loop.
  • CAN-FD or custom. micro-ROS exposes a transport interface with open, close, write, and read callbacks. If your platform already runs a CAN bus, you can implement it, though you take on the fragmentation logic.

For a mobile robot where the MCU sits on the same chassis as the computer, serial is almost always the right answer. The Leo Rover uses exactly this pattern: an onboard microcontroller handles motor control and sensor sampling, and the Raspberry Pi runs the ROS 2 stack above it.

How do you publish a topic from a microcontroller?

The following example is a minimal micro-ROS publisher for an Arduino-compatible target using the micro_ros_arduino library. It publishes an incrementing std_msgs/msg/Int32 at 10 Hz over serial. Every micro-ROS program follows this same shape regardless of the port.

First, the firmware. Install the micro_ros_arduino library for your ROS 2 distribution, then flash this:

#include <micro_ros_arduino.h>
#include <rcl/rcl.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <std_msgs/msg/int32.h>

rcl_publisher_t publisher;
std_msgs__msg__Int32 msg;
rclc_executor_t executor;
rcl_timer_t timer;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;

void timer_callback(rcl_timer_t *timer, int64_t last_call_time) {
  (void) last_call_time;
  if (timer != NULL) {
    rcl_publish(&publisher, &msg, NULL);
    msg.data++;
  }
}

void setup() {
  set_microros_transports();
  delay(2000);

  allocator = rcl_get_default_allocator();
  rclc_support_init(&support, 0, NULL, &allocator);
  rclc_node_init_default(&node, "mcu_node", "", &support);

  rclc_publisher_init_default(
    &publisher,
    &node,
    ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
    "mcu_counter");

  rclc_timer_init_default(&timer, &support, RCL_MS_TO_NS(100), timer_callback);
  rclc_executor_init(&executor, &support.context, 1, &allocator);
  rclc_executor_add_timer(&executor, &timer);

  msg.data = 0;
}

void loop() {
  rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
}

Note the structure. There is no rclcpp::spin() that blocks forever. The rclc executor is explicitly stepped with spin_some, which returns after processing available work. That gives you room in loop() for your real-time code – encoder reads, PID updates, safety checks.

Running the agent on the host

With the board flashed and connected, start the agent. The Docker image is the fastest path and avoids polluting your workspace:

docker run -it --rm --net=host --device=/dev/ttyUSB0 \
  microros/micro-ros-agent:humble \
  serial --dev /dev/ttyUSB0 -b 115200

Replace humble with your distribution and /dev/ttyUSB0 with your actual device. Host networking lets the agent participate in the same DDS network as ROS 2 tools running on the host. If you prefer a source build inside a ROS 2 workspace, the micro_ros_setup package provides the build scripts, and the agent is then launched with:

ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyUSB0 -b 115200

For a UDP transport instead, swap the subcommand:

ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888

Verifying the connection

Once the agent reports a session, the MCU is represented as a node in your graph. Confirm it with standard tooling – no special micro-ROS commands required:

ros2 node list
ros2 topic list
ros2 topic echo /mcu_counter
ros2 topic hz /mcu_counter

You should see /mcu_node, the topic /mcu_counter, and a stream of incrementing integers at approximately 10 Hz. If ros2 node list is empty but the agent logs show session creation, check that your ROS_DOMAIN_ID matches between the agent process and your shell, and, when using Docker, that the container can participate in the host DDS network.

What are the memory constraints you need to plan for?

This is where micro-ROS projects succeed or fail. The library is small by ROS standards but not free, and the ROS 2 type system is not designed exclusively around static allocation.

Published micro-ROS documentation and benchmarking material from the project indicate that a minimal client with a single publisher fits in the low tens of kilobytes of RAM, with flash usage typically in the range of tens to low hundreds of kilobytes depending on the middleware configuration, transport, and how many message types you link in. Treat these as approximate figures: your actual footprint depends heavily on your toolchain, optimization level, and configuration. Measure your own build rather than trusting a number from a blog post.

Several factors drive memory consumption more than anything else:

  1. Number of entities. Each publisher, subscriber, service, client, and timer consumes session state in the Micro XRCE-DDS client. These are configured through colcon.meta with limits such as RMW_UXRCE_MAX_PUBLISHERS and RMW_UXRCE_MAX_SUBSCRIPTIONS. Set them to what you actually need, not to a generous default.
  2. Message type complexity. A std_msgs/Int32 is 4 bytes. A sensor_msgs/LaserScan with unbounded arrays requires you to pre-allocate the array memory yourself, because micro-ROS cannot safely rely on runtime dynamic allocation on most targets.
  3. Transport buffers. RMW_UXRCE_STREAM_HISTORY and the MTU setting directly multiply into static buffer size. Large messages need large buffers, and those buffers live in RAM permanently.
  4. The RTOS. FreeRTOS task stacks are often the largest single consumer. A micro-ROS task needs a stack measured in kilobytes, not hundreds of bytes.

The practical rule: if your target has less than roughly 32 KB of usable RAM after accounting for your application, micro-ROS will be painful. At 64 KB and above you have room to work. At 192 KB or more, as on many STM32F4 variants, you have comfortable headroom for several publishers and subscribers plus real application logic.

Handling unbounded message fields

Messages with variable-length arrays or strings require explicit memory management. You must allocate the buffer and set the capacity before publishing or subscribing. For a string field:

static char buffer[64];
msg.data.data = buffer;
msg.data.capacity = 64;
msg.data.size = 0;

Forgetting this step is a common cause of hard faults in new micro-ROS firmware. The micro-ROS project provides micro_ros_utilities with helper functions that allocate message memory according to a configurable size specification, which is worth using once your message types get nontrivial.

When does micro-ROS not make sense?

micro-ROS is a good fit for a specific set of problems, and forcing it elsewhere costs you flash, RAM, and debugging hours. Fictionlab’s engineering experience across custom platform builds points to several cases where a simpler approach wins.

  • Your MCU has under 32 KB of RAM. An ATmega328P at 2 KB is not a micro-ROS target and never will be. A fixed-format binary serial frame is the correct engineering choice there.
  • You exchange one or two fixed values. If the entire interface is “send velocity, receive encoder counts,” a 12-byte struct over UART with a CRC is less code, less memory, and easier to debug with a logic analyzer.
  • You need hard real-time guarantees across the link. micro-ROS can standardize communication, but the transport is still shared and the agent still runs on a non-real-time Linux kernel. Keep hard deadlines entirely on the MCU side.
  • Your board has no reliable connection to a host. micro-ROS requires an agent. A fully standalone sensor node with no companion computer gains nothing from it.
  • High-bandwidth raw data. Pushing uncompressed camera frames or dense point clouds through a serial micro-ROS link will saturate it. Use a dedicated interface for bulk data.

Conversely, micro-ROS pays off clearly when you have several message types, expect the interface to evolve, want ROS 2 services or parameters on the MCU, or need multiple engineers working across the hardware/software boundary. The shared IDL definition becomes the contract, and ros2 topic echo becomes your debugging tool instead of a hex dump.

How does this apply to mobile robot platforms?

On a field robot, the split is natural. The microcontroller owns anything with a deadline: motor commutation, wheel odometry integration, battery monitoring, emergency stop handling, servo timing. The Linux computer owns anything with state: navigation, SLAM, perception, mission logic, logging.

micro-ROS makes that boundary transparent. The MCU publishes sensor_msgs/JointState for wheel positions and velocities, subscribes to a command topic, and exposes diagnostics through diagnostic_msgs. From the navigation stack’s perspective there is no microcontroller – just topics.

This matters for teaching and research work too. In an education setting, students can modify firmware behavior and immediately observe the effect through standard ROS 2 tools, without first learning a bespoke protocol. The learning curve stays on ROS 2 concepts rather than on project-specific plumbing.

For teams building a platform around a specific payload or sensor set, the same reasoning applies to custom robot development: a micro-ROS firmware layer means the low-level board can change without rewriting the host-side interface, as long as the topic and message contract holds.

Frequently asked questions

Is micro-ROS an official part of ROS 2?

micro-ROS is a ROS 2 ecosystem project maintained separately from the ROS 2 core repositories. It tracks ROS 2 distributions, so you should build a micro-ROS client and agent intended for the same ROS 2 distribution as your host, such as Humble or Jazzy. Keeping these components aligned is the supported configuration.

Do I need the micro-ROS agent running at all times?

Yes, for the MCU to communicate with and be visible in the ROS 2 graph. The agent performs the DDS-side work on the client’s behalf. If the agent stops, the MCU’s firmware keeps running – your control loop does not halt – but communication with the ROS 2 graph stops until the connection is restored and the client reconnects. Design your firmware so that loss of the agent triggers a safe state, typically stopping motors after a timeout.

Which microcontrollers and RTOSes are supported?

micro-ROS supports FreeRTOS, Zephyr, and NuttX, along with bare-metal builds and an Arduino library, depending on the maintained port and ROS 2 distribution. Commonly used targets include STM32 families, ESP32, Teensy, Raspberry Pi Pico, and Renesas boards. Because the transport layer is a small callback interface, porting to an unsupported board is realistic if the chip meets the memory requirements.

Can a micro-ROS node offer services and parameters, not just topics?

Yes. The rclc API supports services and clients in addition to publishers and subscribers, and rclc also provides a parameter-server implementation that your firmware must initialize explicitly. Each adds to the entity count and therefore to memory, so configure the corresponding limits in colcon.meta. Parameters are useful for tuning PID gains at runtime without reflashing.

What latency should I expect over a serial transport?

Latency depends on baud rate, message size, agent polling, and host scheduling, so there is no single number worth quoting. What you can control: raise the baud rate, keep messages small, and use best-effort reliability for high-rate sensor streams where a dropped sample is acceptable. Measure end-to-end on your own hardware with timestamps on both sides rather than relying on published figures from a different setup.

How does micro-ROS compare to rosserial from ROS 1?

rosserial was a ROS 1 bridge that translated a custom serial protocol into ROS 1 topics. micro-ROS is architecturally different: the MCU runs a ROS 2 client library over Micro XRCE-DDS rather than a rosserial-style application protocol. That brings ROS 2 message type support, services, parameters when configured, and QoS configuration to the microcontroller, at the cost of a larger footprint than rosserial required.

Can I run micro-ROS without Linux on the host side?

The agent is most commonly deployed on Linux, which is the standard platform for ROS 2 robotics systems. ROS 2 also supports Windows and macOS, but micro-ROS agent deployment, transport access, and DDS networking should be validated for the specific platform and version. Docker is often convenient on Linux; direct serial-device access in Docker on Windows or macOS can require additional setup.

Where to go from here

Start small. Flash the Int32 publisher above, confirm it appears in ros2 topic list, then add one subscriber and one timer-driven control loop. Measure your flash and RAM usage after each addition so you learn your own numbers rather than approximations. Once the pattern is familiar, replacing a custom serial protocol with micro-ROS on an existing robot is mostly a firmware refactor, not a redesign.

If you are evaluating micro-ROS as the firmware layer for a specific platform – a new sensor payload, a modified drivetrain, or a purpose-built chassis – Fictionlab’s team works on exactly this kind of integration. See custom robotics development to discuss how a micro-ROS firmware layer fits your hardware.


Read more