Part 4: Adding Obstacle Detection
Welcome to Part 4 of the Raspberry Pi Pico W Robotics Course! In this section, we’ll integrate the HC-SR04 ultrasonic sensor to detect obstacles and use Neopixel LEDs to give visual feedback based on proximity. By the end of this tutorial, your robot will be able to sense objects and change LED colors according to the measured distance.
What You’ll Do in Part 4
In this part, we’ll focus on enhancing your robot’s autonomy by adding obstacle detection and LED indications. Here’s what you’ll accomplish:
- Understand how the HC-SR04 ultrasonic sensor works.
- Measure distance in centimeters using MicroPython.
- Use Neopixel LEDs to change colors based on proximity.
- Experiment with random color animations.
Let’s dive in!
Step 1: Understanding the HC-SR04 Ultrasonic Sensor
The HC-SR04 ultrasonic sensor measures distance by emitting sound waves and timing how long they take to return. It has four pins:
- VCC (5V power supply)
- GND (ground)
- Trig (trigger pin to start measurement)
- Echo (echo pin to read the return signal)
Typical measurable range is from 2 cm to 400 cm, with an accuracy of around 3 mm.
Step 2: Setting Up Neopixel LEDs
Neopixel LEDs (WS2812B or similar) allow you to control multiple RGB LEDs using just one data line. You can set each LED to display a variety of colors. In this project, we’ll use them to visually indicate proximity:
- Red for very close objects.
- Yellow for moderate distance.
- Green for objects further away.
Step 3: The Combined Code
The following script measures distance using the HC-SR04, then changes Neopixel LED colors accordingly. It also includes a fun mode to randomly light up LEDs with different colors. Copy and paste this into your Thonny editor or any MicroPython environment on your Raspberry Pi Pico W.
"""
This script uses an ultrasonic sensor (HC-SR04) and Neopixel LEDs on a Raspberry Pi Pico.
It measures distance using the ultrasonic sensor and changes the LED colors based on proximity:
- Red (<10 cm)
- Yellow (10-20 cm)
- Green (>20 cm)
Additionally, it includes a function to randomly light up LEDs with different colors.
Module used:
https://www.amazon.com/Ultrasonic-Compatible-Distance-Avoidance-Raspberry/dp/B0BRMP73C2
Neopixel helper library found at shillehtek.com
"""
from machine import Pin, time_pulse_us
import random
import time
from neopixel import Neopixel
# Initialize ultrasonic sensor pins and Neopixel LEDs.
# Defines trigger/echo pins for distance measurement and sets up LED colors.
SOUND_SPEED = 340
TRIG_PULSE_DURATION_US = 10
TRIG_PIN = Pin(15, Pin.OUT)
ECHO_PIN = Pin(14, Pin.IN)
# Set params for Neopixel
NUMPIX = 6
PIXELS = Neopixel(NUMPIX, 0, 28)
# Define LED colors
YELLOW = (255, 100, 0)
ORANGE = (255, 50, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
LAVENDER = (230, 230, 250)
# Color dictionary for assigning random colors
COLOR_DICT = {
0: YELLOW,
1: ORANGE,
2: GREEN,
3: BLUE,
4: RED,
5: LAVENDER
}
def get_distance():
'''Returns distance in centimeters'''
TRIG_PIN.value(0)
time.sleep_us(5)
TRIG_PIN.value(1)
time.sleep_us(TRIG_PULSE_DURATION_US)
TRIG_PIN.value(0)
ultrason_duration = time_pulse_us(ECHO_PIN, 1, 30000) # 30ms timeout
distance_cm = SOUND_SPEED * ultrason_duration / 20000
print(f"Distance : {distance_cm} cm")
return distance_cm
def change_color_with_distance():
'''
Continuously measures distance and changes LED color based on proximity:
- <10 cm: RED
- 10–20 cm: YELLOW
- >20 cm: GREEN
'''
while True:
distance = get_distance()
if distance < 10:
color = RED
elif distance < 20:
color = YELLOW
else:
color = GREEN
# Set all 6 LEDs to the same color
for i in range(NUMPIX):
PIXELS.set_pixel(i, color)
PIXELS.show()
time.sleep(1)
def fun():
'''
Loops through random colors for the 6 LEDs.
Demonstrates how to randomly assign colors and
can be modified further for brightness, effects, etc.
'''
while True:
random_led = random.randint(0, 5)
random_color = random.randint(0, len(COLOR_DICT) - 1)
print(random_led, COLOR_DICT[random_color])
PIXELS.set_pixel(random_led, COLOR_DICT[random_color])
PIXELS.show()
time.sleep(0.5)
# Start the main distance-based color function:
change_color_with_distance()
In this code, change_color_with_distance()
continuously checks the distance and updates all LEDs to a color that reflects how close an obstacle is. The fun()
function is an alternative loop that assigns random colors to random LEDs. You can comment out one function call and use the other, depending on which feature you want to test.
Step 4: Testing and Tweaking
- Upload this script to your Raspberry Pi Pico W.
- Open the serial console to watch distance readings in real time.
- Move an object closer and farther to see the LED colors change.
- Experiment with
time.sleep()
values and color thresholds to find what works best for your project.