Search Code

Lesson 7 of 10: Line Tracking, Cliff Detection Calibrate Greyscale Sensor Raspberry Pi Pico Car

This lesson is part of: Raspberry Pi Pico 4WD Smart Car

Lesson 7 of 10: Line Tracking, Cliff Detection Calibrate Greyscale Sensor Raspberry Pi Pico Car

In this seventh lesson of the Raspberry Pi Pico Smart Car course, we explore the crucial functions of line tracking and cliff detection using a grayscale sensor module. This module acts as the "eyes" of your car, allowing it to follow a predetermined path autonomously and avoid dangerous drops. The project demonstrates how to calibrate the sensor for accurate readings and implement logic that keeps the car on track, with a fun "Don't Push Me" feature that simulates cliff detection. This guide is perfect for anyone looking to move beyond basic remote control and dive into the world of sensor-guided robotics.

This technology has numerous practical applications beyond just a fun DIY project. The principles you'll learn here are foundational for building real-world autonomous systems. Here are a few ideas to inspire you:

  • Automated Delivery Robots: Build a small robot that can navigate a warehouse or office floor by following marked lines to deliver items.
  • Tabletop Maze Solver: Create a robot that can navigate a complex maze drawn with black electrical tape.
  • Safety and Security Robot: Implement a patrol bot that follows a set path and uses cliff detection to avoid falling down stairs or off elevated surfaces.
  • Educational Platform: Use the car to teach the fundamentals of PID control, sensor fusion, and autonomous navigation.

Hardware and Components

This project builds upon the SunFounder Raspberry Pi Pico 4WD car kit. The key component for this lesson is the grayscale sensor module, which is essential for both line tracking and cliff detection. The module features three independent sensors that measure the amount of light reflected from the surface below. This data is used to distinguish between a black line, a white surface, and a "cliff" or open space. The full kit also includes the Pico car chassis, motors, and a battery pack to power the system.

Wiring Guide

Proper wiring is the first step to getting your sensor to work. The grayscale module connects to the Raspberry Pi Pico using four wires. The wiring is straightforward and is demonstrated clearly in the video.

Here is the wiring configuration as shown in the video (in video at 03:01):

  • Black Wire: Connect to a GND pin on the Pico.
  • Red Wire: Connect to the 3.3V pin on the Pico.
  • Yellow Wire (Right Sensor): Connect to GPIO pin 28.
  • Brown Wire (Center Sensor): Connect to GPIO pin 27.
  • White/Gray Wire (Left Sensor): Connect to GPIO pin 26.

Calibrating the Grayscale Sensor

Before the car can reliably follow a line, you must calibrate the sensor to understand the different readings for white, black, and empty space. This is a critical step to ensure your car makes the right decisions. The sensor's sensitivity can be adjusted with a small potentiometer on the module.

The video demonstrates the calibration process (in video at 04:39). The sensor should be positioned at an optimal height above the ground—around 11 millimeters or one centimeter—to get clear readings. By adjusting the potentiometer and observing the values, you can find the sweet spot. The goal is to have a significant difference between the readings for a white surface (which reflects a lot of light) and a black line (which reflects very little).

Here's a summary of what the readings mean:

  • White Surface: High reflection, resulting in high sensor values (e.g., around 20,000).
  • Black Line: Low reflection, resulting in low sensor values (e.g., around 3,000).
  • Cliff (Hollow Space): No reflection, resulting in the lowest sensor values (e.g., around 1,200).

Code Explanation

Since no code was provided for this lesson, we will focus on the conceptual logic and the key functions from the Robojax library that are used to make the car work. The video explains the two main functions you will use from the provided library.

Testing the Sensor

The first step is to test your sensor with a simple script. This script uses a function called get_grayscale_value() to read the raw values from the three sensors. It prints these values to the serial monitor so you can see how they change when the sensor is over different surfaces. This is useful for verifying your wiring and performing the calibration step.


# This is a conceptual example of the test function
from machine import Pin, ADC
import time

# Define the ADC pins for the grayscale sensors
grayscale_pins = [26, 27, 28]
sensors = [ADC(Pin(pin)) for pin in grayscale_pins]

while True:
    # Read the raw 16-bit values from the sensors
    values = [sensor.read_u16() for sensor in sensors]
    print("Left: {} Center: {} Right: {}".format(values[0], values[1], values[2]))
    time.sleep_ms(100)

Line Tracking Logic

For line tracking, the code doesn't use the raw analog values. Instead, it converts them to a simple digital status: 1 if the sensor is over the black line, and 0 if it is over the white surface. This is handled by a function that returns a list of three values (e.g., [0, 1, 0]). The main control loop then uses this list to determine the car's actions.

The logic is based on comparing the sensor status to predefined conditions. For example, if the status is [0, 1, 0], the car is perfectly on the line and should move forward. If the status is [1, 0, 0], the car has drifted left and needs to correct its path to the right. The video explains these conditions in detail (in video at 13:05), showing how different combinations of sensor readings correspond to different motor commands, such as turning left, turning right, or making a sharp correction.


# This is a conceptual example of the line-following logic
# Assume 'status' is a list like [0, 1, 0] from the grayscale sensor

if status == [0, 1, 0]:  # Center sensor on the line
    # Move forward
    pass
elif status == [1, 0, 0]:  # Left sensor on the line
    # Turn right
    pass
elif status == [0, 0, 1]:  # Right sensor on the line
    # Turn left
    pass
# ... other conditions for sharper turns

Live Project Demonstration

In the video's demonstration, the car is placed on a printed track with a thick black line. The car successfully follows the line, making turns and corrections as it moves along the path. The video highlights a common issue where wheels can come loose during sharp turns, and shows a simple solution of adding a small piece of tape to the wheel shaft for a tighter fit. Once this is fixed, the car navigates the entire track smoothly, demonstrating the effectiveness of the calibration and the line-following logic.

Video Chapters

  • [00:00] Introduction to Line Tracking and Cliff Detection
  • [03:01] Wiring the Grayscale Sensor Module
  • [04:39] Calibrating the Grayscale Sensor
  • [13:05] Understanding the Line-Following Logic
  • [19:43] Testing the Line-Tracking Code
  • [24:50] Live Demonstration on a Test Track
No code attached.

Resources & references

No resources yet.

Files📁

No files available.