2.5 Additional Exercise

2.5.1 Hardware Setup

You'll need:

  • Camera/Webcam

  • Jumper wires

  • Arduino Nano (or Uno)

  • USB cable to connect Arduino to PC

  • External 5V power supply (if required)

  • Red LED

  • Blue LED

  • 2 x 220Ω resistors

  • 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).


2.5.2 Writing ROS Node

We will build system in ROS combine detection in face detection with Arduino LEDs. When face detected, the Blue Led will light up and Red Led will turn off, vise versa when no face detected.

Create a ROS package if you haven’t already:

Before editing the python code, make sure already install all necessary python library.

code for python

ser_facedetection.py:

Make sure to modify /dev/ttyUSB0 to match the correct serial port for your Arduino.

Also look for your camera index and modify the code cv2.VideoCapture(0)


2.5.3 Uploading Arduino Code

This Arduino code will receive commands via serial communication from the ROS 2 node and control the LEDs accordingly. Connect the Red LED to Pin 7 and the Blue LED to Pin 8.

Arduino Sketch:


2.5.4 Running the Code

  1. Open a terminal on your PC and source ROS 2:

  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