Raspberry Pi Line Follower

Line Follower Sensors


Line Follower Sensors

The TCRT5000 reflective optical sensor consists of a paired infrared emitter and phototransistor in a single package. These sensors are useful for applications such as position sensors in rotary encoders, or for detection of reflective material or surfaces. Because of this, they are ideal for use in robotics applications which require an autonomous vehicle to follow a line or path.

Preparing for Use

The leads on the TCRT5000 are quite short, so cannot easily be used with jumper cables. To make them easier to work with, we soldered our sensors onto small pieces of matrix board. The matrix board we used has short strips of copper, so does not require the copper strips to be cut. As well as the sensor we also soldered short strips of PCB header cut from a right angled 36 way PCB header. These PCB pins would allow direct connection of jumper cables, to make the sensor easier to connect.

Soldered Line Follower

Soldered Sensor

Once the sensor had been prepared in this way, it was relatively simple to connect to various circuits for use with different boards.

Raspberry Pi

For use with any board, the line follower sensor still requires other components to be connected to it. To enable us to do this, we used a standard breadboard to allow these components to be easily connected. The diagram below shows the circuit that we used for connecting the line follower sensor to a Raspberry Pi 3.

Raspberry Pi Line Follower

The infrared emitter of the sensor is powered from the 3.3V supply via a 220Ω resistor to limit the current. The collector of the phototransistor is connected to the 3.3V supply via a 10k resistor, and is also connected to one of the GPIO pins of the Raspberry Pi. Thus, when no infrared light is reflected back onto the phototransistor, the voltage on the GPIO pin will register as digital high. However, infrared light reflecting onto the phototransistor will cause the voltage on the GPIO pin to register as digital low. A standard LED is also connected to one of the GPIO pins via a 330Ω resistor. This LED can be used to indicate the state of the sensor.

The Python code to use this circuit is shown below.

import RPi.GPIO as GPIO
import time

sensor = 21
led = 26
sleeptime = 0.1

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN)
GPIO.setup(led, GPIO.OUT)

while True:
    GPIO.output(led, not GPIO.input(sensor))
    time.sleep(sleeptime)

BBC micro:bit

To connect the line following sensor to a BBC micro:bit, the same circuit is used as for the Raspberry Pi.

microbit Line Follower

As with the Raspberry Pi, the infrared emitter is powered from the 3V supply, via a 220Ω resistor. The collector of the phototransistor is connected to the 3V supply via a 10k resistor, and to pin 1 of the micro:bit. An LED is connected to pin 0 of the micro:bit via a 330Ω resistor, as an indicator.

The PXT code for use with the micro:bit and line follower circuit is shown in the diagram below.

microbit Line Follower PXT

Using digital input on pin 1 does not give a clear enough transition between digital high and low with our circuit. However, this is not a problem, since the micro:bit supports analog input. The analog signal can have a range of between 0 and 1023, so comparing the input on pin 1 against a value of half this range is sufficient to indicate the state of the sensor. When no infrared is reflected onto the sensor, pin 1 will have an analog value greater than 512. An analog value of less than 512 indicates that infrared light is reflecting onto the phototransistor. Thus, the code shown will light the LEDs on the board and the LED on the breadboard when the phototransistor detects this reflected infrared light.

Arduino

Using the line follower sensor with an Arduino Uno requires the same circuit as with the Raspberry Pi and micro:bit. The only minor difference with the Arduino is that the supply voltage is now 5V. The diagram below shows the circuit for using the sensor with the Arduino Uno.

Arduino Line Follower

The collector of the line follower phototransistor is connected to pin 2 of the Arduino. The indicator LED is connected to pin 13. Thus, the code for our Arduino line follower is as shown below.

#define led 13
#define sensor 2

void setup() {
    pinMode(led, OUTPUT);
    pinMode(sensor, INPUT);
}

void loop() {
    int contact = digitalRead(sensor);
    if (contact == HIGH) {
        digitalWrite(led,LOW);
    } else {
        digitalWrite(led,HIGH);
    }
    delay(100);
}

Leave a comment