Tuesday, September 25, 2012

Great Article on Designing the RasPi

Wired has a great article by Pete Lomas, a co-founder and trustee of the Raspberry Pi Foundation, where he describes the design process for the Raspberry Pi.

http://www.wired.com/opinion/2012/09/raspberry-pi-insider-exclusive-sellout-to-sell-out/?pid=45&viewall=true


Saturday, September 22, 2012

So Many Inputs, So Few GPIO Pins

I keep coming up with so many improvements to my interface design (I'm up to version 6 now) that I'm not getting much built.  I decided that I had to make the pins for the SPI and I2C buses available.  But that means using five GPIO pins that I had other plans for.  I have to have at least eight inputs to handle the motion detectors and door sensors for the alarm system.  At least four relay outputs would be nice too, so the Raspberry Pi is quickly running out of GPIO pins.
Time to get a little help from another useful IC - the multiplexer.   There are several possible choices, but the obvious one is the 74151, of which I conveniently happen to have a few.

This diagram shows how this needs to be connected.  GPIO pins 1, 2, and 3 are set to output mode and used as address lines to select which input to read.  GPIO pin 0 is set to input mode and connected to the output of the multiplexer.

(Note: I always refer to WiringPi pin numbers, not the standard  BCM numbering.)

The result is that the interface can still have 8 inputs, but only use 4 GPIO pins to do it.

I will probably do the same thing (in reverse) to allow two GPIO pins to provide four select lines for the SPI bus.  It may be a while until I do that since the SPI bus is for future expansion.  I don't have any devices for it yet, just ideas.

Thursday, September 20, 2012

GPIO Output Relay Interface

Note:  See my revised relay circuit here.

To allow the greatest flexibility, my outputs are all relays.  This allows me to switch a variety of voltages and provides protection to the GPIO pins of the Raspberry Pi.  A relay is just a switch that is controlled by an electromagnetic coil.  Powering the coil will make the switch turn on.  The relays I used are made to mount on a circuit board and can be driven by 5V.  They can easily switch 12V or more at a moderate current level.  They are NOT meant to control house current!  That can be the topic of another post.

The Pi GPIO pins will only output 3.3V at a few milliamps.  This is not enough to drive the relay directly, but is is enough to switch a transistor on and off.  A common NPN switching transistor handles that job nicely.  I used the 2N2222 which is highly available (i.e. even Radio Shack carries it.)


Note:  NO SPST is a switch type designation and means Normally Open, Single Pole Single Throw.  This is the simplest type of switch.

A 1K ohm current limiting resistor is attached to the base of the transistor.  Power is connected to the relay coil which is then connected to the collector of the transistor.  The emitter is connected to ground.  When the GPIO pin is low, no current will flow from the collector to the emitter and the relay will be off.  Setting the GPIO pin to high will "turn on" the transistor, power will flow and the relay will turn on.

Notice that there is a diode attached across the coil of the relay in a reversed orientation.  There is magnetic energy stored in the coil while it is energized and holding the relay closed.  When power is removed, the collapsing magnetic field in the coil causes a brief but powerful surge of reverse voltage which can damage the switching transistor and cause premature failure.  The diode is there to stop this reverse voltage and protect the transistor.  I prefer to use relays that have internal surge suppression diodes and they are becoming more common now.  If your relay does not have the diode internally, then it is highly recommended that one be added.

Wednesday, September 19, 2012

GPIO Input Circuit

After holidays and many other distractions, I am finally able to get back to work on my Raspberry Pi interface.

Here is the GPIO input circuit that I came up with using an opto-coupler for protection. The opto-coupler that I chose is the LTV-847 (Jameco part number 878286) which provides 4 opto-couplers in a 16-pin DIP format.

Power applied to the anode and cathode will cause the internal LED to emit light.  This is detected by the internal photocell which controls the output.  Because there is no electrical connection between the input and output sides, opto-couplers are handy for connecting between very different voltage levels.  They are also excellent at preventing the introduction of electrical noise into a system.  For this application, the isolation will provide electrical protection to the Raspberry Pi.

The complete circuit for using this is shown below.  The 1KΩ resistor on the input is for limiting the current that can flow through the LED.  The 10KΩ pull-up resistor is internal to the Raspberry  Pi.  Be sure to set the pull-up option when you set the pin to input mode.  Using a separate 5V power supply for the interface provides greater protection than powering this all from the 5V line on the GPIO header.  If that line gets shorted to ground, or even if it just draws too much current, it can cause the Pi to suddenly reboot.


When the input is open, no current will flow through the detector and the Raspberry Pi will see the pin hi due to the pull-up resistor.  When the the input is connected to ground, current will flow and the Pi will see the the pin  as low, since it is effectively connected to ground now.