Connect UART (Serial) Device to Raspberry Pi GPIO



There are many useful devices that can communicate via UART. One useful example would be an Arduino. Here, I'll show you how to connect UART device to a Raspberry Pi 2 and communicate with it using a serial program. In this case, I'll be using a Moteino, which is an awesome pseudo-Arduino device that has the added awesomeness of wireless RF capability. These things are extremely useful for home automation projects. I'm in the process of developing a home security system, and I plan to have many wireless accelerometers attached to all of my windows and doors to monitor for break-ins. Each sensor node will consist of an accelerometer connected to a Moteino over I2C, and each node Moteino will send its data to a gateway Moteino device that will be connected via UART to the Raspberry Pi. I can then use the Raspberry Pi to operate based on that accelerometer data (turn on Pi Camera, send text messages, alert the police, whatever).

Below is an image of the GPIO on the Raspberry Pi 2.

To use the Pi to both power and talk to the Moteino, we only need to connect 4 pins.

Here's a photo of my setup:

The Raspberry Pi, by default, reserves the UART pins for programming purposes. If we want to use it to connect to another device, we need to make a couple of changes.

First, we need to make a change in /boot/cmdline.txt

We need to remove the text "console=ttyAMA0,115200". The result should look like this:

The second change we need to make is in /etc/inittab Find the lines that say  T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
 * 1) Spawn a getty on Raspberry Pi serial line

Now comment out the second line by adding a # at the beginning. It should look like this: 
 * 1) Spawn a getty on Raspberry Pi serial line
 * T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Once you've made these changes, you need to reboot:

We can test our serial connection using a program such as minicom

For the Moteino, we can open UART communication with minicom by issuing the following command

Here "-b 115200" sets the baud rate to 115200, "-o" does not initialize (whatever that means), and "-D /dev/ttyAMA0" tells the program where the device is. (The UART GPIO is addressed as /dev/ttyAMA0.) If all goes well, you should now have a connection. Here's the output from my Moteino:

To exit minicom hit: 