GPIO Examples

The following few pages will introduce you to programming the GPIO on the Raspberry Pi using command-line tools, shell and C programs. We will use LEDs for output and buttons for inputs.

First – the development platform. This is a Raspberry Pi inside an SKPang breadboard system and I’m using components from their Raspberry Pi Starter Kit. The top of the case has been removed in my photos to reduce reflections for the photos.

SKPang's Raspberry Pi Starter Kit

SKPang’s Raspberry Pi Starter Kit

Other starter kits are of-course available, (and you might not be in the UK!) but if you get something like this – a small breadboard, some female to male jumper leads, LEDs, resistors and buttons then you’ll be fine. (e.g. if you are in the US, then Adafruit has a range of some Pi accessories, but do shop around!)

Examples:


Comments

GPIO Examples — 8 Comments

  1. Dear Drogon:
    I have looked at some of your project examples and I belive you do great job publishing this examples and teaching how to work with Raspeberry Pi.

    I have looked into http://www.rasperrypi.org and elinux.org trying to find something similar as project VIC314 (retrotext.blogspot.co.uk/2012/06/its-complete-post-is-coming-soon.html). I’ll like to replace the Keyrah USB interface by using the GPIO of the RasPi. I found a good project that analyzes the C64 keyboard (www.waitingforfriday.com/index.php/C64_VICE_Front-End).

    Do you think it is possible to program the Raspeberry Pi to use a C64 matrix keyboard as a main keyboard and still use the VICE Commodore 64 emulator? Ofcourse I’ll still like to have the two DB9 joystick ports too.

    Do you think this can be done, what would you suggest me to read?

    My best regards

    • It looks like an 8×8 matrix, so you need 16 bits of GPIO.

      The Pi has 17 bits of GPIO, so this is do-able without any additional hardware. It looks like the joysticks might just be “keys” that are not on the actual keyboard, so that single 8×8 interface can read the 64keys on the keyboard, so I guess the joystic shadows some of those keys?

      So with some careful building it should be possible…. Not sure about the analog inputs though.

      If you need to use the Pi’s GPIO for other things as well, then you could use an I2C GPIO expander, or a single shift-register to represent

      -Gordon

  2. What is the fastest practical switching speed of the GPIO? Are you using system level API calls and so are they somehow scheduled in and out (no pun) with the OS or is there a low-level driver?

    • There is no low-level driver, you can poke the hardware directly and achieve something approximating 20,000,000 accesses/second. However you can not rely on that being reliable for many reasons.

      -Gordon

  3. Hi,
    Nice to know you from your blog. it’s really useful information about Raspi.
    I would like to try read PrinterPort / DB25 out from PC using GPIO on raspi.
    Here code that i use :

    import os
    import RPi.GPIO as GPIO
    import time
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(23, GPIO.IN)
    GPIO.setup(24, GPIO.IN)
    GPIO.setup(4, GPIO.IN)
    GPIO.setup(17, GPIO.IN)
    GPIO.setup(27, GPIO.IN)
    GPIO.setup(22, GPIO.IN)
    GPIO.setup(10, GPIO.IN)
    GPIO.setup(9, GPIO.IN)
    GPIO.setup(11, GPIO.IN)
    GPIO.setup(18, GPIO.IN)
    GPIO.setup(15, GPIO.IN)
    while True:
    if ( GPIO.input(23) == False ):
    print(‘Read data printer-out’)
    print(str(int(GPIO.input(24)))+’ ‘+str(int(GPIO.input(4)))+’ ‘+
    str(int(GPIO.input(17)))+’ ‘+str(int(GPIO.input(27)))+’ ‘+
    str(int(GPIO.input(22)))+’ ‘+str(int(GPIO.input(10)))+’ ‘+
    str(int(GPIO.input(9)))+’ ‘+str(int(GPIO.input(11))))

    Pin 23 connect to Pin 1 on DB25
    Pin 24 connect to Pin 2 on DB25 as BIT0
    Pin 4 connect to Pin 3 on DB25 as BIT1
    Pin 17 connect to Pin 4 on DB25 as BIT2
    Pin 27 connect to Pin 5 on DB25 as BIT3
    Pin 22 connect to Pin 6 on DB25 as BIT4
    Pin 10 connect to Pin 7 on DB25 as BIT5
    Pin 9 connect to Pin 8 on DB25 as BIT6
    Pin 11 connect to Pin 9 on DB25 as BIT7

    When i try to print a as char, the output is :
    pi@raspberrypi ~ $ sudo python /home/pi/hadi/readParalel.py
    Read data printer-out

    Read data printer-out
    (
    Read data printer-out
    P

    Do you have idea how to read PrinterPort out data using GPIO on Raspi..,

    Thank you..,
    Muhammad Abdul Hadi

    • Hi,

      I don’t maintain the Python wrappers for wiringPi – I just do the C side of it all. You might want to ask in some Python forums about your Python code.

      -Gordon

  4. Hi Gordon,

    Nice to read this web, but I’m so confused as I’m still new in this kind stuff, may I ask something,
    what I need is something like this, if the pin in is changed from 1 to 0 or from 0 to 1 (make a different condition), then it send data to MySQL database, everytime the pin in is changed, it automatically send data to MySQL, (of course inserts new row in the MySQL).
    If it can, would you please let me know what kind of script that I need to learn, and in what kind of language program that I need to use.
    Many thanks,

    Cheers,
    Bing.

    • You can do this in a bash script, C, C++, PHP, Python, Perl, etc. just about any language that will let you manipulate MySQL can be used to monitor the GPIO. Use whatever language you are most familiar with when dealing with MySQL – that will most likely be the harder part of the whole thing.

      -Gordon

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *