Raspberry Pi Robots with Python

By @makerhacks4/5/2018programming

https://i.imgur.com/2pntKBK.gif

One of the many benefits of single board computers such as the Raspberry Pi is having the GPIO pins (General Purpose IO) exposed and usable.

Back in the day, we could access serial and joystick ports for our 8-bit projects, and that sparked many homebrew electronics innovations.

Raspberry Pi GPIO

For now, the pins on the Raspberry Pi can be thought of as wires that can be told to be inputs, or outputs that can be on or off.

Plug in a sensor or an LED and you can read or power the device, depending on your project.

This can be a superpower, allowing you to sense and affect the world around you, provided you take some simple precautions to not fry your Pi and let the smoke out of it.

Of course, if you do, at least you are not out the cost of a whole desktop PC :)

Depending on which version of the Pi you have depends on how many pins you have access to. There are also pin/io expander modules that give your projects more capability, and things like ADC to allow you analog capabilities.

Current and Voltage

Unlike most Arduino boards, Pi pins are only 3v capable. For sending a signal that is fine, but a lot of things are NOT 3v so be careful.

The usual rule of thumb is to not exceed 3mA per GPIO pin, or 50mA total draw. This is only enough to drive a very small motor, or say an LED.

GPIO Numbers

https://www.raspberrypi.org/documentation/usage/gpio/images/a-and-b-physical-pin-numbers.png
* Source: Official documentation

You can number the pins based on the chip numbering or the way the board has them. Beginners find it easier to simply count pins but it is a good habit to start using the GPIO numbering from the get-go. You just need to refer to a diagram, and to be honest you ordinarily would even with physical numbering due to the special GND, 5v pins etc.

Python libraries

There are two main libraries used in Pi world for Python programmers.

  1. RPi.GPIO
  2. GPIO Zero

The first is the basic library, the second is a friendlier wrapper.

As well as these two, there is the port of Wiring, and PigPio. These are both C libraries with more advanced low-level capabilities.

Most people stick with the defaults above unless they need very in-depth or accurate control.

Driving Motors + Simple GPIO example

Rather than blink an LED, let's start where we want to be by driving motors.

My favourite way to drive a motor is with the L293D chip, this isolates your Pi (or Arduino) from the motor power draw, allowing you to power motors that the board alone could not.

https://makerhacks.com/wp-content/uploads/2015/07/l293d-300x241.png

or L293 based module:

https://makerhacks.com/wp-content/uploads/2015/07/l298n-300x246.png

For Pi specifically, CamJam in the UK have developed a Pi module for their robot competitions that works for both full-sized and zero boards that sits over the pins.

Regardless, don't worry about wiring too much at this stage, just familiarize yourself with the process.

Full code Gist here

import RPi.GPIO as GPIO
import time


# Pi has (confusingly) two numbering schemes for pins
# usually you would choose based on what is printed on
# your dongles/pinout prints
GPIO.setmode(GPIO.BCM)

# H-Bridge pins
motor_pin_A1 = 7
motor_pin_A2 = 8
motor_pin_B1 = 9
motor_pin_B2 = 10

# Set all the pins as output
GPIO.setup(motor_pin_A1, GPIO.OUT)
GPIO.setup(motor_pin_A2, GPIO.OUT)
GPIO.setup(motor_pin_B1, GPIO.OUT)
GPIO.setup(motor_pin_B2, GPIO.OUT)

# Fire up the pins!
GPIO.output(motor_pin_A1, True)
GPIO.output(motor_pin_A2, False)
GPIO.output(motor_pin_B1, True)
GPIO.output(motor_pin_B2, False)

# Wait a second
time.sleep(1)

# Stop all the pins
GPIO.output(motor_pin_A1, False)
GPIO.output(motor_pin_A2, False)
GPIO.output(motor_pin_B1, False)
GPIO.output(motor_pin_B2, False)

# Clean up
GPIO.cleanup()
66

comments