SoftwareSerial Library
SoftwareSerial available begin islistening
overflow peek read print
println listen write
The SoftwareSerial library allows serial communication on other digital pins of an Arduino board.

The SoftwareSerial library allows serial communication on other digital pins of an Arduino board, using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol.

The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library by 'Mikal Hart'.

To use this library:

#include <SoftwareSerial.h>

Limitations of This Library

SoftwareSerial library has the following known limitations:

If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library .

Examples

Methods


SoftwareSerial() 

Create an instance of a SoftwareSerial object. Multiple SoftwareSerial objects may be created, however only one can be active at a given moment.

Syntax

SoftwareSerial( rxPin, txPin, inverse_logic)

Parameters

Returns

None.

Example

	#include <SoftwareSerial.h>
	const byte rxPin = 2;
	const byte txPin = 3;

SoftwareSerial mySerial( rxPin, txPin ); // Set up new SoftwareSerial object

See also


available() 

Get the number of bytes (characters) available for reading from a software serial port. This is data that has already arrived and stored in the serial receive buffer.

Syntax

mySerial.available()

Parameters

None.

Returns

The number of bytes available to read.

Example

#include <SoftwareSerial.h>

#define rxPin 10   
#define txPin 11

/*F********************************************************************
* Set up a new SoftwareSerial object7SoftwareSerial mySerial 
   =  SoftwareSerial(rxPin, txPin);
**********************************************************************/
void 
setup()  
{    
	pinMode( rxPin, INPUT );      // Define pin modes for TX and RX11    
	pinMode( txPin, OUTPUT );
	mySerial.begin( 9600 );
}
/*F********************************************************************
* Set baud rate for the SoftwareSerial object15    
**********************************************************************/
void 
loop() 
{
	if( mySerial.available() > 0) 
		mySerial.read();
}

See also


begin()  

Sets the speed (baud rate) for the serial communication. Supported baud rates are: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200 bauds.

Syntax

mySerial.begin( speed ) 

Parameters

Returns

None.

Example

#include <SoftwareSerial.h>23

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object7SoftwareSerial 

mySerial =  SoftwareSerial( rxPin, txPin);

/*F********************************************************************
*
**********************************************************************/
void 
setup()  
{
	pinMode( rxPin, INPUT ); // Define pin modes for TX and RX11    
	pinMode( txPin, OUTPUT );
	mySerial.begin( 9600 ); // Set the baud rate for the SoftwareSerial object
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{    
	// ...
}

See also


 isListening() 

Tests to see if requested software serial object is actively listening.

Syntax

mySerial.isListening()

Parameters

None.

Returns

Boolean.

Example

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 1   
SoftwareSerial portOne( 10, 11);

/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{
	Serial.begin( 9600 );                   // Set baud rate for Serial port
	portOne.begin( 9600 );        // Set baud rate for SerialSoftware object
}
/*F********************************************************************
*
**********************************************************************/
void 
loop()
{
if( portOne.isListening()) 
{
Serial.println("portOne is listening!");
}

See also


overflow()  

Tests to see if a SoftwareSerial buffer overflow has occurred. Calling this function clears the overflow flag, meaning that subsequent calls will return false unless another byte of data has been received and discarded in the meantime. The SoftwareSerial buffer can hold up to 64 bytes.

Syntax

mySerial.overflow() 

Parameters

None.

Returns

Boolean.

Example

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial portOne( 10, 11);

/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{    
	Serial.begin( 9600 );                   // SET BAUD RATE FOR sERIAL PORT
	portOne.begin( 9600 );        // SET BAUD RATE FOR SerialSoftware OBJECT
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
	if( portOne.overflow()) 
	{        
		Serial.println("portOne overflow!");
	} // ...
}

See also


peek()  

Return a character that was received on the RX pin of the software serial port. Unlike read(), however, subsequent calls to this function will return the same character. Note that only one SoftwareSerial object can receive incoming data at a time (select which one with the listen() function).

Syntax

mySerial.peek()

Parameters

None.

Returns

The character read or -1 if none is available.

Example

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial( 10, 11 );

/*F********************************************************************
*
**********************************************************************/
void 
setup()
{
	mySerial.begin( 9600 ); // Set baud rate for SerialSoftware object
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
	char c = mySerial.peek();
}

See also


read()  

Return a character that was received on the RX pin of the SoftwareSerial objecto. Note that only one SoftwareSerial object can receive incoming data at a time (select which one with the listen() function).

Syntax

mySerial.read() 

Parameters

None.

Returns

The character read or -1 if none is available.

Example

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 
// and TX in digital pin 11
SoftwareSerial mySerial( 10, 11 );
/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{    
	mySerial.begin( 9600 );        // Set baud rate for SerialSoftware object
}
/*F********************************************************************
*
**********************************************************************/
void loop() 
{    
	char c = mySerial.read();
}

See also


print()  

Prints data to the transmit pin of the SoftwareSerial object. Works the same as the Serial.print() function.

Syntax

mySerial.print( val )

Parameters

Returns

The number of bytes written (reading this number is optional).

Example

#include <SoftwareSerial.h>
// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 

SoftwareSerial mySerial( 10, 11 );

analogValue;
/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{
	mySerial.begin( 9600 );     // Set baud rate for SerialSoftware object
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
	analogValue = analogRead( A0 );           // Read analog value on pin A0    
	             // Print analogValue in the Serial Monitor in many formats:   
	mySerial.print( analogValue );      // Print as an ASCII-encoded decimal
	mySerial.print( "\t");                          // Print a tab character
	mySerial.print( analogValue, DEC);  // Print as an ASCII-encoded decimal
	mySerial.print("\t");                           // Print a tab character
	mySerial.print(analogValue, HEX);  // Print an ASCII-encoded hexadecimal
	mySerial.print("\t");                           // Print a tab character
	mySerial.print( analogValue, OCT);    // Print as an ASCII-encoded octal
	mySerial.print("\t");                           // Print a tab character
	mySerial.print( analogValue, BIN);   // Print as an ASCII-encoded binary
	mySerial.print("\t");                           // Print a tab character
	mySerial.print( analogValue/4, BYTE); 
		                                // Print as a raw byte value (divide
		         // value in 4 because analogRead() function returns numbers
		       // from 0 to 1023, but a byte can only hold values up to 255)
	mySerial.print("\t");                           // Print a tab character
	mySerial.println();                       // Print a line feed character
	// Pause for 10 milliseconds before the next reading
	delay(10);
}

See also


println()  

Prints data to the transmit pin of the SoftwareSerial object followed by a carriage return and line feed. Works the same as the Serial.println() function.

Syntax

mySerial.println( val )

Parameters

Returns

The number of bytes written (reading this number is optional).

Example

#include <SoftwareSerial.h>
// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial( 10, 11);
int analogValue;
/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{    // SET BAUD RATE FOR SerialSoftware OBJECT
	mySerial.begin( 9600 );
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{    
	// Read the analog value on pin A0    
	analogValue = analogRead(A0);
	// Print analogValue in the Serial Monitor in many formats:
	mySerial.print( analogValue );         // Print as an ASCII-encoded decimal
	mySerial.print( "\t" );                // Print a tab character    
	mySerial.print( analogValue, DEC);    // Print as an ASCII-encoded decimal    
	mySerial.print("\t");                // Print a tab character    
	mySerial.print( analogValue, HEX);    // Print as an ASCII-encoded hexadecimal
	mySerial.print("\t");                // Print a tab character    
	mySerial.print( analogValue, OCT);    // Print as an ASCII-encoded octal    
	mySerial.print("\t");                // Print a tab character    
	mySerial.print( analogValue, BIN);    // Print as an ASCII-encoded binary    
	mySerial.print("\t");                // Print a tab character    
	mySerial.print( analogValue/4, BYTE); // Print as a raw byte value (divide the
							   // value in 4 because analogRead() function returns numbers                                         
	// from 0 to 1023, but a byte can only hold values up to 255)    
	mySerial.print("\t");                // Print a tab character    
	mySerial.println();                  // Print a line feed character    
	// Pause for 10 milliseconds before the next reading36    
	delay(10);
}

See also


listen()  

Enables the selected SoftwareSerial object to listen. Only one SoftwareSerial object can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() function (unless the given instance is already listening).

Syntax

mySerial.listen()

Parameters

None.

Returns

Returns true if it replaces another.

Example

#include <SoftwareSerial.h>
// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 
SoftwareSerial portOne( 10, 11);56// Set up a new SoftwareSerial object with RX in digital pin 8 and TX in digital pin 9
SoftwareSerial portTwo( 8, 9);

/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{    
	Serial.begin( 9600 );            // SET BAUD RATE FOR THE SERIAL OBJECTS
	portOne.begin( 9600 );   // SET BAUD RATE FOR THE SERIALSOFTWARE OBJECTS
	portTwo.begin( 9600 );
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
	portOne.listen();           // Enable SoftwareSerial object to listen
	if( portOne.isListening())
	{
		Serial.println("portOne is listening!");
	}
	else
	{
		Serial.println("portOne is not listening!");
	}
	if( portTwo.isListening()) 
	{
		Serial.println("portTwo is listening!");
	} 
	else
	{
		Serial.println("portTwo is not listening!");
	}
}

See also


 write() 

Prints data to the transmit pin of the SoftwareSerial object as raw bytes. Works the same as the Serial.write()function.

Syntax

mySerial.write( val )

Parameters

Returns

The number of bytes written (reading this number is optional).

Example

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX 
// in digital pin 11
SoftwareSerial mySerial( 10, 11);

/*F********************************************************************
*
**********************************************************************/
void setup() 
{
	mySerial.begin( 9600 );  // Set baud rate for SerialSoftware object8    
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{    
	mySerial.write( 45 ); // Send a byte with the value 4513    
		// Send the string “hello” and return the length of the string
	int bytesSent = mySerial.write( “hello” ); 
}

See also