Tutorial: Using Raspberry Pi Pico and HW-488 Infrared Obstacle Avoidance Sensor to Play/Pause a Video on Your PC via USB

 


In this tutorial, we'll demonstrate how to use the Raspberry Pi Pico and an HW-488 Infrared Obstacle Avoidance Sensor to control video playback on your PC. The setup will allow you to automatically play or pause a video based on your proximity to the sensor. When you are in front of the sensor, the video will play; when you step away, the video will pause.

We'll use CircuitPython to achieve this, as it has built-in support for USB Human Interface Devices (HID), allowing the Pico to act like a USB keyboard.

Prerequisites

Hardware:

  • Raspberry Pi Pico
  • HW-488 Infrared Obstacle Avoidance Sensor
  • Jumper wires
  • USB cable for Pico connection to the PC

Software:

  • CircuitPython firmware for Raspberry Pi Pico
  • Adafruit HID Library for CircuitPython

Step 1: Setup CircuitPython on Raspberry Pi Pico

To begin, we need to install CircuitPython on the Raspberry Pi Pico.

Installing CircuitPython:

  1. Visit the CircuitPython download page.
  2. Download the latest .uf2 file for Raspberry Pi Pico.
  3. Hold the BOOTSEL button on the Pico, then connect it to your computer via USB.
  4. Release the button once connected. Your Pico should appear as a mass storage device.
  5. Drag and drop the .uf2 file onto the Pico's drive. The Pico will reboot, and a CIRCUITPY drive will now appear on your computer.
or simply use Thonny.

Step 2: Setup the Adafruit HID Library

To enable the USB keyboard functionality, we need to install the Adafruit HID Library on the Pico.

Installing the Adafruit HID Library:

  1. Download the Adafruit CircuitPython Library Bundle.
  2. Extract the zip file, and locate the adafruit_hid folder within the lib directory.
  3. Copy the adafruit_hid folder into the lib directory on the CIRCUITPY drive.

Step 3: Connect the HW-488 Infrared Sensor to Raspberry Pi Pico

Now, let's connect the HW-488 Infrared Sensor to the Pico. This sensor detects obstacles by sending infrared light and measuring reflections.

Wiring:

  • VCC (Sensor) → 3.3V (Pico)
  • GND (Sensor) → GND (Pico)
  • OUT (Sensor) → GPIO 15 (Pico)


Step 4: Write and Upload the Code

Here’s the CircuitPython code that will monitor the sensor and act as a USB keyboard to send the spacebar key (typically used for playing and pausing videos in most media players).

Code:

import board
import digitalio
import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

# Set up the sensor and keyboard
sensor = digitalio.DigitalInOut(board.GP15)  # Assuming sensor connected to GPIO 15
sensor.direction = digitalio.Direction.INPUT
keyboard = Keyboard(usb_hid.devices)

# Variables to track the state
sensor_active = False

                                                                                                                                                                      # Delay to prevent multiple triggers
delay_time = 1  # 1 second delay

while True:
    if not sensor.value and not sensor_active:
        # If the sensor detects an object (like you're in front of it)
        print("Object detected, playing video...")
        keyboard.press(Keycode.SPACE)  # Press space to play/pause
        keyboard.release_all()
        sensor_active = True
        time.sleep(delay_time)

    elif sensor.value and sensor_active:
        # If no object is detected
        print("No object detected, pausing video...")
        keyboard.press(Keycode.SPACE)  # Press space again to pause
        keyboard.release_all()
        sensor_active = False
        time.sleep(delay_time)

    time.sleep(0.1)

Steps:

  1. Open a text editor or an IDE that supports CircuitPython (such as Mu Editor).
  2. Copy the code above into the editor.
  3. Save the file as code.py onto the CIRCUITPY drive. This will automatically run the code when the Pico is powered.

Step 5: Test Your Setup

With everything connected and the code uploaded, it’s time to test the setup:

  1. Run a video: Open a media player on your PC (such as VLC or Windows Media Player) or YouTube
    and start playing a video.
  2. Trigger the sensor: Move your hand or body in front of the IR sensor. The video should automatically play when you're in front of the sensor and pause when you move away.

The Raspberry Pi Pico will act as a USB keyboard, pressing the spacebar to control the video playback.


Troubleshooting

  • No action when in front of the sensor?
    • Check the wiring connections, especially the signal pin (GPIO 15) and make sure the sensor is powered correctly.
  • Multiple triggers?
    • If the video is rapidly pausing and playing, try increasing the delay_time value in the code to debounce the sensor detection.
  • No HID functionality?
    • Ensure the adafruit_hid folder is correctly copied to the lib directory on the CIRCUITPY drive.

Conclusion

Congratulations! You've successfully set up a Raspberry Pi Pico and an infrared sensor to control video playback on your PC. This project showcases how the Pico can be used as a USB HID device, interacting with your computer through sensor inputs. You can modify this setup to perform other tasks like controlling slideshows, presentations, or even creating a touchless control system for media.

Comments

Popular posts from this blog

Tutorial: Using Raspberry Pi as an OpenVPN Client Router with Ethernet Sharing

How to Use a Raspberry Pi 4 as a Firewall to Block Adult Content and Protect Your Family