Build a ROS2 Robot on Raspberry Pi 5: From Zero to Autonomous Navigation
Complete guide to building a differential drive robot with ROS2 Humble on Raspberry Pi 5, including lidar SLAM and Nav2 navigation.
ROS2 (Robot Operating System 2) is the standard middleware for robotics. Whether you're building a home robot, a research platform, or an industrial AGV, understanding ROS2 is essential. This guide builds a functional autonomous robot from scratch using affordable components available in India.
Hardware You Need
Raspberry Pi 5 (4GB or 8GB, 8GB recommended for Nav2): ₹7,000–₹8,500 RP2040-based microcontroller (for low-level motor control): ₹400–₹600 2× DC geared motors with encoders: ₹800–₹1,200 Motor driver (L298N or TB6612FNG): ₹150–₹400 LD19 or RPLidar A1 (for SLAM): ₹5,000–₹8,000 Differential drive chassis: ₹600–₹1,200 3S LiPo battery: ₹1,500–₹2,500 Total: approximately ₹15,000–₹22,000
Installing ROS2 Humble on Raspberry Pi 5
Flash Ubuntu 22.04 Server LTS (arm64) to your Pi 5 SD card using Raspberry Pi Imager. Boot, update: `sudo apt update && sudo apt upgrade`. Then follow the official ROS2 Humble installation (binary install from apt). Add `source /opt/ros/humble/setup.bash` to your ~/.bashrc. Install key packages: `sudo apt install ros-humble-navigation2 ros-humble-slam-toolbox ros-humble-teleop-twist-keyboard`.
Setting Up the Differential Drive
Your RP2040 (or Arduino) handles encoder reading and PWM motor control. Flash it with micro-ROS to communicate with ROS2 over USB serial. The microcontroller publishes /wheel_odom (encoder ticks) and subscribes to /cmd_vel (velocity commands). On the Pi, run the micro-ROS agent: `ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyACM0`. Verify with `ros2 topic echo /wheel_odom`.
SLAM with slam_toolbox
Connect your lidar (RPLidar: `sudo apt install ros-humble-rplidar-ros`). Launch slam_toolbox in mapping mode. Drive the robot around your space using teleop_twist_keyboard. As the robot moves, it builds a 2D occupancy grid map. Save the map: `ros2 run nav2_map_server map_saver_cli -f ~/my_map`. You now have a map.pgm and map.yaml: your navigation map.
Autonomous Navigation with Nav2
With a saved map, switch to localisation mode (AMCL) instead of SLAM. Launch nav2_bringup with your map. Open RViz2, load the nav2 perspective, and use the "2D Nav Goal" tool to click a destination on the map. Nav2's planner (NavFn or Smac) generates a global path; the controller (DWB) executes it while avoiding obstacles detected by the lidar in real time. First time it works is genuinely exciting.
Debugging Tips
`ros2 topic hz /scan` - check lidar is publishing at expected rate (10Hz+) `ros2 run tf2_tools view_frames` - visualise your TF tree, missing transforms are the #1 Nav2 failure cause `ros2 doctor` - general health check Make sure your /odom → /base_link and /base_link → /laser_frame TF transforms are correct. Wrong transforms make SLAM useless. Set use_sim_time:=false when running on real hardware
