2.2 Controlling LEDs with ROS 2
Next, we'll integrate two LEDs with the Arduino. These will be controlled through ROS commands.
2.2.1 Hardware Setup
You'll need:
Red LED
Blue LED
2 x 220Ω resistors
Jumper wires
Breadboard
Wiring:
Connect the Red LED to digital pin 7 (with the resistor in series).
Connect the Blue LED to digital pin 8 (with the resistor in series).
Connect the grounds to the Arduino GND pin.
2.2.2 Writing ROS Node for Serial Communication
The first step is writing a simple ROS node in Python that sends a command over serial to the Arduino.
Create a ROS package if you haven’t already:
You will now have a new folder within your workspace’s src directory called ros_arduino_basic.
Here’s a simple ROS node that communicates with the Arduino:
ser_leds.py:
Make sure to modify /dev/ttyUSB0 to match the correct serial port for your Arduino.
2.2.3 Uploading Arduino Code
Now, upload a simple Arduino sketch to receive data from ROS.
Arduino Sketch:
Upload this code to your Arduino using the Arduino IDE.
2.2.4 Running the Code
Open a terminal on your PC and source ROS 2:
Run the ROS node:
Now you can see commands being sent from ROS to Arduino. Check the Arduino Serial Monitor to see the received data.
Last updated