Skip to main content
shopping_basket Basket 0
Login

Raspberry Pi 2nd UART a.k.a. Bit Banging a.k.a. Software Serial

SparqEE
1

The Question:

“If the Raspberry Pi only has 1 UART, what if I need 2?”

title

The Answer:

Did I mention it’s SUPER EASY? Well it is – and I was surprised…

Since the Raspberry Pi only has one UART broken out over the I/O port, and the SparqEE CELLv1.0 uses said UART, what if you need the RasPi to access another?

It’s actually fairly straightforward to access a 2nd UART on the Raspberry Pi.  For SparqEE customers if they also want to use one of our SparqEE GPS boards, follow the same procedure!

Simply plug in the 2nd UART device into 2 GPIO pins (one TX on RX) rather than using a HW UART. 

Here’s what we came up with:

You can use the standard, HW UART on the Raspberry Pi to capture UART data.  In addition to this standard UART, as demonstrated below, you can use two GPIO pins on the Raspberry Pi to “Bit Bang” data in or as a “Software Serial” port rather than needing a 2nd Hardware one (that isn’t broken out).

The below software Serial port utilizes the great PIGPIO Library which you can download and install for free at the following URL: PIGPIO Library

Here’s the code I got working with the SparqEE GPS module running at 9600baud and the installation on the Raspberry Pi:

Execute this code on the Raspberry Pi to install:

wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install

Here’s the python code. Put it into a file such as “test.py,” change the permissions of the file “chmod 755 test.py,” then execute the script “./test.py”

With this code I simply wanted to input some GPS NMEA strings into the RasPi and print them out. I used GPIO pin 18 for the Receive (RX) side of the GPS device.

#!/usr/bin/python

import sys
import time
import difflib
import pigpio

RX=18

try:
        pi = pigpio.pi()
        pi.set_mode(RX, pigpio.INPUT)
        pi.bb_serial_read_open(RX, 9600, 8)

        print "DATA - SOFTWARE SERIAL:"
        while 1:
                (count, data) = pi.bb_serial_read(RX)
                if count:
                        print data
                time.sleep(1)

except:
        pi.bb_serial_read_close(RX)
        pi.stop()

The SparqEE Product Range

View our website at www.SparqEE.com

Recommended Articles

Comments

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

[Comment was deleted]

March 13, 2016 01:28

Hello, I tried implementing this code and indeed i do get an output every second, but the nmea lines which im getting are incomplete. Every second the "data" buffer is not being filled with lines in order, since im either starting to read at the middle of a line or i finish before the end of the line is read. Is there a way to get the clean output of all the nmea lines every second?

DesignSpark Electrical Logolinkedin