How to use rosserial with two Arduinos and Raspberry Pi

Arduino is a great development board for reading data from various sensors and controlling the robot’s DC motors. Raspberry Pi is good at running ROS on Linux. So, we can benefit from both systems to build smart robots. The easiest way is to connect these boards via USB cable and make them communicate through ROS nodes.

In this tutorial, I’ll show you how to use two Arduino boards as an extension of the Raspberry Pi computer. Before starting writing the ROS nodes, I have to set the Pi to identify each Arduino. This identification becomes necessary when the robot’s architecture is complex.

Only one ROS node can run on an Arduino board. And because I have two Arduino, I will use one to generate a random number and another to control the LED connected to pin 13.

How To use rosserial with Two Arduinos and Raspberry Pi

I will run one ROS nodes that will send and receive data according to this flowchart

The description of the flowchart

  • The user will start all ROS nodes by running a .launch file.
  • The first Arduino board will run a random number script and send data to Raspberry Pi.
  • A ROS node will receive a random number from the first Arduino board. The node will run on Raspberry Pi and will command the LED on the second Arduino board.
  • The second Arduino board will turn ON and OFF the LED (pin 13) depending on the commands received from the ROS node running on the Pi board.

The ROS node to generate a random number

Testing the node
Step 1: Open a Linux Terminal and type the command:


Step 2: Open a second Linux Terminal and type the following command:

rosrun rosserial_python /dev/ttyACM*

Step 3: To see the random numbers generated by the Arduino node, open a third Terminal and type the following command:

rostopic echo /random_number

The ROS node that displays and calculates the LED’s stage

This ROS node in Python will run on Raspberry Pi. Before you start writing the Python code, you must create the workspace and the package that contains the node. All you need to do is in this article.

The ROS node that controls the LED

Write the launch file

<node pkg=”rosserial_python” type=”” name=”twoArduino_LED” output=”screen”>
<param name=”port” value=”/dev/ttyACM0″/>
<param name=”baud” value=”57600″/>
<node pkg=”rosserial_python” type=”” name=”twoArduinos_RandNo” output=”screen”>
<param name=”port” value=”/dev/ttyACM1″/>
<param name=”baud” value=”57600″/>
<node name=”random_number” pkg=”pi_and_arduino” type=”” output=”screen” />



9 comments » Write a comment

  1. When I try to compile the ROS node that controls the LED for arduino I get the error message :

    Invalid use of template-name ros::Subscriber without an agument list

  2. I believe the board receiving the control commands should rather be subscribed to the ‘LED’ topic. Could have been a simple omission.

  3. In the last script, there is a typo in
    ros::Subscriber sub(“OFF”, &messageCb);
    The arduino node should subscribe to “LED” topic not “OFF”
    ros::Subscriber sub(“LED”, &messageCb);

  4. Are the arduino’s communication over their USB cables to the rPi? You didn’t day anything about connection the serial pin of the arduino to the serial pin of the Rpi, so it must be using USB.

  5. There is still a typo in the last script; “The ROS node that controls the LED”
    The following would not compile…
    ros::Subscriber sub(“LED”, &messageCb);

    This worked on my setup…
    ros::Subscriber sub(“LED”, &messageCb);

    Thanks for the tutorial.

  6. My previous reply comment did not appear as I typed it.
    “ros::Subscriber sub(“LED”, &messageCb);”

    std_msgs::String needs to go between row::Subscriber and sub(“LED”, &messageCb); and it needs to be enclosed with “”

    ros::Subscriber sub(“LED”, &messageCb);

Leave a Reply