Programming GPIO example





Now that you hopefully have the required supplies (Raspberry Pi, male-female jumper wires, bread-board, resistor and LED light), you're ready to tackle a basic example of using GPIO (General Purpose Input Output).

Because we're using multiple devices here, it may be a bit confusing with how we're communicating with the Raspberry Pi at times. There are many options here, though you will be required to eventually be comfortable with using or at the very least a . Click on either if you're confused.

XRDP is a remote-desktop application that you can use with your Raspberry Pi and the remote desktop functionality of operating systems like Windows.




SSH, or Secure Shell, is a method for connecting to a device's "terminal." SSH is a protocol, and the terminal is the command prompt equivalent of a linux machine. This is known as interacting with the machine "headless," meaning without a GUI. There are many basic commands to learn for this, but I will just put a few below:

 
		  # make a directory:
		  mkdir newdirname
		  
		  # change directory:
		  cd whatdir
		  
		  # list items in dir
		  ls
		  
		  #create or open a file:
		  nano filename
		  
		  # Root dir symbol, you can use this to cd to the root dir quickly with something like: cd ~
		  ~ 
		  

You can either interact with the terminal remotely with the Raspberry Pi using something like XRDP (remote desktop), which still gives you the GUI, or you can also use SSH. A popular program for SSH is called Putty. You use this to connect remotely to the Raspberry Pi. To do it, you will just need the IP address for your Raspberry Pi, usually this is just the local IP Address. Something like 192.168.X.X is what it will look like. You can find your Raspberry Pi's IP address by typing "ifconfig" in the terminal. You will be asked for a username and password to connect. Default username: pi, default password: raspberry.

This video assumes, initially, you are using XRDP or you are connected with a mouse, keyboard and monitor to the Raspberry Pi.

First, to use GPIO, you will need to make sure you have the packages necessary on your Raspberry Pi. Via the Pi terminal, type:

sudo apt-get install python-rpi.gpio

Once you have that, you're ready to code with GPIO.

Now, open up a Python script from the desktop.

Our first program is going to act like a door with a password. The idea is that, if the LED light is "on," then the door is locked. In order to get the light to turn off and the "lock" to unlock, we need to enter a correct password.

import RPi.GPIO as gpio
import time
		

First, we just import RPi.GPIO as gpio for some short-hand, then we import time so we can make the program sleep for a moment.

gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)
		

Here we are setting the mode to BCM, which is the naming convention for the GPIO pins. You can either address the pins by their actual physical pin number, or their "name" assigned to them. In order to be as careful as possible, it's best to explicitly check which you are doing.

while True:
    gpio.output(18, gpio.HIGH)
	passcode = raw_input("What is pi?: ")
		

while True is an infinite loop that will just always run, we are setting the output in to high, and then asking the user to input the password.

GPIO pins have "HIGH" or "LOW," which can be thought of as a 1 or a 0 value. On or off. They can also either be input or output. Input pins will "read" either a high or low value, and then output pins will actually push out a high or low signal.

	if passcode == "Awesome":
	    gpio.output(18, gpio.LOW)
		time.sleep(4)
		

We're assuming here the password is "Awesome." If that is what the user enters, then we set the output pin to low, which will turn off the LED light for 4 seconds.

	else:
	    gpio.output(18, gpio.HIGH)
		print("Wrong Password!")
		

If the password is not "Awesome," then the console will output that the password is wrong and continue the high signal.

The entire script is:

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)

while True:
    gpio.output(18, gpio.HIGH)
	passcode = raw_input("What is pi?: ")
	
	if passcode == "Awesome":
	    gpio.output(18, gpio.LOW)
		time.sleep(4)
	
	else:
	    gpio.output(18, gpio.HIGH)
		print("Wrong Password!")	

Now that we have the program, let's cover actually implementing it with the Raspberry Pi. Save the file to /home/pi/gpioexample.py

The next tutorial:






  • Robotics with the Raspberry Pi
  • Programming GPIO example
  • Running GPIO
  • Building Autonomous / RC car intro
  • Supplies needed
  • Motor Control
  • Connecting the four motors
  • Forward and Reverse
  • Turning
  • Pivoting
  • User Control
  • Remotely controlling the car
  • Adding a distance sensor (HC-SR04)
  • Programming with the distance sensor
  • Autopilot and/or auto-correct
  • Autonomous Beginnings
  • Testing Autonomous Code
  • Streaming video example one
  • Less latency streaming option