Idiot's Guide To Getting Started with Raspberry Pi's GPIO Pins

The idiot in question being... me!

It's been ages since I did any real electronics. Most of my work involves software and pre-assembled bits of kit. I thought that it was time I reacquainted myself with the joys of electricity 🙂

Because I'm fundamentally lazy, I purchased the all-in-one Raspberry Pi 2 kit from Vilros.

Raspberry Pi Kit

Lots of LEDs, some buttons, a nice case, all the cables, resistors, and all sorts of bits and bobs. Including a breadboard!

What's A Breadboard?

I remember - from school - that a breadboard is a.... thing... that lets you... electricity?

Honestly, it wasn't until I saw this diagram from Adafruit that it all finally clicked into place:

Photo by Collin Cunningham - Attribution-ShareAlike Creative Commons


The kit comes with a T-Shaped Cobbler which fits onto the breadboard and means you have a neatly labelled system with fewer wires running about the place.

I followed a sample tutorial on how to wire in an LED to be permanently on. Basically +3.3v → long leg of LED. Short leg of LED → 330Ω resistor → Ground.

Ok, that was easy. Now lets make it flash on command!

The wiring is fairly similar, but we use a controlable GPIO pin instead.

GPIO pin 21 → long leg of LED. Short leg of LED → 330Ω resistor → Ground.

Here's what it looks like:
GPIO LED Tutorial

And here's the Python code to switch it on, then off.

import RPi.GPIO as GPIO
led = 21
GPIO.setup(led, GPIO.OUT)
# Switch on
GPIO.output(led, 1)
# Switch off
GPIO.output(led, 0)


Ok, so, next thing - how to get information in to the Pi?

I was confused about exactly how the push switch worked - why were their four pins?

I read the O'Reilly Raspbery Pi Tutorial and became enlightened.
Push Switch

So, lets create a thingumy which lights up an LED when a button is pressed.

The LED will be the same as the above. The button will be wired as:
GPIO pin 19 → Button A. Button B → Ground.

Push Button Pi

And the code is:

import RPi.GPIO as GPIO
import time

# Pin 19 will sense for button pushing
button = 19
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# The LED
led = 21
GPIO.setup(led, GPIO.OUT)

while True:
    input_state = GPIO.input(button) # Sense the button
    if input_state == False:
        print('Button Pressed')
        # Switch on LED
        GPIO.output(led, 1)
    else :
        # Switch off LED
        GPIO.output(led, 0)

I'm not entirely sure why if input_state == False: is the logic for the button being pressed. Seems like it ought to be True, no? Anyway, there's a long discussion about all the ways sensors can be read on the raspberry-gpio-python pages.

Well, there you have it. An utterly simple guide to getting started. I just hope future-me finds this useful!

9 thoughts on “Idiot's Guide To Getting Started with Raspberry Pi's GPIO Pins

  1. Thanks for that Graham - I have a similar background to you, so just dabbling again in electronics for the first time since school days. I picked up the same kit for the same reason (great minds and all that), but was unsure which way round to connect the GPIO cable on the PI. A quick check with my old multimeter confirmed that it was the way one would expect (i.e. cable not running over the PI), and along with your breadboard pic above, there were no mishaps!
    Anyway, the experiments above worked well, and I followed the instructions in the PDF on the Vilros website (shown on this product's page) to set up a flashing LED, etc.
    I think the reason that pushing the switch sets a logical 'FALSE' is due to it being connected to ground ('0'), so connecting the GPIO circuit to ground is thus a FALSE input.

    Anyway, thanks again - it was reassuring to see your clear pics as I'm taking my first steps in this direction.

  2. Hey Terence. Thanks for the super easy tutorial! I got a similar kit for Christmas (though mine has the camera module) and have been pulling my hair out trying to get GPIO/led control to work. When I did not use the T Cobbler and used the wires to connect the breadboard to the ribbon cable (which is connected to the r-pi), the commands did not work. The led did go on when connected directly to the first pin (3.3v). I followed your instructions exactly and everything works! Guess I'll have to go back to see what I did wrong.... Anyway, thanks for the sanity check!

  3. YES! Just what I've been looking for. I opted to move the time sleep under the GPIO.output. Only noting this hear because I was thinking my light wasn't working. I was rapidly clicking... and then I noticed the sleep function... was sleeping, while I let go of the button and the state changed.

  4. I noticed that if you keep the button pressed down the message "Button Pressed" repeatedly comes up. Is there a way to make it only come up once?

  5. Thank you a lot! I got a Raspberry Pi for Christmas and didn't really know what to do with the breadboard, so your guide was exactly what I needed. The world needs more of these easy to understand tutorials.?

  6. I’d gotten as far as using the GPIO for output, but had no clue how to use the button. Thank you for a very clear tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *