Raspberry Pi 2nd UART a.k.a. Bit Banging a.k.a. Software SerialFollow article
How do you feel about this article? Help us to provide better content for you.
Thank you! Your feedback has been received.
There was a problem submitting your feedback, please try again later.
What do you think of this article?
“If the Raspberry Pi only has 1 UART, what if I need 2?”
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).
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()