If you're like me when I first started working with things like Arduino, Raspberry Pi, and other electronic development platforms, you've probably seen a bunch of projects online. In quite a few of these projects, you may have seen a Raspberry Pi being used as the brains of the project. So, you may have purchased a Raspberry Pi to try and use it as the brains for your project. However, you may have realized, after using it for a while, that controlling the Pi's GPIO pins or (General Purpose Input Output pins) is not as easy as everyone makes it out to be. What I'd like to do in this Instructable, is to kinda simplify the coding side of things a little bit by giving you an example that you can modify, add to, or take away from to make coding your project a little bit easier.
Before we can get into coding the GPIO pins, you need to open Python as a root user, or an admin.
First of all, start up your Pi and navigate to Terminal. There are several different ways you can do this. The two most common ways are: 1) Find it on the Raspberry Pi's start menu (I don't know what it's actually called, but if you've ever used Windows you'll know what I'm talking about). 2) You can press CTRL + ALT + t.
sudo idle3 &
That will open the Python IDE as a root user, allowing you to have complete sudo control over the Raspberry Pi's GPIO pins.
Step 1: Python Code
Alright, now that you're inside of the Python IDE and you've given Python complete control over your GPIO header pins, we can get into the code. Below, you'll find the example I was talking about. It's not very complicated unless you're just beginning to delve into the vast world of coding. If you've done some coding before, you should have no issues modifying the code below to work with your project. However, if you are new, I'd be more than willing to answer any questions you may have. Let me just break it down a little bit. Please notice the numbers I put next to each line below.
- Lines 1 and 2 are pretty simple. They are just importing two different libraries that we'll need to make out code work with the Pi. The first line is importing the library called RPi.GPIO and it's renaming it GPIO so that it's easier for us to reference and use in our code.
- Line 3 is doing something that we're not going to get into in this Instructable. Just know that it's required for us to use if we want to control the GPIO pins.
- Lines 4 and 5 are simply creating two variables (led and button) and assigning them to GPIO pins 4 and 14.
- Lines 6 and 7 are setting up our pins as either an input (button) or an output (led).
- Line 8 is beginning a while loop.
- Line 9 is creating an if statement inside the while loop that is checking to see if the button has been pressed or not. This statement is grabbing the input value of button (either True or False) and checking to make sure that it's True before allowing the program to move on.
- Line 10 sets the output value of led to 1 (meaning it sends power to pin 4).
- Line 11 is pausing the program for 1 second.
- Line 12 sets the output value of led to 0 (meaning it will no longer send power to pin 4).
- Line 13 is pausing the program for 1 second.
- And lastly, Line 14 is cleaning up the GPIO pins on the Pi so that they can be used again.
1.) import RPi.GPIO as GPIO
2.) import time
3.) GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False)
4.) led = 4
5.) button = 14
6.) GPIO.setup(led, GPIO.OUT) //Setting pin 4 as output
7.) GPIO.setup(button, GPIO.IN, GPIO.PUD_UP) //Setting pin 14 as input
8.) while True:
9.) if GPIO.input(button) == True:
10.) GPIO.output(led, 1) //Turns led on (1 = on, 0 = off)
11.) time.sleep(1) //Delay
12.) GPIO.output(led, 0)
14.) GPIO.cleanup() //Cleans up used ports and makes them ready to be used again.
Step 2: GPIO Reference
The picture above lists all the GPIO pins on the Raspberry Pi. Make sure that you look at the different coloured boxes next to each pin and not the boring looking smaller boxes that only increase numerically. When looking for a GPIO make sure you find the pin that says GPIO(pin number) such as GPIO4 which you'll find is numerical pin number 7. Hopefully, I cleared up some confusion instead of compounding it...