Skip to content

Tutorial: How to use your Raspberry Pi like an Arduino

2012 June 15
by Tedb0t

Finally got to experiment with the Raspberry Pi’s GPIO (General Purpose Input/Output) pins.  I tried three methods: Python, Bash and C, and will describe each.  But first, here’s some setup information.

  • Connecting to the RPi’s GPIO header: I used an old floppy disk drive IDE ribbon cable.  It has 34 pins, but the RPi’s header is only 26 pins, so part of the connector is unused. I soldered some breadboard-friendly header pins onto the other end, which is working pretty well so far. Bear in mind that some IDE cables may be more complicated than they look. Before powering anything up, make sure you check that each conductor is indeed separate and you’re not accidentally grounding or shorting anything. You can always make your own by getting some 26-conductor IDC connectors and using a vice to crimp it onto a piece of ribbon cable.
  • Pin Connections: …are confusing! There’s a master table here, and a diagram here.  Looking at the diagram, my breakout header starts with 3.3V and moves left to right, top to bottom.  Here are the pin connections from my header as pictured:
    • Pin 2: 5V
    • Pin 3: “GPIO0”. Connected to one end of the switch (the other end is connected to ground, since this GPIO pin has an internal pull-up resistor.  This means that when the switch is not closed, the pin reads as “High,” and when closed, “Low.”
    • Pin 6: GND (note that it’s jumpered to my ground rail on my breakout board)
    • Pin 7: “GPIO4”. The anode of the LED through a 220-ohm resistor.
  • I’ll use this breakout board setup for all the following examples.  Different libraries/tools use different pin numbers, adding to the confusion, so I’ll try to clear it up.  Also note that I’m doing this through an SSH session, so there’s no monitor attached to the RPi. See my beginning instructions here if you don’t already have an SSH connection to your Pi.
  • Important Note: The RPi Wiki takes pains to remind you that these GPIO pins are unbuffered and unprotected, so if you short something out, you could fry your whole Pi, so be careful!  There are a number of other breakout boards being developed that should make this safer.
  • Important Note: Any program that accesses the GPIO pins must be run as root, so if you get an error, remember to “sudo” your command.
  • Python: One of my preferred languages, so I figured I’d try it out. First, install pip (Python package installer):
    • sudo curl | python
    • Next, install the RPi.GPIO Python module:
    • sudo pip install rpi.gpio
    • Now, you can enter the python interpreter (sudo python) and do stuff like:
    • import RPi.GPIO as GPIO
      GPIO.setup(7, GPIO.OUT)
      GPIO.output(7, True)
  • Bash: Something I’ve always loved about Unix is the idea that everything is a file.  On the Raspberry Pi, the GPIO pins are files too! With the pictured breadboard setup, we can do the following to turn the LED on and off (note that this uses the “BCM” pin numbering but the breadboard stays the same!):
    • (You can’t just put sudo before each command because the second command that the output is directed to would not be run as root. So, for simplicity, we drop into root first.)
      sudo su -
      echo "4" > /sys/class/gpio/export
      echo "4" > /sys/class/gpio/export
      echo "out" > /sys/class/gpio/gpio4/direction
      echo "1" > /sys/class/gpio/gpio4/value
      echo "0" > /sys/class/gpio/gpio4/value
    • To read from an input, ‘cat’ it like any other file!
      echo "0" > /sys/class/gpio/export
      echo "in" > /sys/class/gpio/gpio0/direction
      cat /sys/class/gpio/gpio0/value
  • C: Good old C.  The native example for C is really hairy, but luckily a plucky fellow named Gordon wrote an Arduino-like library just for the Raspberry Pi!  Here’s what you need to do:
    • Download and install the library:
      cd /tmp
      tar xfz wiringPi.tgz
      cd wiringPi/wiringPi
      sudo make install
      cd ../gpio
      sudo make install
    • Your Pi’s system now has the wiringPi library installed, so we can write a little program that uses it.
      cd ~
      nano blink.c
    • Type or paste in this program:
    • And compile it:
      cc -o blink blink.c -L/usr/local/lib -lwiringPi
    • And run it:
      sudo ./blink
    • You should have a happily blinking LED. Yay! Now for something more interesting:
  • Where’s my analogRead?  Sadly, unlike an Arduino, the RPi doesn’t have an onboard ADC (Analog to Digital Converter). However, some of the breakout boards being designed will include an external ADC, such as the Gertboard.  You can add your own external ADC, such as this one, by using the SPI bus on the RPi (the MOSI, MISO and SCK pins).
  • That’s all for now! Hope this helps, and as always, let me know if it works or you run into any problems.

Related Posts:

75 Responses
  1. May 30, 2015

    What’s up to all, it’s actually a good for me to pay a visit this website, it includes
    precious Information.

  2. June 20, 2015

    Hey, I think your website might be having browser compatibility issues.

    When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give you a quick heads up!
    Other then that, superb blog!

  3. August 22, 2016

    Have you ever considered creaqting an ebook
    or guest authoring on other sites? I have a blog basedd on the same information you doscuss annd would love to have you share
    sme stories/information. I know my viewers wkuld value your work.
    If you are even remotely interested, feel free to send
    me an email.

Comments are closed.