How to Control an LED with ROS and ESP32
Introduction
In this tutorial, we'll learn how to control an LED using the Robot Operating System (ROS) and an ESP32 development board. ROS is a popular framework for building robotics applications, and it provides a convenient way to communicate between different nodes in a system. We'll use ROS to send commands to the ESP32, which will turn an LED on or off.
Prerequisites
Before we get started, you'll need the following:
- An ESP32 development board
- A breadboard
- An LED
- A 220 ohm resistor
- Jumper wires
- A computer running Ubuntu 20.04 with ROS installed
Setting up the Hardware
First, let's connect the LED to the ESP32. Insert the longer leg of the LED into pin 2 on the ESP32, and the shorter leg into the breadboard. Connect one end of the resistor to the same row as the shorter leg of the LED, and the other end to the ground rail on the breadboard.
Creating a ROS Node
Next, let's create a ROS node that will control the LED. Here's the code we'll use:
arduino#include <ros.h>
#include <std_msgs/Empty.h>
ros::NodeHandle nh;
const int LED_PIN = 2;
void messageCb(const std_msgs::Empty& toggle_msg) {
digitalWrite(LED_PIN, HIGH - digitalRead(LED_PIN)); // blink the led
}
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb);
void setup() {
pinMode(LED_PIN, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop() {
nh.spinOnce();
delay(1);
}
This code listens for messages on the /toggle_led
topic, and toggles the LED on or off when a message is received. We'll publish messages to this topic later to control the LED.
Save this code to a file called toggle_led.cpp
, and compile it using the following command:
css$ arduino-cli compile --fqbn esp32:esp32:esp32 toggle_led.cpp
This will generate a binary file called toggle_led.esp32.esp32.esp32.bin
.
Uploading the Binary to the ESP32
Now, let's upload the binary to the ESP32. Connect the ESP32 to your computer via USB, and run the following command:
css$ arduino-cli upload -p /dev/ttyUSB0 --fqbn esp32:esp32:esp32 toggle_led.cpp
This will upload the binary to the ESP32, and start the toggle_led
node.
Controlling the LED
Finally, let's send messages to the /toggle_led
topic to turn the LED on or off. Open a new terminal window, and run the following command to turn the LED on:
rust$ rostopic pub /toggle_led std_msgs/String "data: 'on'"
The LED should turn on. To turn it off, run the following command:
rust$ rostopic pub /toggle_led std_msgs/String "data: 'off'"
And that's it! You should now be able to control the LED using ROS and the ESP32.
Conclusion
In this tutorial, we learned how to control an LED using ROS and an ESP32 development board. We created a ROS node that listens for messages on the /toggle_led
topic, and toggles the LED on or off when a message is received. We then uploaded the binary to the ESP32, and sent messages
arduino#include <ros.h>
#include <std_msgs/Empty.h>
ros::NodeHandle nh;
const int LED_PIN = 2;
void messageCb(const std_msgs::Empty& toggle_msg) {
digitalWrite(LED_PIN, HIGH - digitalRead(LED_PIN)); // blink the led
}
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb);
void setup() {
pinMode(LED_PIN, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop() {
nh.spinOnce();
delay(1);
}
And here is the modified listener.py
code to turn the LED on and off using the rostopic pub
command:
python#!/usr/bin/env python
import rospy
from std_msgs.msg import String
LED_PIN = 2
def callback(data):
if data.data == "on":
rospy.loginfo("Turning LED on")
GPIO.output(LED_PIN, GPIO.HIGH)
elif data.data == "off":
rospy.loginfo("Turning LED off")
GPIO.output(LED_PIN, GPIO.LOW)
def listener():
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("toggle_led", String, callback)
rospy.spin()
if __name__ == '__main__':
try:
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_PIN, GPIO.OUT)
listener()
except rospy.ROSInterruptException:
pass