Lesson 23 Tracking Sensor

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

A tracking sensor (as shown below) has the same principle with an obstacle avoidance sensor except its smaller transmitting power.

Components

– 1 * Raspberry Pi

– 1 * Network cable (or USB wireless network adapter)

– 1 * Tracking sensor module

– Several jumper wires

Experimental Principle

When the infrared transmitter on the sensor emits rays to a piece of paper, if the rays shine on a white surface, they will be reflected and received by the receiver, and pin S will output low level; If the rays encounter black lines, they will be absorbed, thus the receiver gets nothing, and pin S will output high level.

Experimental Procedures

Step 1: Build the circuit

                          Raspberry Pi                           Tracking Sensor

                                 GPIO0 ——————————   S

                                  3.3V ——————————–   V+

                                 GND ———————————  G

Step 2: Edit and save the code (see path/Rpi_SensorKit_code/22_trackSensor/ trackSensor.c)

Step 3: Compile

              gcc  trackSensor.c  -lwiringPi

Step 4: Run

              ./a.out

Draw a black line on a white or light-color surface. When the tracking sensor encounters the black line, a string “Black Line is detected” will be printed on the screen.

 trackSensor.c

#include <wiringPi.h>
#include <stdio.h>

#define TrackSensorPin    0
#define LedPin            1

int main(void)
{
	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
	
	pinMode(TrackSensorPin, INPUT);
	pinMode(LedPin,  OUTPUT);

	while(1){
		if(digitalRead(TrackSensorPin) == LOW){
			printf("White line is detected\n");
			digitalWrite(LedPin, LOW);     //led on
			delay(100);
			digitalWrite(LedPin, HIGH);    //led off
		}	
		else{
			printf("...Black line is detected\n");
			delay(100);
		}
	}

	return 0;
}

Python Code

#!/usr/bin/env python
import RPi.GPIO as GPIO

TrackPin = 11
LedPin   = 12

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(LedPin, GPIO.OUT)   # Set LedPin's mode is output
	GPIO.setup(TrackPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led

def loop():
	while True:
		if GPIO.input(TrackPin) == GPIO.LOW:
			print '...led on'
			GPIO.output(LedPin, GPIO.LOW)  # led on
		else:
			print 'led off...'
			GPIO.output(LedPin, GPIO.HIGH) # led off

def destroy():
	GPIO.output(LedPin, GPIO.HIGH)     # led off
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()