Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Arduino compatible coding 17: Using softwareSerial in Arduino

By Nikhil Agnihotri March 6, 2022

In the previous tutorial, we learned about serial communication in Arduino using the universal asynchronous receiver-transmitter (UART). We also discussed how Arduino can talk with a computer system using the UART protocol. 

The UART is a dedicated circuit that implements serial communication according to its protocol. Arduino boards have one or more UART/USART. These UART/USART interfaces are available through the header of the Arduino boards. At least one UART (available at pins 0 and 1) is shared with the USB port of Arduino, which enables loading sketches to the board over a USB interface. 

Apart from the UART hardware, it’s also possible to implement software serial on any digital I/O pin of Arduino. The software serial simply replicates the functionality of the UART hardware. This replication is done via the softwareSerial library. 

The softwareSerial library is based on the NewSoftSerial library by Mikal Hart. This library virtually implements the UART protocol on any digital I/O pin of Arduino. Multiple software serial ports can be defined in a user-program for full-duplex serial communication with several devices. Despite multiple software serial ports, however, only one port can be used at a time. 

The software serial ports can communicate data at speeds as high as 115200 bps. The embedded sensors typically communicate data to the controllers or computers at low speeds. The software serial is sufficient enough to efficiently communicate data with most of the embedded sensors. 

It’s also worth noting that the data reception through the software serial can only be implemented on those Arduino channels/pins that support change interrupts. 

This table summarizes the pins on the different Arduino boards available for the software serial:

The softwareSerial library
To implement software serial in an Arduino sketch, the softwareSerial library can be used. This library can be imported in a sketch using this statement:

#include <SoftwareSerial.h>     

The library contains these methods:

SoftwareSerial() – used to create an instance of the softwareSerial class. Multiple instances can be created in a sketch, but only one instance can be used to receive/transmit data at a time. This method has this syntax: 

SoftwareSerial <serialPortInstance>(rxPin, txPin, inverse_logic)

SoftwareSerial takes three parameters and the third one is optional. 

  • The first parameter, RxPin, is the pin number assigned to receive the serial data. 
  • The second parameter, TxPin, is the pin number assigned to transmit the serial data. 
  • The third (optional) parameter is inverse_logic. If set to “True,” then the logical LOW at the RxPin is treated as bit 1 and the logical HIGH at the RxPin is treated as bit 0. By default, the inverse_logic parameter is set to “False.” 

This is a valid example of this method: 

 SoftwareSerial portOne(2, 3);  

softwareSerial.begin() – sets the baud rate for the software serial communication. It must be called after creating a software serial instance. The supported baud rates are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200 bps. It has this syntax:

serialPortInstance.begin(baud_rate)

The method only takes one argument that’s the baud rate for the software serial communication. The baud rate is in bits-per-second (bps). Here’s a valid example of this method:

 portOne.begin(9600);

softwareSerial.available() – returns the number of bytes available to read from the software serial port. It has this syntax:

serialPortInstance.available() 

This method requires no arguments. Here’s a valid example:

if (portOne.available()>0)
{
portOne.read();
}

softwareSerial.listen() – used to select a port to listen for incoming data. Only one software serial port can be selected at a time. When a port is selected to listen, it means the data has already arrived at the software serial ports and is rejected. 

While a port is selected to listen for the serial data, incoming data from other software serial ports are also discarded. This method has this syntax:

serialPortInstance.listen() 

And this is a valid example: 

portOne.listen(); 

softwareSerial.isListening() – used to test if the requested software serial port is listening for the incoming data or not. If the requested port is active, the method returns True. Otherwise, it returns False. It has this syntax:

serialPortInstance.isListening() 

Here’s a valid example of this method:

if (portOne.isListening())
{
Serial.println(“Port One is listening!”);
}

softwareSerial.read() – returns a character received on the software serial port. If no data is available to read, then it returns -1. It has this syntax:

serialPortInstance.read() 

Here’s a valid example of this method:

char c = portOne.read();

softwareSerial.peek() – returns a character received on the software serial port. It’s different from the read() because in subsequent calls, it returns the same character. It has this syntax:

serialPortInstance.peek() 

Here’s a valid example of this method:

char c = portOne.peek(); 

softwareSerial.write() – prints data to the TxPin of the selected software serial port. This data is printed as bytes. The method returns the number of bytes printed to the selected software serial port. It has this syntax: 

serialPortInstance.write(data_to_transmit) 

The method takes the data to be printed as an argument. Here’s a valid example:

portOne.write(“hello”); 

softwareSerial.print() – used to send ASCII characters to the serial port. This function needs a value or string as an argument. It returns the number of bytes written to the port, although reading that number is optional. It also accepts BIN, OCT, DEC, or HEX as optional arguments to specify the base format of the characters (either binary, octal, decimal, or hexadecimal). 

If the value passed is a floating-point number, the number of decimal places can be passed as an argument. This method has this syntax:

serialPortInstance.print(data_to_transmit)

This is a valid example:

portOne.print(“hello \t”); 

softwareSerial.println() – the same as the print() except that the sent ASCII characters are followed by a carriage return (‘\r’) and a newline character(‘\n’). This method has this syntax:

serialPortInstance.println(data_to_transmit)

Here’s a valid example:

portOne.println(“hello”); 

softwareSerial.overflow() – tests if the software serial overflow has occurred. The software serial buffer can only store 64 bytes. 

On calling this method, the overflow flag is cleared. Therefore, a subsequent call to this method does not return True until the overflow occurs again. It has this syntax:

serialPortInstance.overflow()

This is a valid example:

if (portOne.overflow())
{
Serial.println(“SoftwareSerial overflow!”);
}

Recipe: Reading data from the NEO-6MV2 GPS modem using softwareSerial
In this recipe, we connect the NEO-6MV2 GPS modem to Arduino’s software serial port at pins 2 and 3. Then, we observe the GPS data on Arduino IDE’s serial monitor of Arduino by connecting the Arduino UNO board to a computer via a USB. 

Components required 

1. Arduino UNO x1
2. NEO-6MV2 GPS modem x1
3. USB cable x1
4. Jumper wires or connecting wires

Circuit connections 

  • Connect the Tx pin of the GPS modem to Arduino UNO’s pin 2
  • Connect the Rx pin of the GPS modem to Arduino UNO’s pin 3 
  • Supply the 5V and ground to the NEO-6MV2 modem from Arduino 
  • Connect Arduino to a computer via a USB cable and open the serial monitor from Arduino IDE

Arduino sketch 

How the project works
The NEO-6MV2 GPS modem is connected to a software serial port of Arduino. The software serial port’s RxPin is assigned to pin 2. Its TxPin is assigned to Arduino UNO’s pin 3.

Arduino UNO is connected to a computer via a USB cable. The serial port of Arduino is shared with the USB interface and it communicates data to the computer via the UART hardware.

Arduino is programmed to read data at its software serial port from the GPS modem and transmit it to the serial port. From Arduino’s serial port, the GPS data is observed on Arduino IDE’s serial monitor.

Programming guide
The Arduino sketch begins by importing the softwareSerial library. In the setup() function, Arduino’s serial port is initialized to a 9600 baud rate and a message is printed on the serial port to convey that the GPS modem is connected to the software serial port.

An instance of the software serial port is defined with the name, ‘portOne,’ with Arduino UNO’s pin 2 assigned to the RxPin and Arduino UNO’s pin 3 assigned to the TxPin.

In the loop function, the data is read from the software serial port, ‘portOne,’ and transferred to Arduino’s serial port. The data from Arduino’s serial port is observed on Arduino IDE’s serial monitor.

In the next tutorial, we’ll cover synchronous serial communication in Arduino by using the I2C protocol.

 

 


Filed Under: Arduino Projects
Tagged With: Arduino
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • BOM sent to Contract assemblers doesnt correspond to schem
  • Half-bridge LLC resonant converter using UCC25600
  • Lightbox circuit help
  • Transistor circuit in audio
  • Antiparallel Schottky Diodes VDI-Load Pull

RSS Electro-Tech-Online.com Discussions

  • Developing DC UPS module
  • An Update On Tarrifs
  • Trying to use a L9110s motor driver chip
  • I want to make a CRT with some modifications But i have no Idea where to start
  • Funny Images Thread!

Featured – Designing of Audio Amplifiers part 9 series

  • Basics of Audio Amplifier – 1/9
  • Designing 250 Milli Watt Audio Power Amplifier – 2/9
  • Designing 1 Watt Audio Power Amplifier – 3/9
  • Designing a Bass Boost Amplifier – 4/9
  • Designing a 6 Watt Car Audio Amplifier – 5/9
  • Design a low power amplifier for headphones- 6/9

Recent Articles

  • Fischer connector system adds ratchet locking system designed for 300g shock resistance
  • Littelfuse introduces tactile switch with enhanced bracket peg design for mounting strength
  • Infineon releases GaN switch with monolithic bidirectional design
  • Sienna Semiconductor data converters feature sample rates from 20 to 250 Msps
  • Delta’s 5,500 W power supplies achieve 97.5% energy efficiency for AI servers

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe