Controlling a Turtlebot Arm with an Arduino Ultrasonic Sensor

From COINS Research Group
Jump to: navigation, search

Tutorial Overview


Simple program for Turtlebot Arm PhantomX Pincher with ROS and Arduino (UNO) board


Abdelrahman Mahmoud


This tutorial was developed for ROS Hydro and Indigo versions. For any suggestions/comments about this tutorial, please send an email to / Make sure that you complete the previous tutorial and download the Turtlebot Arm Robot package.

Make sure you download:

  1. Arduino 1.0.6 version
  2. Rosserial_arduino setup package
  3. Make sure to complete this tutorial to have a background about the Listener.cpp file and how it functions.
  4. Download the ArbotiX package for Arduino for the d2xx package.

After downloading the d2xx package follow the readme file to install


  • Ultrasonic Ranger v1.0, (Or any other one just make sure you are able to read the data/range from).

ultrasonic sensor and button connection with UNO board


In this tutorial, I will present an integration and the steps to sync a Turtlebot Arm PhantomX Pincher with an ultrasonic sensor using UNO arduino board with a C++ (listener) file for the turtlebot arm robot and an arduino file (publisher) for the UNO board with ROS. When an object comes in front of the sensor by a certain range (2 cm for example) the Gripper will close and it will open when the distance is > 2cm. You can open the gripper by pressing the button it will ignore the sensors command and open the gripper.

Main Steps

Let's divide this tutorial into two parts:

1-The Publisher: an arduino program with ros library that will allow us to publish a certain topic carrying a value that you can define according to the type of message/data you want (Boolean, String, char...etc). This program reads the data from the ultrasonic sensor and the button and send them in two different messages/topics. Every topic can have a single value of a single type as mentioned before.

2-The Listener: a C++ program that can receive the topic that was published from the arduino program and give the command to the Gripper either to open or to close so it controls the turtlebot arm robot.

The Arduino Program:

//Include the sensor_msgs/Range.h library for the Ultrasonic sensor
//Include the std_msgs/Bool.h library for the button
#include <ros.h>
#include <sensor_msgs/Range.h>
#include <std_msgs/Bool.h>
#include "Ultrasonic.h"

//Create the node handler
ros::NodeHandle  nodeHandler;

//Initializing the messages that will be published in each topic
sensor_msgs::Range range_msg;
std_msgs::Bool pushed_msg;

//Initializing the Publishers for the two topics
ros::Publisher pub_range( "/ultrasound", &range_msg);
ros::Publisher pub_button("/buttonPushed", &pushed_msg);

char frameid[] = "/ultrasound";

//Define the pins in your UNO board
Ultrasonic ultrasonic(4);
const int button_pin = 7;

void setup() {


 //Setup your range_msg 
 range_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
 range_msg.header.frame_id =  frameid;
 range_msg.field_of_view = 0.1;
 range_msg.min_range = 0.0;
 range_msg.max_range = 3.5;

 //Initialize an input pin for our push button
 pinMode(button_pin, INPUT);
 digitalWrite(button_pin, HIGH);

void loop()

 long RangeInCentimeters;
 RangeInCentimeters = ultrasonic.MeasureInCentimeters();

 //Save the range value between the sensor and the object in the range_msg message
 range_msg.range = RangeInCentimeters;
 range_msg.header.stamp =;

 //publish the first topic "/ultrasonic"

 //Save the value of the button_pin to the pushed_msg message = digitalRead (button_pin);
 //publish the second topic "/buttonPushed"


Make sure you include the right Ultrasonic.h library or you can get the Range value from your ultrasonic sensor using any other method but you have to save the final range value in the (range_msg) message.


You have to be careful the range value is in “inch” or “cm”.

The Turtlebot Arm robot program:

Designed not only for this tutorial and it is a bit advanced for this one but we will describe the parts that is used in this tutorial.file link

The part we are using is the ultrasonic-arm-program-methods and the values used in the straightGripper() , openGripper() and closeGripper() are based on observation. You can change the value to compare the range with in the “if condition” in the ultrasonicCallback() method.

Note: Make sure that you put the file in the right place and changed your CMakeLists.txt file

Executing step

1. Upload the ros program in the (ArbotiX Sketches) to the turtlebot arm robot and make sure you picked the right Board and the right Serial Port.

2. Run this command : sudo chmod 777 /dev/ttyUSB0

To give the permission to the port you are using. Note: make sure you give the permission to the right port.

3. roslaunch turtlebot_arm_bringup arm.launch

To launch the arm.launch file and load the drivers for the robot. =Note:= make sure that the arm robot is connected to the same port in the arm.yaml file.

4. rosrun beginner_tutorials armcontrol

Where beginner_tutorials is the package containing the listener file and the armcontrol in the listener file.

Note Make sure that you compile the work space before loading anything to avoid errors. Follow the link to know how.

5. Upload the Arduino program to your board. Make sure that you choose the right Board and Serial Port.

6. rosrun rosserial_python /dev/ttyACM0

where the ttyACM0 is the port for the Arduino board.

Note If it fails uploading in step 5 or fails running in step 6 repeat step 2 but for the arduino board's port.

7. Try putting your arm in front of the ultrasonic sensor.