Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi Embedded Projects Hotshot

You're reading from   Raspberry Pi Embedded Projects Hotshot Enter the world of mechatronic systems with the Raspberry Pi to design and build 12 amazing projects

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher
ISBN-13 9781849696227
Length 250 pages
Edition 1st Edition
Concepts
Arrow right icon
Toc

Table of Contents (15) Chapters Close

Preface 1. Hello World FREE CHAPTER 2. A Raspberry WebIDE Example 3. The Arduino Raspberry Pi Interface 4. Christmas Light Sequencer 5. Internet of Things Example – An E-mail Alert Water Fountain 6. Raspberry Pi as a Personal Assistant 7. Raspberry Pi-based Line Following Robot 8. Connect Four Desktop Game using Raspberry Pi 9. The Raspberry Pi-enabled Pet/Wildlife Monitor 10. Raspberry Pi Personal Health Monitor 11. Home Automation using Raspberry Pi 12. Using a Raspberry Pi for Science and Education 13. Tips and Tricks 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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime