Tuesday, August 28, 2012

Always Sage Advice - Use Protection

If you read any about interfacing to the Pi GPIO pins you are bound to come across the dire warnings - Be Careful.  These pins connect directly to the microprocessor without any protection.  You can destroy the Universe if you wire something wrong.

OK.  Maybe not that dire, but you get the idea.  You may get away with interfacing directly with the GPIO pins (I know I have) but even if your circuit is designed perfectly, accidents still happen.  Something can fall across the circuit board and short things out.  So it is wise to protect your Pi.

I am building an interface that will connect to hard-wired alarm circuitry.  That means lots of lines running lots of places and just that many more opportunities for bad things to happen.  This protection is also a good idea since a lot of people using the Pi will likely be from the younger and less experienced crowd.  This is what the Pi was developed to encourage, so having a safe way to interface to the most flexible I/O on the device is critical.

Here are some of the options for protecting a logic circuit.

Zener Diode - A Zener diode is one that allows no reverse current to flow until a threshold is reached.  Above that threshold, current will flow.  A 3.3V Zener diode between a GPIO pin and ground can protect it from any over voltage that is applied.  Any voltage above 3.3V will just be shunted to ground.

Mike Cooke has provided a design for a screw terminal break-out board for the Raspberry Pi GPIO that uses Zener diodes for protection.  The design may be found here: Raspberry Pi Breakout Box

Transistor - A transistor can be used to switch a known safe logic level.

Line Driver or Buffer - Chips that contain multiple transistor switches internally.  These are easier to install and provide a cleaner design when you have many lines to protect.

Opto-Isolators - Chips similar to Buffers but these use pairs of internal LEDs and optical sensors instead of transistors.  This provides total circuit isolation and can be useful in a noisy electrical environment.

A Bi-directional Logic Level Converter seemed like the perfect thing to use. However, I tried the TXB0108 provided by Adafruit and had problems.  It seemed to work fine in my prototyping board, but when I put things together, I found that it had problems driving some TTL chips.  I checked it with a multimeter and found it only put out 2.5 volts when asserted on the 5V side.  This wasn't enough for an input into the MAX232N that is used for the RS-232 interface.

I am interested in hearing from anyone else who has tried this chip.

The GPIO pins that are used for output on my interface are protected by transistors which control relays.  That is more than adequate protection.  I am still pondering what route to take now for protecting the inputs. Opto-isolators are something I am already familiar with (and probably already have some) so I am leaning toward that option.

Eventually, I will build a new version of my interface so that I can apply all that I have learned along the way. I already regret not leaving pins open for I2C.  That will have go in the next revision.

Monday, August 27, 2012

Raspberry Pi Serial Port

Prototyping the serial port interface.
Many of the GPIO pins on the Pi have other special uses.  The most useful of these are the serial port pins #8 and #10, which are transmit and receive for an RS-232 serial port.  By default this port will output diagnostic messages during boot and then provide a user login.  The configuration is 8 bits, no parity, 1 stop bit, no hardware handshaking, at 115200 baud.  The device name is /dev/ttyAMA0.

I need to use this serial port to interface to my X10 system via a CM11A computer interface module.  That can be a topic for several future posts.

First lets cover some important facts about RS-232 and voltage levels.  The Pi uses levels that are 0V to represent a zero and 3.3V to represent a one.  RS-232 uses -3V to -15V to represent a zero and 3V to 15V to represent a one.  Thus, a level converter is required to create this interface.  The MAX232 series of chips was designed for this exact purpose.

External capacitors are needed to drive the charge pumps inside the chip.  Note: There are several variations of the MAX232 chip which have different requirements.  The one shown in the circuit here uses 0.1uF capacitors.  The ones I have use 1.0uF and some versions even have the capacitors built in.  When in doubt, check the datasheet for the chart that shows the requirements for each variation.
Data Sheet for MAX232 family

If, like me, you want to take complete control of the serial port for your own uses, there are two configuration changes to make:

First, disable the boot up and diagnostic output to the serial port.
sudo vi /boot/cmdline.txt
and remove the two options referring to the serial port.
So, this
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
becomes this
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Second, disable the login prompt
sudo vi /etc/inittab
find the line near the end
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
and delete it or comment it out by putting a # at the start of the line.

Reboot and the serial port will now be free for your exclusive use.

Saturday, August 25, 2012

My Pi Has Arrived

It's Chrismas in July!  My Raspberry Pi has finally arrived.  Setup was pretty easy:

  • Standard cell phone charger (micro USB) at least 700mA
  • either an HDMI or composite video connection
  • USB keyboard and mouse
  • Cat5 cable run to my network switch
  • Compact Flash card, 4GB, loaded with Raspbian “wheezy” OS
The system booted up just as expected.  I logged on and entered "startx" to begin the graphical interface.  Gave it a test drive.  The processing power is on the low side but still pretty good.  A powerful graphics chip makes 1080p HD video possible.  I am impressed.

Ran the configuration program
sudo Raspi-config 
set timezone and enable SSH

Set the IP address to static (default is automatic)
sudo vi /etc/network/interfaces
and change the eth0 section to
iface eth0 inet static
Then restart eth0 to take the change
sudo ifdown eth0
sudo ifup eth0

Create a user for myself and set the password
adduser ted
passwd ted

Change the default "pi" user's password while I'm at it.

Update the package manager and install Samba to create windows shares.
apt-get update
apt-get install samba
apt-get install samba-common
Configured and created the /pihome network share (google "Samba Howto")
vi /etc/sambe/smb.conf 

I am using my preferred development environment on my Windows system and access the files via the network share.  The command line via SSH is used to build it.

I installed the WiringPi library and got the GPIO part working.

My next post will describe the hardware interface I an building - serial port, 8 inputs, 6 relay outputs.