Using the GPIO pins to control output

Posted bySystem Administrator29/06/20160 Comment(s)

Using the GPIO pins to control outputs

So you’ve got your Raspberry Pi. You’ve gotten NOOBS setup on it, explored Raspbian a bit, and you’re feeling like a bit of a pro. Awesome! Now let’s get down to the nitty gritty side of things, what it’s all about, GPIO pins. Here is a simple tutorial to show you how to do this.

GPIO stands for General-Purpose-In/Out and is the gateway between your Raspberry Pi, and the world. Whilst plugging in a display, keyboard/mouse, and exploring around the software can be fun, the real excitement comes from connecting up sensors, buttons, motors, lights, anything you can think of can generally be connected up to the Raspberry Pi, one way or the other.

Today we’re going to embark on a multi-part tutorial which will get you using programming buttons, LEDs and sensors using Python (don’t worry, we’ll walk you through it step by step).

To write and execute code in Python, we need to use the Python text editor, and the Python shell. The text editor is where we write our code, and then we use the Python shell to run it and use console features like print readouts.

Let’s go ahead and open up a new project in Python by going to Menu – Programming – Python 3 (IDLE). The Python 3.x.x Shell should open, and then we go to File – New File. This will open up our Python text editor. Got there? Good job. Now before we can program anything we need two essential elements, an input, and an output.

To get our feet wet with GPIO and Python we’re going to make a simple circuit that we can build on later in the tutorial.

No prior knowledge of Python or programming is required to complete this project, however we recommend that you read our ‘Welcome to Analog Electronics’ tutorial first, as this tutorial won’t go into the circuitry in depth.

You can also use a breakout board like the Pi T Cobbler to connect to the GPIO pins to your breadboard, however you will need to cross-match the printed pin labels to the GPIO numbers.

However you connect your Raspberry Pi, make sure that your Raspberry Pi is disconnected form the power supply as the onboard Broadcom chip has limited protection circuitry and is quite sensitive to static and power supply shorts.

Make Sure You Have The Latest RPi.GPIO Module

In order for the code to perform as intended, you should have the latest version of the RPi.GPIO module. At the time of publication 0.6.2 is the latest version. To check what version you have, go to the Terminal and type:

sudo python
importRPi.GPIO as GPIO
GPIO.VERSION

If you don’t have the latest version don’t worry, it’s easy to update by going into your Terminal and typing:

sudo apt-get update

# Let things run their course here before typing the next command, it may take a while

sudo apt-get dist-upgrade

Let’s press on and build a simple pushbutton and LED circuit that we can use to program in Python.

The Goal

By the end of this article, we will have a circuit with a pushbutton that can control the colour of an RGB LED, and print the colour to the console.

But like any good design, we’ll build, and test it in smaller modules as we go.

The Circuit

So what’s going on in this circuit we’ve just built? We’re using a common anode RGB LED with 220 Ohm current limiting resistors to pins 2, 3, 4. Then we have a tactile switch that when pressed, will pull pin 6 low via a 10K current limiting resistor. The current limiting resistor here is super important, otherwise you will be shorting 3.3V straight to ground, which could fry your board. Finally we are connecting 3.3V and Ground from the GPIO power pins. Anyway, that’s the circuit, now let’s tell it do something!

The lines preceded by a ‘#’ symbol are comments, so go ahead and read through the code, and it should make a bit of sense, don’t worry if you don’t get it all, plenty of time for you to master the language of Python.

Once you’ve got a bit of a handle on what’s happening, I encourage you to type out the code yourself the code into your text editor rather than just copy it as it will help you familiarize yourself with the language.

Now, hit F5, it will prompt you to save your program, then it will run in the Python shell. If everything has gone according the plan, when you press the button, the LED should light up! Enjoy that deep seated feeling of contentment at running your first Python program!

At this point I can hear you saying ‘I don’t need a microcontroller just to do that, what’s the point?’ and you’re right, that isn’t an overly clever use of the Raspberry Pi, let’s make it do something more interesting!

RGB Toggle – Part 2

Wouldn’t it be great, if we could toggle the LED on/off with our button, rather than needing to hold it in? Well we can do just that, and while we’re at it, cycle through the different colours of our RGB LED.