Push button + blink LED using ROS on Raspberry Pi 2

The next example is handling input from a button. When we press the button, the code will publish to the led_blink topic and blink the LED. When the switch is off, the LED will also be OFF. The LED is connected to the 12th pin and GND, and the button is connected to the 11th pin and GND. The following image shows the circuit of this example. The circuit is also the same for Odroid:

Figure 26: LED + button in Raspberry Pi 2

The code for interfacing the LED and button is given next. The code can be saved with the name button.cpp inside the src folder:

#include "ros/ros.h" #include "std_msgs/Bool.h" ...

Get Mastering ROS for Robotics Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.