4.3 Line Patrol Sensor

Note: The line patrol sensor will be affected by light. Please run the program in an indoor environment without sunlight to reduce the interference of sunlight on the line patrol sensor. And keep the indoor light sufficient when patrolling the line.

1. Learning Objectives

  1. Learn to combine the line patrol sensor and OLED of the Raspberry Pi Pico 2/Pico mainboard and the car expansion board to conduct experiments.
  2. Understand the use of the line patrol sensor.

2. Hardware Usage

This course uses the line patrol sensor and OLED of the Pico 2/Pico mainboard and the car expansion board.

image-20220302151738285

The line patrol sensor is a sensor that uses infrared rays to process data. It has the advantages of high sensitivity. There is an infrared transmitting tube and an infrared receiving tube on the sensor. When the ground is black, it absorbs all light and the resistance of the receiving tube increases. When the ground is white, it reflects all light and the resistance of the receiving tube decreases. Then, through the voltage comparison circuit on the board, the detected state is converted into a value of 0/1.

3. Program Analysis

Code path: Code -> 2.Advanced course -> 3.Tracking sensor.py

from pico_car import SSD1306_I2C

Use SSD1306_I2C from pico_car.

import time

The “time” library. This library handles everything to do with time, from measuring it to inserting delays into your program. The unit is seconds.

from machine import Pin, I2C

The machine library contains all the instructions MicroPython needs to communicate with Pico and other MicroPython-compatible devices, extending the language of physical computing. Here, the Pin and I2C libraries are used.

i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)

Set the IIC 1 pin to SCL 15, SDA 14, and the frequency to 100000.

oled = SSD1306_I2C(128, 32, i2c)

Initialize the size of the OLED to 128*32 and pass the previously set IIC parameters into it.

Tracing_1 = machine.Pin(2, machine.Pin.IN)

Initialize pin 2 as the foot of line tracking sensor 1 and set it as input.

oled.text('T1', 5, 0)

Set the OLED to display the character 'T1' at position 5,0.

oled.show()

Display the set OLED content.

oled.fill(0)

Clear the set content and prepare for the next display.

Tracing_1.value()

The Tracing_1.value() function is used to detect the level of the corresponding port. In the above program, after identifying 0 and 1, the oled.pixel is used to draw on the OLED.

4. Experimental Phenomenon

After the program is downloaded, we can see that the first line of the OLED displays 'T1 T2 T3 T4', the second line displays the values ​​of each sensor, and the third line displays the detection of black or white through drawing. At the same time, the Shell will also display the detection results.

image-20220302151809246