SWseriale software serial library for Arduino

Recently, for my experiments, I am using an Arduino Nano 3.0 which I bought on Amazon Japan for a cheap price (300 Yen or so). One of the first things that I noticed is that this Arduino, equipped with Atmel ATmega328P microcontroller, unfortunately has only 1 hardware serial port available. For this reason, I decided to create a software serial library which makes use of Pin 3 (RX) and Pin 4 (TX), in order to manage an additional serial port. The library uses an external interrupt (INT1) on Pin 3, in order to catch the "start bit" transition (from 5V to 0V), plus an 8 bit timer (Timer 2) in order to create the timing for reading and writing information bits on the serial bus. Since this library only has few instruction codes, it is lighter than the usual Arduino Software Serial library. Since the transmission is managed using interrupts (with few microseconds execution time) at each bit timing, the calculation load of the CPU is very small, and also the main program execution is not locked during sending and transmission operations.

The library is available on GitHub, on my page (dadez87).

SWseriale Arduino library

In order to test the SWseriale library, I used a USB to Serial (TTL) converter adapter, which you can see in the picture above. The serial converter is connected to the Arduino using 4 pins: VCC (5V) for power supply, GND (0V), TX and RX. The adapter pin TX has to be connected to Arduino pin 3 (RX), while pin RX is connected to Arduino pin 4 (TX).

SWseriale Library Arduino 2The library is very simple, just about 250 lines of code, so it is very light and it uses a small space on ROM memory and RAM memory (space on RAM memory depends on the size of buffer arrays for sending and receiving data, which can be defined in the header). It can be included with the #include "SWseriale.h" directive.

SWseriale_example_04The example program just uses 1528 bytes, 4% of the available ROM flash memory. At the moment, the communication speed supported is 9600 baud, which means that 1 bit period is about 104.167 microseconds. Since the ATmega328P of the Arduino Nano is clocked at 16Mhz, and Timer2 is configured with a 8x prescaler, one timer tick happens in 0.5 microseconds. Therefore, the Timer2 max value has to be set to 207, which is equal to (104.167*2)-1. Since both sending and receiving operations use Timer2, it is not possible to send and receive at the same time, so the communication is half duplex. According to the standard "defines" settings inside the header file, in case a sending request is called during receiving phase, the library will save the data in the buffer and process the sending request at the end of the receiving phase. On opposite, in case a "start bit" is received, on RX pin, during sending phase, the program will continue to send and the received message is lost. For this reason, in order for this library to work properly, it is necessary to implement a simple master-slave communication protocol: master sends a message and waits to receive a reply from the slave, before sending the following message.

SWseriale_example_02A communication test was performed using RealTerm. The example sketch is visible at the end of this article. Arduino continuously checks if any character byte is received from SWseriale serial port, and in positive case, it forwards back the character to the sender. "SWseriale" array of character was sent, and the same string was received back from Arduino.

swseriale_example_03Finally, below, the example code is shown.

Author: Davide Cavaliere

I am an Italian Electrical Engineer graduated at Politecnico di Milano. My interests are motorcycles and cars, electronics, programming, Internet of Things, and Japanese culture.

Leave a Reply

Your email address will not be published. Required fields are marked *