Cheat Sheet

Raspberry Pi Projects For Dummies Cheat Sheet

From Raspberry Pi Projects For Dummies

By Mike Cook, Jonathan Evans, Brock Craft

You can connect your Raspberry Pi to the Arduino and get even more out of both boards, and there are several methods for making this connection. The Raspberry Pi has a variety of GPIO pins that you can use in your projects, but it helps if you know the functions of the various pins. Finally, the Raspberry Pi can power devices in several ways — which can come in handy as you build projects.

Connecting the Raspberry Pi and the Arduino

Some people see the Arduino and Raspberry Pi as rival boards, but this isn’t the case at all. If anything, they’re complementary — the weakness of one is the strength of the other. Here are three ways of connecting an Arduino and a Raspberry Pi.

USB

Simply connect the USB connector on the Raspberry Pi to the USB connector on the Arduino. That’s all you need to do. There is a slight curveball in that the Pi can potentially assign the Arduino one of two ports, so when opening up the serial port to the Arduino, use the following code snippet:

import serial
try:
  ser = serial.Serial('/dev/ttyACM0',115200, timeout=2)
except :
  ser = serial.Serial('/dev/ttyACM1',115200, timeout=2)

This assumes that you’ve set up the code in the Arduino to use the serial port at the same 115200 baud rate with a begin command:

Serial.begin(115200)

You can use any baud rate that the two systems can use, but they have to match. On the Pi side, use ser.write() to send whatever is in the brackets to the Arduino and back = ser.read(1) to get one byte back. If there has been nothing received after the timeout period you set when opening the port, the call returns. You can get as many bytes back from this call as you put in the brackets.

Serial

This works in software much like USB, but here you connect the GPIO pins on the Raspberry Pi to the TX and RX pins on the Arduino. This method can be handy if you have an Arduino with more than one serial port, like the Arduino Mega. The only snag is that if you’re using a 5V Arduino, you need some level shifting circuits. For receiving a 5V signal into the Pi, a simple resistive divider will suffice, but in going from 3V3 up to 5V you need a transistor.

image0.jpg

Any general-purpose NPN transistor can be used here. You communicate in the same way as the USB serial, except you always get the ttyACM0 port.

For both serial methods, you’re transferring bytes not numbers. If you have trouble with this, maybe some other software install has changed the default workings of your serial port on the Pi side. If so, search online for the symptoms of your specific problem.

I2C

The I2C (pronounced “I squared C” but often written I2C) can be used to connect the two together. The I2C system is a master/slave arrangement — only the master sends or requests data. The Raspberry Pi is not very well suited for being an I2C slave, so you have to make it the master. The bus requires pull-up resistors, which are already on GPIO pins 2 and 3 on the Raspberry Pi. Unfortunately, the 5V Arduino has its internal pull-up resistor enabled if you’re using the standard I2C library called “Wire”; because this is pulling the lines up to 5V, it could damage your Pi. So you’ll have to hack the wire library (the one used for I2C) or, even better, use a library that allows control of the internal pull-up resistors like the one found at Github.com. Connection is then simple. Note that there is an example of using the Arduino as an I2C slave in the examples section of the Arduino IDE.

image1.jpg

If you don’t want to change the software, you’ll have to use an I2C level shifting circuit.

Raspberry Pi GPIO Pin Alternate Functions

The general-purpose input/output (GPIO) pins can be switched between input or output and have a pull-up or pull-down resistor enabled, but there are a host of other peripherals in the Raspberry Pi chip that can be switched to these pins. You can see the basic arrangement for one pin, GPIO 18.

image0.jpg

All the other pins have a similar arrangement but with different blocks to select from. The numbers on the switch are the three-bit register value that have to be set in the collection of alternate function select registers. Note that the Alt numbers have little relationship to the bit patterns you actually set.

Although there are 54 GPIO lines in the Raspberry Pi’s processor, only 28 are brought out to the P1 connector on the board (less on the non-plus models); the rest are used for actually making the processor act like a computer (things like the SD card, USB connector, and LEDs). On the B+ and A+ models, you have the first 28 GPIO pins; on earlier models, you have a subset of these. Most diagrams you find give you only a selection of these alternate functions. Here, you can see all the alternate functions and where they appear on the GPIO connector.

image1.jpg

The first thing to spot is that there are two types of “nothing here”: One is blank and the other is labeled as <reserved>. It’s likely that these reserved functions are used for factory testing of the chip or for functions not disclosed in the data sheet. The blank ones are simply not implemented.

For the full gory tale, the BCM2835 ARM Peripherals document is where you want to look, but here’s a quick look at some of the functions:

  • ALT 0: Where most of the interesting and useful alternate functions are as far as the Raspberry Pi is concerned. The SDA and SCL 0 and 1 are the two I2C buses, and the TXD0 and RXD0 are the serial connections. The GPCLK lines are a general-purpose clock output that can be set to run at a fixed frequency independent of any software. The PWM pins provide the two pulse width modulated outputs; the SPI 0 is the serial peripheral interface bus lines. Finally, the PCM pins provide pulse code modulated audio outputs.

  • ALT 1: The pins are used as a secondary memory bus. Due to the design of the Raspberry Pi, this is of no use at all.

  • ALT 2: The only ALT 2 pins brought out the to the GPIO pin header are reserved.

  • ALT 3: The most useful pins here are the CTS0 and RTS0 lines; these are handshaking lines for the serial module if you need them. The BSC lines are for the Broadcom Serial Controller, which is a fast mode I2C-compliant bus supporting 7-bit and 10-bit addressing and having the timing controlled by internal registers. The SD1 lines are probably for the control of an SD card, but the BCM2835 ARM Peripherals document makes no other mention of it. It isn’t the way the Raspberry Pi accesses the SD card anyway.

  • ALT 4: The SPI 1 lines are a second SPI bus. And the ARM pins are for a JTAG interface. JTAG is a way of talking to the chip without any software on it. It’s very much used for the initial tests on a system during development, although it can be used for hardware debugging as well.

  • ALT 5: The useful pins here are the second serial port data and handshaking lines. The PWM lines are exactly the same PWM lines that are switches to GPIO 12 and 13 under ALT 0, only this time they’re switched to GPIO 20 and 21. There are also two of the general-purpose clock lines along with another copy of the ARM JTAG signals.

Powering from the Raspberry Pi

A common question is “Will the Raspberry Pi power this?,” to which the answerer is always “What do you mean by power?” Basically, there are three ways a Pi can power something and each has a separate answer.

Sourcing the power from the GPIO pin

The GPIO pins are quite fragile on the Raspberry Pi, compared with other boards like the Arduino. Each GPIO pin can source (provide the current) or sink (suck the current into the pin to switch something to ground) about 16mA. The total amount of current sourced or sunk from all pins added together should be restricted to about 50mA, which spreads out to about 3mA each for the non-plus models.

The only thing you should be directly powering from a GPIO pin is an LED, and only with a low current at that. Everything else needs to go through some sort of driver — normally a transistor or FET. Also, the output voltage is only 3V3, so many devices need more voltage as well as more current.

Sourcing the power from the Pi

This is where the signal to control something is derived from the GPIO pins, but the actual power to drive it is from the 3V3 or 5V internal power lines of the Raspberry Pi.

Using the 3V3 supply is tricky because that line is going through the onboard voltage regulators and there is a limited amount of spare current available before you get overheating of the regulators. Don’t take any more than 50mA from this power rail. If you want more, consider using a voltage regulator on the 5V line.

Much more promising is the 5V line because it’s derived from the same power source that is powering the whole Pi. Because the Pi takes up to 800mA at peak, if you power the Pi with a 2A supply, you could take about 1.2A from the 5V lines. Parallel up the two 5V pins on the GPIO connector and at least two grounds to reduce the resistance introduced by the connector. But at the upper end of this current draw, expect a bit of voltage droop — you might get a volt or so lost and end up with only about 4V.

Sourcing the power from an external supply

There are times when you need a voltage higher than 3V3 or 5V to control a device. If this is the case, you need a separate power supply. This means that the ground or –ve terminal of the external power supply must be connected to the ground of the Raspberry Pi. A common fear here is that this will in some way damage the Pi, but as long as it’s only the ground that’s connected, there is nothing to worry about.

For example, a typical application might be to drive a 12V relay.

image0.jpg

The way a transistor works ensures that the 12V does not leak back into the Raspberry Pi.

The relay contacts are completely separate from the Pi; this is said to be isolated. Any sort of coil or inductor must have a diode to protect against back EMF; this is a large reverse voltage spike that occurs when the relay coil’s current is removed and the magnetic field collapses. The diode shorts this out and prevents it from causing any damage.

The 1K resistor in the base of the transistor ensures that you don’t draw too much current from the GPIO pin. This circuit is applicable to other things as well — for example, instead of the relay coil, you can have a motor.