Raspberry Pi I/O
The Raspberry Pi has a General Purpose Input/Output (GPIO) connector and this exposes a set of signals and buses. There are 8 general purpose digital I/O pins which can be programmed as either digital outputs or inputs. One of these pins can also be configured for PWM output. There is also a 2-wire I2C interface and a 4-wire SPI interface (with a 2nd select line, making it 5 pins in total) and the serial UART with a further 2 pins. The I2C and SPI interfaces can also be used a general purpose I/O pins when not being used in their bus modes, and the UART pins can also be used if you reboot with the serial console disabled, giving a grand total of 8 + 2 + 5 + 2 = 17 I/O pins. The GPIO pins use a 3.3V logic level and have no internal protection. They are described in more detail here.
An interesting article on benchmarking the GPIO port.
We also have a generic design section on inputs and devices.
When a GPIO pin is set as an input it is 'floating' and has no defined voltage level. For us to be able to reliably detect whether the input is high or low we need to connect it so that it always reads high or low. To 'tie' the GPIO pin we connect either a 'pull up' or 'pull down' resistor. The Raspberry Pi has these internally and each GPIO pin configured as input can be configured with either the pull up or pull down resistor enabled.
A pull down resistor connects the pin to ground through a resistor. This means that when the switch shown is open, there is a path to ground and so it will read low. When the switch is closed, there is a lower resistance path to the 3.3V supply and it will then read high.
A pull up resistor connects the GPIO pin to the 3.3V supply so that it always reads high. When the switch is closed, there is a lower resistance path to ground and so it will read low.
In both of the above circuits, the (10kΩ) resistor ensures that only a low current is drawn when the switch is pressed.
Both the above circuits could damage the pins if they are accidentally set to be outputs. If we drive an output and close the switch we could create a short circuit between the 3.3V supply and ground. To avoid this occurring by accident, a current limiting resistor (say 1 kΩ) is connected into the GPIO pin.
An opto-isolator is basically a light controlled switch. It has the advantages of providing optical isolation and enabling higher voltages to be used to control the input, e.g. 12 V dc shown here. The 2.4 kΩ resistor limits the current into the opto-isolator LED to about 5mA. We typically use a The 3.9 kΩ resistor though, to reduce the current to less than 3 mA. If full electrical isolation is not important, you can also use a common ground.
This circuit is configured in pull down mode but it could equally work in pull up mode. Some sensors are 'normally closed' (e.g. PIR sensors & door contacts) and some are 'normally open'. Either way, the software can be written to handle either type of sensor or device. We are using 12Vdc for signaling around our home and for all aspects of home automation, as it has higher noise immunity and most devices (PIR sensors, etc.) are designed to work with 12V. Every door in our current house has a door contact sensor on it and every room (bedrooms aside) has a PIR movement sensor. These all use standard 6-core alarm cable back to a central point and this approach has proved incredibly reliable over many years.
We are using the ILQ74 quad opto-isolator in all of our applications (ILQ74 datasheet). It's cheap and reliable and the input LED is compatible with a wide range of input voltages, including the 3.3V found on the Raspberry Pi.
GPIO pins configured as output needs to be protected from excessive currents. The use of an ILQ74 opto-isolator IC can ensure as little as 2mA is required from each pin to switch high power loads.
We also have a generic design section on outputs and devices.
3rd Party Hardware
The IO Pi 32 Channel Port Expander is a 32 channel digital expansion board based around the MCP23017 16-bit I/O expander and a pair of MCP23017 expanders are included on the board supporting up to 32 digital inputs or outputs. The IO Pi Expander is powered through the host Raspberry Pi using the GPIO port and extended pins on the GPIO connector allow you to stack the IO Pi along with other expansion boards. The I2C address bits are selectable using the on-board jumpers. The MCP23017 supports up to 8 different I2C addresses so with two MCP23017 devices on each IO Pi you can stack up to 4 IO Pi boards on a single Raspberry Pi giving a maximum of 128 I/O ports. The IO Pi includes a 5V port that can be isolated from the Raspberry Pi via an isolation jumper so you can use a separate high current power supply to power the IO Pi reducing the load on the Raspberry Pi.
PicopBorg is a piggy-back IO board developed to turn on and off fans, motors, solenoids, relays etc. from your Raspberry Pi. It has 4 digital oupuits rated at 12V dc and 2.5A max current. It requires an external power supply to power the motors, etc.
PiFace is a piggy-back IO board developed by the University of Manchester. The board adds eight output and eight inputs to Raspberry Pi, some LEDs, switches and a couple of relays.
The Quick2Wire Pi Interface kit comprises of a main board and a GPIO expander board. The main board connects to the Raspberry Pi with a standard 26-way ribbon cable. On the main board there are expansion sockets for SPI devices, an 8-way GPIO connector and an I2C connector. I2C boards can be connected in series, so the board has a 2nd I2C connector for the next board along.
Robert Savage has built an useful I/O test board. He also has a video on his site, showing the board in operation.
Web & HTTP Control
There are quite a few projects, like the WebIOPi project. WebIOPi is a REST framework and a webapp which allows you to control the Raspberry Pi GPIO pins.
We have a separate page on Java programming on the Raspberry Pi.
Java Hardware Control
The Pi4J project was announced in September 2012. It is intended to provide a bridge between the native hardware and Java for full access to the Raspberry Pi in with a Java-friendly object-oriented approach. Pi4J is an open source project developed by professional software engineers. In addition to the basic raw hardware access functionality, this project also attempts to provide a set of advanced features that make working with the Raspberry Pi an easy to implement and more convenient experience for Java developers.
The Pi4J project has a Pi4J Google Groups forum for community discussions on Pi4J project development.
We have had a lot of success using Pi4J but there are a few things to look out for:
- Make sure you use the right pin numbering scheme.
- Make sure you include all the required packages.
WiringPi is a rework of the Arduino software package called Wiring. Wiring is the core of the input and output control capability for the Arduino, and this project replicates that functionality on the Raspberry Pi. WiringPi is a wiring-like library written in C and should be usable from C++ and many other languages with suitable wrappers. WiringPi is released under the GNU LGPLv3 license.
We are currently investigating Java software to enable I2C control.