Latest at Gadgetoid: Propeller ASC+, the Arduino-compatible multi-core micro: read my first impressions

Pinout

3v3 Power

The 3v3, 3.3 volt, supply pin on the Pi has a max current draw of 50 mA, enough to power a couple of LEDs but not much more.

You should use the 5v0 supply instead, and use a regulator if you need 3v3.

GPIO 2 (SDA)

BCM pin 0 on Rev 1 board and pin 2 on Rev 2 board. WiringPi pin 8. Ladder Board Small Green LED.

It's easy to get started writing a digital HIGH or LOW to a GPIO pin, but you've got to remember a few things:

  • Run your script as root
  • Set your pin's mode to OUTPUT (1)

Assuming you've installed WiringPi2-Python ( pip install wiringpi2 ) then try pasting the following into a .py file:

import wiringpi2 as wiringpi
HIGH = 1
LOW = 0
OUTPUT = 1
INPUT = 0
wiringpi.wiringPiSetup()
wiringpi.pinMode(8,OUTPUT)
wiringpi.digitalWrite(8,HIGH)

Then run it with:

sudo python myscript.py

GPIO 3 (SCL)

BCM pin 1 on Rev 1 board and pin 3 on Rev 2 board. WiringPi pin 9. Ladder Board Small Red LED.

require 'wiringpi2'
HIGH = 1
LOW = 0
OUTPUT = 1
INPUT = 0
io = WiringPi::GPIO.new
io.pin_mode(9,OUTPUT)
io.digital_write(9,HIGH)

GPIO 4 (GPCLK0)

BCM pin 4. WiringPi pin 7. Quick2Wire I2C Interrupt. Ladder Board LED Blue 2.

Ground

GPIO 17

BCM pin 17. WiringPi pin 0. Quick2Wire Push Button. Ladder Board LED Red 1.

GPIO 21 (PCM_D)

BCM pin 21 on Rev 1 board and pin 27 on Rev 2 board. WiringPi pin 2. Ladder Board LED Yellow 1.

GPIO 22

BCM pin 22. WiringPi pin 3. Ladder Board LED Yellow 2.

3v3 Power

GPIO 10 (MOSI)

BCM pin 10. WiringPi pin 12. Ladder Board Button 3.

Connect to the MOSI pin of an ATmega328, Arduino Pin 11, for programming with AVRDude. Make sure your ATmega328 is powered by a 3.3v supply. If you're using 5v, make sure to use a logic-level converter.

GPIO 9 (MISO)

BCM pin 9. WiringPi pin 13. Ladder Board Button 4.

Connect to the MISO pin of an ATmega328, Arduino Pin 12, for programming with Gordon's modified AVRDude. Make sure your ATmega328 is powered by a 3.3v supply. If you're using 5v, make sure to use a logic-level converter.

GPIO 11 (SCLK)

BCM pin 11. WiringPi pin 14.

Connect to the SCK pin of an ATmega328, Arduino Pin 13, for programming with Gordon's modified AVRDude. Make sure your ATmega328 is powered by a 3.3v supply. If you're using 5v, make sure to use a logic-level converter.

Ground

EEPROM ID DATA

Model B+ only!

The EEPROM ID pins are an i2c bus reserved for new add-on boards, which can incorporate an EEPROM with an ID and pin state configuration. They have no other purpose, and are cleverly placed so that they are hidden under protruding 26pin add-on boards rather than any other useful IO.

GPIO 5

Model B+ only!

GPIO 6

Model B+ only!

GPIO 13

Model B+ only!

ALT0 / PWM
PWM 1

GPIO 19

Model B+ only!

ALT0 / PCM ALT4 / SPI1 ALT5 / PWM
PCM Frame Sync MISO PWM1

GPIO 26

Model B+ only!

Ground

Model B+ only!

5v Power

The 5v0 power pins are connected directly to the Pi's power input and will capably provide the full current of your mains adaptor, less that used by the Pi itself.

Don't be disuaded by what sounds like a measly low voltage. You can do a lot with this! I regularly use mine to power Arduinos and have even run a small Electroluminescent wire inverter right off the 5v pin!

5v Power

This pin is exactly the same as its 5v companion. Two is better than one, right? There's nothing much more to say about the 5v pin.

Ground

The Revision 2.0 Raspberry Pi turns all the original "do-not-connect" pins into additional ground. Pay careful attention to where these are, because connecting any outputs or voltage supplies directly to ground will not to your Pi any favours!

GPIO 14 (TXD)

BCM pin 14. WiringPi pin 15.

This pin doubles up as the UART transmit pin, TXD.

UART is extremely useful if you want to get into Piduino ( a delightful blend of Arduino and Raspberry Pi ) territory. Hook the RXD up to the Arduino's TXD via a 3.3v <-> 5v logic level converter, and the TXD up to the Arduino's RXD. Personally I avoid using a logic level converter and run my breadboard Arduino on 3.3v.

GPIO 15 (RXD)

BCM pin 15. WiringPi pin 16.

This pin doubles up as the UART recieve pin, RXD.

GPIO 18 (PCM_CLK)

BCM pin 18. WiringPi pin 1. Quick2Wire LED. Ladder Board LED Red 2.

ALT5 / PWM
PWM 0

Ground

GPIO 23

BCM pin 23. WiringPi pin 4. Ladder Board LED Green 1.

GPIO 24

BCM pin 24. WiringPi pin 5. Ladder Board LED Green 2.

Ground

GPIO 25

BCM pin 25. WiringPi pin 6. Quick2Wire SPI Interrupt. Ladder Board LED Blue 1.

GPIO 8 (CE0)

BCM pin 8. WiringPi pin 10. Ladder Board Button 2.

Connect directly to the Reset (RST) pin of an ATmega 328 for programming with Gordon's modified AVRDude. Make sure your ATmega328 is powered by a 3.3v supply. If you're using 5v, make sure to use a logic-level converter.

GPIO 7 (CE1)

BCM pin 7. WiringPi pin 11. Ladder Board Button 1.

EEPROM ID CLOCK

Model B+ only!

The EEPROM ID pins are an i2c bus reserved for new add-on boards, which can incorporate an EEPROM with an ID and pin state configuration. They have no other purpose, and are cleverly placed so that they are hidden under protruding 26pin add-on boards rather than any other useful IO.

Ground

Model B+ only!

GPIO 12

Model B+ only!

ALT0 / PWM
PWM 0

Ground

Model B+ only!

GPIO 16

Model B+ only!

GPIO 20

Model B+ only!

ALT0 / PCM ALT4 / SPI1
PCM Data In MOSI

GPIO 21

Model B+ only!

ALT0 / PCM ALT4 / SPI1
PCM Data Out SCLK

Pinout!

The comprehensive Raspberry Pi Pinout guide for the Revision 2.0 Raspberry Pi, now with Model B+

Pinout isn't meant to be printable, but it's a great quick-reference and a comprehensive starter guide to learning about your Raspberry Pi's GPIO pins.

Model B+

Now that the Raspberry Pi Model B Plus is here, I've updated Pinout with the 14 extra pins you'll find on your shiny new board.

Note: While I've placed a gap to visually separate the additional 14 pins on the B+, you wont find this gap on the actual board!

5v Power

The 5v power pins, two of them, provide your project with any current your power adaptor can muster minus that which is used by the Pi itself.

5v power is great for regulating down to 3.3v, which you can then use to power an ATmega that's logic-level compatible with your Pi.

If you need more than 5v, driving motors for example, you'll have to find your power elsewhere!

3.3v Power

At a measly 50mA max current, the 3.3v pin on the Raspberry Pi isn't terribly useful, but it's enough to light an LED or two.

0v Ground

When powering a project from the Pi, the Ground pin completes your circuit.

If you want to memorise this pinout, you should forget the ground pins spaced out around the GPIO header and focus just on the one below the 5v pins and pin 25.

UART - Universal Asynchronous Receiver/Transmitter

The 2 UART pins in WiringPi are: 15, 16

UART is a handy, straight forward way to interface an Arduino ( or bootloaded ATmega ) with your Pi. You must, however, be careful with logic-levels between the two devices: the Pi is 3.3v and the Arduino is 5v. Connect the two and you might conjure up some magic blue smoke.

Personally I'm a fan of building out a Arduino Bootloaded ATmega 328 circuit on a breadboard with a voltage regulator to take the Pi's 5v line and convert it to 3.3v. The ATmega 328 seems to run quite happily at 3.3v using a 16Mhz crystal and you'll then have an Arduino clone with 3.3v logic.

The following python example opens the Pi's UART at 9600baud and puts 'hello world'

import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()
serial = wiringpi.serialOpen('/dev/ttyAMA0',9600)
wiringpi.serialPuts(serial,'hello world!')

GPIO - General Purpose Input/Output

The 8 GPIO pins in WiringPi are: 0, 1, 2, 3, 4, 5, 6, 7

SPI - Serial Peripheral Interface

Known as the four-wire serial bus, SPI lets you daisy-chain multiple compatible devices off a single set of pins by assigning them different addresses.

A simple example of an SPI peripheral is the MCP23S17 digital IO expander chip Note the S in place of the 0 found on the I2C version. Using it in WiringPi2 is a doddle:

import wiringpi2 as wiringpi
HIGH = 1
OUTPUT = 1
PIN_BASE = 65
SPI_ADDR = 0x20
wiringpi.wiringPiSetup()
wiringpi.mcp23S17Setup(PIN_BASE,SPI_ADDR)
# 16 pins including the starting pin
mcp23S17pins = range(PIN_BASE,PIN_BASE+15)
for pin in mcp23S17pins:
	wiringpi.pinMode(pin,OUTPUT)
	wiringpi.digitalWrite(pin,HIGH)

You can also use the SPI port to "Bit-Bang" an ATmega 328, loading Arduino sketches onto it with Gordon's modified version of AVRDude.

Hook up you Pi's SPI port to that of your ATmega, and power the ATmega from the 3.3v pin on the Pi. Make sure you're not running any SPI device drivers, and run "avrdude -p m328p -c gpio" to verify the connection.

See the individual pins to learn how to connect up your ATmega.

I2C - Inter Integrated Circuit

The Raspberry Pi's I2C pins are an extremely useful way to talk to many different types of external peripheral; from the MCP23017 digital IO expander, to a connected ATmega.

You can verify the address of connected I2C peripherals with a simple one-liner:

sudo apt-get install i2c-tools
sudo i2cdetect -y 1

You can access i2c from Python using the smbus library:

import smbus
DEVICE_BUS = 1
DEVICE_ADDR = 0x15
bus = smbus.SMBus(DEVICE_BUS)
bus.write_byte_data(DEVICE_ADDR, 0x00, 0x01)

Raspberry Pi Ladder Board

The inexpensive Raspberry Pi Ladder Kit is a great way to get started with GPIO tinkering.

It connects 12 LEDs and 4 buttons directly to the GPIO pins, making it extremely easy to start programming. Using the pins on the left, you simply set 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9, the LEDs, to OUTPUT (1) and 10,11,12,14, the buttons, to INPUT (0) and start reading/writing.

In practice, it's actually a little more complicated than this. The buttons all need their pull up/down switched to PUD_UP/UP (2), because pressing a button on the ladder board pulls that pin down to ground.

The code below should get you started:

import wiringpi2 as wiringpi
INPUT = 0
OUTPUT = 1
LOW = 0
HIGH = 1
BUTTONS = [13,12,10,11]
LEDS = [0,1,2,3,4,5,6,7,8,9]
PUD_UP = 2

wiringpi.wiringPiSetup()

for button in BUTTONS:
    wiringpi.pinMode(button,INPUT)
    wiringpi.pullUpDnControl(button,PUD_UP)

for led in LEDS:
    wiringpi.pinMode(led,OUTPUT)

while 1:
    for index,button in enumerate(BUTTONS):
        button_state = wiringpi.digitalRead(button)
        first_led = LEDS[index*2]
        second_led = LEDS[(index*2)+1]
        #print str(button) + ' ' + str(button_state)
        wiringpi.digitalWrite(first_led,1-button_state)
        wiringpi.digitalWrite(second_led,1-button_state)
    wiringpi.delay(20)

The button locations and their corresponding pin numbers also don't correspond with those of the LEDs so we have to pair up the buttons accordingly for a basic example. Finally, never forget to call wiringpi.wiringPiSetup() or nothing much will happen!

Raspberry Pi WiringPi

WiringPi is an attempt to bring Arduino-wiring-like simplicity to the Raspberry Pi. The goal is to have a single common platform and set of functions for accessing the Raspberry Pi GPIO across muliple languages. WiringPi is a C library at heart, but it's available to both Ruby and Python users who can "gem install wiringpi" or "pip install wiringpi2" respectively.

Python users note the 2 on the end, the WiringPi2-Python library finally brings a whole host of existing WiringPi functionality to Python including brand new features from WiringPi 2.

For more information about WiringPi you should visit the official WiringPi website.

Getting started with WiringPi

WiringPi uses its own pin numbering scheme, here you'll learn how WiringPi numbers your GPIO pins, what those pins do and how to do shiny things with them from within Python or Ruby.

WiringPi, the Arduino-like GPIO library for the Pi, is available in C right from Gordon's git repository, Python, Ruby and even Perl and PHP to a lesser extent.

Installing to Python couldn't be easier, just:

sudo pip install wiringpi2

Note the 2 on the end? That's the all new, shinier WiringPi!

PiBorg LedBorg RGB LED Board

The PiBorg LedBorg is an ultra-bright RGB LED board for the Raspberry Pi. It has its own driver, so you don't need to drive it manually.

If you want a much, much wider range of colours, though, you can drive it manually using softPwm in WiringPi. The pin assignments for this are as follows:

WiringPi pin 0: Red LED
WiringPi pin 2: Green LED
WiringPi pin 3: Blue LED

This is easy using WiringPi in Python:

import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()
wiringpi.softPwmCreate(0,0,100)
wiringpi.softPwmCreate(2,0,100)
wiringpi.softPwmCreate(3,0,100)
wiringpi.softPwmWrite(3,100)
wiringpi.softPwmWrite(0,100)
					

Geekroo Clockatoo Clock Board With buzzer

The Clockatoo buzzer is available on WiringPi pin 7, GPIO pin 4.

The GeekRoo Clockatoo is a great little board combining a battery-backed up real-time clock, a 4 digit, 7 segment display and a buzzer into one package.

GeekRoo's own instruction manual gets you started with the clock itself, but doesn't tell you how to use the buzzer.

WiringPi's softTone does a great job of pulsing the buzzer at a variety of annoying ( or alerting ) speeds, try this for a simple alert tone:

import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()
wiringpi.softToneCreate(7)
wiringpi.softToneWrite(7,1)
					

The second parameter for softToneWrite is the frequency, drive it higher and the alarm sound will become steadily more irritating until you're sure to wake up!

ATmega 328p / Arduino over SPI

Did you know that your Pi could power and program an ATmega 328p/Arduino directly, with nothing but a few wires, a breadboard, a 16Mhz crystal oscillator and some 22pF capacitors?

Read my complete Pico PiDuino tutorial to get started for just over £5

You'll need to install Gordon's modified AVRDude.

Connect 8/CEO to your ATmega's Reset/RST pin, 9/MISO to its MISO pin (D12), 10 to its MOSI pin (D11) and 11/SCLK to its SCLK pin (D13).

Power your ATmega with the 3.3v and GND pins from your Pi, and you're good to go.

Make sure you have no rogue SPI device drivers running and check it's connected correctly using:

avrdude -p m328p -c gpio

To get started compiling Arduino sketches from the command line:

sudo apt-get install arduino arduino-mk

This basic Makefile should get you started. Create a basic sketch, name it mysketch.ino and run:

export BOARD=atmega328
make
avrdude -p m328p -c gpio -e -U flash:w:build-cli/Arduino.hex

PWM - Pulse Width Modulation

Pibrella

The all-in-one light, sound, input and output add-on board from Pimoroni vs Cyntech uses lots of IO on the Pi but leaves both Serial and I2C free leaving plenty of room for expansion if you get creative.

Pibrella is easy to use, first you should install the module using LXTerminal/Command Line:

sudo apt-get install python-pip
sudo pip install pibrella

Then import it into your Python script and start tinkering:

import pibrella
pibrella.light.red.on()

Display-o-Tron 3000

Display-o-Tron also uses both i2c and SPI, but you can connect multiple devices to these busses so I've not highlighted them.

Dot3k uses the same chip as the PiGlow ( SN3218 ), which has a fixed address so you can't use a PiGlow alongside a Dot3k