Detect The Line Tracking

  1. Introduction:

This experiment aims to detect lines using an IR sensor and phototransistor setup interfaced with a Raspberry Pi Pico. The system is designed to read sensor values and determine the presence of a line on a surface. This type of experiment is commonly used in applications like line-following robots, where sensors are employed to detect the difference between light and dark surfaces (lines) and provide feedback to the control system.



  1. Scope:

The scope of this experiment includes:

  1. Setting up an 8-channel line-tracking module.

  2. Writing and implementing code in both MicroPython and C to read sensor outputs.

  3. Displaying the sensor states, replacing 0 with a space (indicating a line detected) and 1 with an underscore (indicating no line detected).

  4. Testing and validating the system by printing sensor outputs to a console.

  5. Demonstrating the real-time detection of lines using the Raspberry Pi Pico.





  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • 8-channel line tracking sensor (IR and phototransistor-based).

  • Breadboard

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.



  1. Connection Details:

The connection details for the hardware setup are as follows:

  • Line sensor outputs are connected to Raspberry Pi Pico GPIO pins.

  • IR control pin is connected to one GPIO pin to manage the IR LEDs.

  • Power (VCC) and Ground (GND) are connected to the Pico’s 3.3V and GND pins respectively.

Sensor Output

Pico GPIO Pin

OUT1

GPIO 15

OUT2

GPIO 14

OUT3

GPIO 13

OUT4

GPIO 12

OUT5

GPIO 11

OUT6

GPIO 10

OUT7

GPIO 9

OUT8

GPIO 8

IR Control

GPIO 16


UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).





  1. Working Principle:

The IR and phototransistor pair work on the principle of detecting light reflected from a surface.

  • When the IR light hits a light-colored surface (like white), the light is reflected, and the phototransistor detects it, resulting in a HIGH signal (1).

  • When the IR light hits a dark-colored surface (like a black line), little to no light is reflected, and the phototransistor detects a LOW signal (0).

  • The Raspberry Pi Pico reads the sensor outputs and prints the results, replacing 0 with a space and 1 with an underscore.

The 8-Ch way Line Detection Tracking IR Infrared Sensor Module infrared photoelectric switch adopts a high transmit power infrared photodiode and a highly sensitive phototransistor. It works by applying the principle of objects’ reflecting IR light – the light is emitted, then reflected, and sensed by the synchronous circuit.

8-Ch way Line Detection Tracking IR Infrared Sensor Module determines whether there exists an object or not by the light intensity. It can easily identify black and white lines. This module is an infrared tracking sensor one that uses a TRT5000 sensor. The blue LED of TRT5000 is the emission tube and after electrified it emits infrared light invisible to human eye. The black part of the sensor is for receiving; the resistance of the resistor inside changes with the infrared light received.

This 8-Ch way Line Detection Tracking IR Infrared Sensor Module has 8 IR LED/phototransistor pairs mounted on a 0.375″ pitch, making it a great detector for a line-following robot. Each sensor provides a separate digital I/O-measurable output. This reflection sensor array is intended as a line sensor, but it can be used as a general-purpose proximity or reflection sensor. The module is a convenient carrier for eight IR emitter and receiver pairs evenly spaced at intervals of 0.375″ (9.525mm). The lower output voltage is an indication of greater reflection.

Each of the eight infrared reflectance sensors operates independently. Each sensor has a capacitor that is used to establish an RC timing circuit used to determine the amount of reflectance based on time.

8-Ch way Line Detection Tracking IR Infrared Sensor Module can be used with both 5V and 3.3V microcontrollers depending on a jumper setting. The board consumes about 100mA when active and the infrared LEDs can be powered off during idle times to conserve energy at about 10mA.

Features: –

  • Voltage: 3.3V-5V

  • Supply current: 100 mA

  • Output Type: Digital

  • Two 3mm mounting holes

  • 8- way reflective optical sensors

  • Optimal sensing distance: 0.125″ (3mm)

  • Maximum recommended sensing distance: 0.375″ (9.5mm)

  • TCRT5000 infrared photoelectric sensor on each of the 8 line-following modules

  • Sensing Distance12mm , 8mm will be the best

  • Adopts I2C ports which only occupies 4 pins of the MCU

  • Positioning holes on the board for easy assembly









  1. Application code:

  • MicroPython:

from machine import Pin

import time


# Define the GPIO pins connected to the line sensors

sensors = [

Pin(15, Pin.IN),

Pin(14, Pin.IN),

Pin(13, Pin.IN),

Pin(12, Pin.IN),

Pin(11, Pin.IN),

Pin(10, Pin.IN),

Pin(9, Pin.IN),

Pin(8, Pin.IN)

]


# Define the IR control pin

ir_pin = Pin(16, Pin.OUT)


while True:

# Turn on the IR LEDs

ir_pin.value(1)


# Read the sensor states

sensor_states = [sensor.value() for sensor in sensors]


# Replace 1 with "_" and 0 with " " (space) and print the output

output = ''.join([' ' if state == 0 else '_' for state in sensor_states])

print("Sensor States:", output)

# Delay to slow down the output rate

time.sleep(0.5)


# Optional: Turn off the IR LEDs to save power

ir_pin.value(0)

time.sleep(0.5) # Delay with IR LEDs off


  • Embedded C:

#include "pico/stdlib.h"


// Define the GPIO pins for the line sensor outputs

#define OUT1 15

#define OUT2 14

#define OUT3 13

#define OUT4 12

#define OUT5 11

#define OUT6 10

#define OUT7 9

#define OUT8 8

#define IR_PIN 16 // Define the IR control pin


int main() {

// Initialize stdio for output

stdio_init_all();


// Initialize the GPIO pins for each sensor

gpio_init(OUT1);

gpio_init(OUT2);

gpio_init(OUT3);

gpio_init(OUT4);

gpio_init(OUT5);

gpio_init(OUT6);

gpio_init(OUT7);

gpio_init(OUT8);

gpio_init(IR_PIN);


// Set all sensor pins as input

gpio_set_dir(OUT1, GPIO_IN);

gpio_set_dir(OUT2, GPIO_IN);

gpio_set_dir(OUT3, GPIO_IN);

gpio_set_dir(OUT4, GPIO_IN);

gpio_set_dir(OUT5, GPIO_IN);

gpio_set_dir(OUT6, GPIO_IN);

gpio_set_dir(OUT7, GPIO_IN);

gpio_set_dir(OUT8, GPIO_IN);

// Set IR_PIN as output to control IR LEDs

gpio_set_dir(IR_PIN, GPIO_OUT);


while (true) {

// Turn the IR LEDs on by setting the IR_PIN high

gpio_put(IR_PIN, 1);


// Read sensor states and display space for 0 and "_" for 1

printf("Sensor States: ");

printf("%c", gpio_get(OUT1) == 0 ? ' ' : '_');

printf("%c", gpio_get(OUT2) == 0 ? ' ' : '_');

printf("%c", gpio_get(OUT3) == 0 ? ' ' : '_');

printf("%c", gpio_get(OUT4) == 0 ? ' ' : '_');

printf("%c", gpio_get(OUT5) == 0 ? ' ' : '_');

printf("%c", gpio_get(OUT6) == 0 ? ' ' : '_');

printf("%c", gpio_get(OUT7) == 0 ? ' ' : '_');

printf("%c\n", gpio_get(OUT8) == 0 ? ' ' : '_');


// Small delay to prevent overwhelming output

sleep_ms(500);


// Optional: Turn the IR LEDs off by setting the IR_PIN low to save power

gpio_put(IR_PIN, 0);

sleep_ms(500); // Delay with IR LEDs off

}


return 0;

}



  1. Hardware Image:



  1. Output:

When the program is running, the console will display the states of the sensors as:


Sensor States: _ _ _ _ _




  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Initialize the Pico SDK

include(pico_sdk_import.cmake)


project(line_tracking)


# Initialize the Pico SDK

pico_sdk_init()


# Add executable

add_executable(line_tracking main.c)


# Link the standard Pico libraries

target_link_libraries(line_tracking pico_stdlib)


# Enable USB output, disable UART for stdio

pico_enable_stdio_usb(line_tracking 1)

pico_enable_stdio_uart(line_tracking 0)


# Create map/bin/hex/uf2 files

pico_add_extra_outputs(line_tracking)

  1. References:

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)