It can be both an exciting and intimidating challenge to build your first ROS2 project. After building a basic robot with an Arduino, you may feel ready to explore the world of ROS 2—the industry-standard robotics framework. However, once you dive into tutorials, you’re quickly overwhelmed by the abundance of workspaces, packages, nodes, and commands. As frustration mounts, your robot sits motionless in the corner. Does that sound familiar?
The purpose of this guide is to help you steer clear of that situation. By adopting a methodical approach, decomposing the intricate ideas into digestible chunks, and avoiding needless diversions, we will concentrate on completing your first ROS2 project successfully. Here, changing your perspective is crucial. Rather than attempting to construct the world’s most advanced robot right away, focus on building a simple, functional system that works reliably—one that helps you learn the fundamentals without burnout or confusion.
Phase 1: The Foundation – A Simple, Reliable Robot for Your First ROS2 Project
Before diving into the world of ROS, it’s essential to ensure that your robot hardware works independently, without relying on the ROS system. This will help you isolate hardware issues from software problems later on. Here’s how you can approach this:
Chassis and Components
- Chassis: Start with a simple 2-wheel or 4-wheel drive chassis. This will be the base for your robot.
- Microcontroller: Use an ESP32. The ESP32 is crucial here due to its built-in WiFi capability, which will later allow communication with ROS 2. Note that an Arduino Uno won’t suffice for this guide.
- Sensors: Use an ultrasonic sensor like the HC-SR04, which provides clean and reliable distance readings.
- Motor Control: Implement a motor driver board such as the L298N or DRV8833 to control your robot’s movement.
- Pre-ROS Test: Start by writing a simple Arduino sketch for the ESP32. The sketch should have basic functions like goForward(), turnLeft(), etc. In your loop, read the ultrasonic sensor’s distance. If the distance is less than 20cm, stop and turn.
Why This Step Matters
By ensuring your hardware works independently of ROS, you can troubleshoot motor wiring, sensor calibration, and power supply issues without worrying about the complexities of ROS 2 communication. This step lays a solid foundation for your first ROS2 project, ensuring you’re ready to move on to the next phase with confidence.
Phase 2: The Blueprint – Answering the Key Questions Before You Code
Planning is crucial before jumping into the coding phase. This phase will help you understand how ROS 2 works and how to structure your system efficiently.
Key Questions for Building a ROS 2 System
1. What Are Our ROS 2 Building Blocks (Nodes and Topics)?
In ROS 2, we break the problem into nodes that communicate through topics. Let’s define the nodes and topics for the obstacle avoidance problem:
- Sensor Data Node (microROS_node on ESP32): This node is responsible for reading the ultrasonic sensor data and publishing the distance to a topic.
- Topic: /ultrasonic_distance (Float32 message)
- Topic: /ultrasonic_distance (Float32 message)
- Obstacle Avoidance Node (logic_node on PC): The logic node runs on your main computer and decides the robot’s actions based on the sensor data. It subscribes to the /ultrasonic_distance topic and publishes movement commands.
- Topic: /cmd_vel (geometry_msgs/msg/Twist message containing linear and angular velocity)
- Topic: /cmd_vel (geometry_msgs/msg/Twist message containing linear and angular velocity)
- Motor Driver Node (microROS_node on ESP32): This node subscribes to the /cmd_vel topic and translates movement commands into motor control signals.
Here’s a simple flow diagram of how these nodes communicate:
yaml
[ESP32: Sensor] -----> Publishes on /ultrasonic_distance -----> [PC: Logic Node]
^ |
| | (decides to turn)
| V
[ESP32: Motors] <----- Subscribes to /cmd_vel <----------------- [PC: Logic Node]
Why This Architecture?
This architecture isolates concerns effectively. The logic node on your PC handles the decision-making, while the ESP32 deals with low-level hardware control. This clear separation simplifies debugging and modification crucial advantages when working on your first ROS2 project.
2. How Will the ESP32 Communicate with ROS 2?
The ESP32 communicates with ROS 2 through micro-ROS. It’s a library that turns the microcontroller into a full-fledged participant in the ROS 2 network. The micro-ROS Agent runs on your PC, receiving data from the ESP32 over WiFi and forwarding it to the rest of the ROS 2 system.
3. How Will We Control the Motors?
While ROS 2 has a more sophisticated solution called ros2_control, for this basic project, we’ll stick with a simpler approach. The motor driver on the ESP32 will subscribe to the /cmd_vel topic and convert linear and angular velocity data into motor speeds.
Here’s a simplified C++ example:
cpp
void cmdVelCallback(const Twist& msg) {
float forward_speed = msg.linear.x;
float turn_speed = msg.angular.z;
// Simple differential drive math
float left_motor_speed = forward_speed - turn_speed;
float right_motor_speed = forward_speed + turn_speed;
// Convert these speeds to PWM signals for motors
setMotorSpeeds(left_motor_speed, right_motor_speed);
}
This method avoids the complexity of ros2_control while still introducing you to the basic principles of communication in ROS 2.
4. How Can We “See” What the Sensor Sees?
Debugging without visual feedback can be challenging, which is why RViz, ROS’s 3D visualizer, is invaluable. We’ll create a visualization node to display what the robot’s ultrasonic sensor detects.
- Task: The visualization node will subscribe to the /ultrasonic_distance topic and display a marker in RViz, showing the distance from the robot to any detected obstacle.
By adding RViz, you gain a powerful tool to visualize your robot’s surroundings and troubleshoot sensor issues effectively.

Phase 3: The Execution – Build One Piece at a Time
Now that we’ve laid out the plan, it’s time to start coding. Follow this step-by-step process to build your ROS 2 system.
Step 1: Set Up micro-ROS on Your ESP32
First, you need to set up micro-ROS on your ESP32. This step is essential in your first ROS2 project, as it involves compiling and uploading the necessary code to enable your ESP32 to publish and subscribe to ROS 2 topics.
Step 2: Test the Link
Start the micro-ROS agent on your PC and run the following command in your terminal to verify that the ESP32 is sending data:
bash
ros2 topic echo /ultrasonic_distance
You should see the distance data from your sensor. This confirms that your ESP32 is properly connected to ROS 2.
Step 3: Write the Logic Node
Now, write the ROS 2 logic node that will subscribe to the /ultrasonic_distance topic and publish movement commands to the /cmd_vel topic. Use Python or C++ to accomplish this.
Step 4: Test the Full Loop
Once you have the logic node running, place your hand in front of the ultrasonic sensor. The robot should begin to move in response to the sensor data. Congratulations, you’ve completed the first step in building a ROS 2 robot!
Step 5: Add Visualization
Write the visualization node and open RViz. Add a Marker display that listens to the /marker topic, and you’ll see a virtual obstacle appear when the sensor detects it.
Conclusion
By breaking down the process into clear phases and focusing on understanding the core ROS 2 communication system, you’ve successfully completed your first ROS2 project a robot that can avoid obstacles using ROS 2. What’s more, you’ve learned how to communicate between nodes, use micro-ROS, and visualize sensor data in RViz. These skills will serve as the foundation for more complex projects down the road.
For more in-depth guides and support on ROS 2, you can explore the ROS 2 Documentation or visit micro-ROS for specific details on using micro-ROS with ESP32.
If you’re looking to go further—from beginner-level robots to fully autonomous systems our Mobile Robotics Engineering course at Robotisim offers structured, hands-on learning with expert guidance. We’ll help you move from “just trying to make it work” to building robots that actually work.