How to get your Pi to go

Line-Following Robot Script

As you can see in Listing 1, I've put some of what I learned about the Pi2Go-Lite Python library into action to make the Pi2Go-Lite follow a circuit with a simple Python script. This line-following robot circuit is easily created by sticking duct tape down on a very light floor surface (or large sheets of white paper) in pretty much any shape or formation you like. You would likely have to alter the script if the lines were to crossover in a figure eight or a similar pattern; otherwise, this code will work straight out of the box. You can find this script and other Pi2Go-Lite examples on Zachary Igielman's GitHub page [5].

The script initializes the Pi2Go library, and sets some variables that control the speed of the motors. Before entering into the main loop of the script that's repeated infinitely until you elect to cancel it with a keyboard interrupt, the Pi2Go-Lite is set to drive forward at full speed. After assigning the methods for checking both line-following IR sensors to variables, a number of if statements check to see where the robot is on the line. If the IR sensors detect no line at all, the robot is set to stop, so it can be rescued. If, however, one IR sensor detects a line and other doesn't the script tells the robot to turn in order to seek out the line in the opposite direction.

Of course, like most things that utilize the Raspberry Pi, the sky really is the limit with Pi2Go-Lite. It's even possible to add ever more amazing stuff to your robot project, like the brilliant MeArm acrylic robot arm (Figure 3)!

Figure 3: Pi2Go with a MeArm attached. You can even go crazy and attach a robot arm like the MeArm, to the front of the Pi2Go.


  1. 4tronix storefront:
  2. Pi2Go homepage:
  3. Pi2Go-Lite build instructions:
  4. Pi2Go Python library and instructions:
  5. Example Pi2Go scripts by Zachary Igielman:

The Author

Russell Barnes is the editor of and has been a computer and technology writer for more than 15 years. He lives in the UK with four Raspberry Pis and three Arduinos, much to the chagrin of his wife, Rebecca.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content