Prerequisites
Before diving in, make sure you have the following ready:
- A computer running Ubuntu 20.04 with ROS Noetic installed
- An Arduino board (Uno, Mega, or Nano recommended for beginners)
- The Arduino IDE (version 1.8.x or 2.x)
- A USB cable to connect Arduino to your PC
Step 1: Install the rosserial Packages
Open a terminal and install the necessary ROS packages:
sudo apt-get update
sudo apt-get install ros-noetic-rosserial-arduino
sudo apt-get install ros-noetic-rosserial
These packages include the server-side ROS node and the tools needed to generate the Arduino client library.
Step 2: Generate and Install the ros_lib Arduino Library
The Arduino side of rosserial uses a library called ros_lib. You need to generate it from your ROS installation so it includes all the message types available on your system.
- Navigate to your Arduino libraries folder (usually
~/Arduino/libraries/) - Run the library generator:
cd ~/Arduino/libraries
source /opt/ros/noetic/setup.bash
rosrun rosserial_arduino make_libraries.py .
You should now see a ros_lib folder inside your Arduino libraries directory. Restart the Arduino IDE if it was already open.
Step 3: Write Your First rosserial Sketch
Let's write a simple publisher that sends a "Hello World" string message from Arduino to ROS.
In the Arduino IDE, create a new sketch and paste the following code:
#include <ros.h>
#include <std_msgs/String.h>
ros::NodeHandle nh;
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);
char hello[] = "Hello from Arduino!";
void setup() {
nh.initNode();
nh.advertise(chatter);
}
void loop() {
str_msg.data = hello;
chatter.publish(&str_msg);
nh.spinOnce();
delay(1000);
}
This sketch initializes a ROS node handle, advertises a topic called chatter, and publishes a string message every second.
Step 4: Upload the Sketch to Arduino
- Connect your Arduino via USB
- In the Arduino IDE, select the correct Board and Port under the Tools menu
- Click Upload and wait for the "Done uploading" confirmation
Step 5: Launch the rosserial Server Node
Back on your Ubuntu machine, open a new terminal and start the ROS master:
roscore
In another terminal, launch the rosserial server and tell it which serial port your Arduino is on:
source /opt/ros/noetic/setup.bash
rosrun rosserial_python serial_node.py /dev/ttyUSB0
Note: Replace /dev/ttyUSB0 with the actual port your Arduino is assigned. Check with ls /dev/tty* before and after connecting the Arduino to identify it.
Step 6: Verify the Messages Are Flowing
Open a third terminal and echo the /chatter topic:
source /opt/ros/noetic/setup.bash
rostopic echo /chatter
If everything is working, you'll see output like:
data: "Hello from Arduino!"
---
data: "Hello from Arduino!"
---
Congratulations — your Arduino is now a ROS node!
Common Baud Rate Setting
By default, rosserial communicates at 57600 baud. If you need to change this, you can pass it as an argument to the node handle in your sketch:
nh.getHardware()->setBaud(115200);
And specify the baud rate when launching the server:
rosrun rosserial_python serial_node.py /dev/ttyUSB0 _baud:=115200
Next Steps
Now that you have a working rosserial connection, consider exploring:
- Subscribing to topics from ROS (e.g., controlling an LED with a Boolean message)
- Publishing sensor data such as ultrasonic range readings
- Using custom message types for more complex data