Book Image

Raspberry Pi Embedded Projects Hotshot

Book Image

Raspberry Pi Embedded Projects Hotshot

Overview of this book

Table of Contents (20 chapters)
Raspberry Pi Mechatronics Projects HOTSHOT
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Implementation of line following logic based on sensor data


In this task, we will implement a simple line following technique using the infrared sensor. We will make use of a pair of infrared sensors to track a black line on a white surface. The robot will move forward if both the sensors are on a white surface. The robot turns left if the left sensor is on the black line and vice versa.

Prepare for lift off

The sensor needs to be soldered and connected to the Raspberry Pi (something like the one shown in the preceding schematic). Alternatively, you may use a sensor of your choice.

Engage thrusters

  1. As always, we will get started by importing the required modules, especially Rpi.GPIO:

    import RPi.GPIO as GPIO
    from time import sleep
  2. We will set the pin configuration that we will use in this program:

    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18,GPIO.IN)
    GPIO.setup(25,GPIO.IN)
  3. The control logic explained earlier is implemented as follows:

    state = 1
    prev_state = 0
    while True:
      #both sensors...