How to use Arduino Software Serial ?

Hello friends, I hope you all are fine and having fun. In today's tutorial, I am going to show you How to use Arduino Software Serial. In my previous tutorial, we have had a look at How to use Arduino Serial Write and How to use Arduino Serial Read. In both of these tutorials, we have done the hardware Serial Communication. But we all know that Arduino has just one Serial Port placed at pins 0 and 1.

So, if you are having two or more serial modules, then there's difficulty in adding two modules because we just have one hardware serial port. So, in such cases, there's a need to add one more serial port and that serial port can be created at any two pins of Arduino and is called software serial. Software Serial is also named Virtual Serial Port.

It's really very comfy if you are working on serial modules. If you ask me, I have never used a hardware serial port because pin # 0 and pin # 1 are also used for uploading code and debugging the code via Arduino Serial Monitor. So, I always connect my Serial modules via software serial and then check their output on Serial Monitor. So, let's get started with How to use Arduino Software Serial:

Where To Buy?
No.ComponentsDistributorLink To Buy
1Arduino UnoAmazonBuy Now

How to use Arduino Software Serial?

  • I am going to use Proteus software for testing the codes.
  • You can download the Proteus Simulation for Arduino Software Serial, by clicking the below button:
Download Simulation & Code

Arduino Code

  • First of all, let me tell you where you can find Examples of Software Serial.
  • Arduino has a Library of Software Serial in it. If you can't find its library then you should download the Software Serial Library.
  • Now copy and paste the below code in your Arduino software:
#include <SoftwareSerial.h>

SoftwareSerial SoftSerial(2, 3);

void setup()
{
  Serial.begin(9600);
  SoftSerial.begin(9600);
  
  SoftSerial.println("       **** Its a Software Serial **** ");
  SoftSerial.println(" Designed by www.TheEngineeringProjects.com");
  SoftSerial.println();
  
  Serial.println("       **** Its a Hardware Serial **** ");
  Serial.println(" Designed by www.TheEngineeringProjects.com");
  Serial.println();
}

void loop()
{
      
      if (Serial.available())
      {
           char data = Serial.read();
           SoftSerial.print(data);
      }
}
  • In the above code, we have first included the Arduino Software Serial Library using #include<SoftwareSerial.h>.
  • After that, I have created the SoftSerial object and its parameters are SoftSerial(RX, TX), so in the above code pin # 2 has become RX of our Arduino Software Serial and pin # 3 become TX.
  • Now our SoftSerial object is ready and then we have initialized our software serial by using SoftSerial.begin(9600), here we have started our software serial and the baud rate set is 9600.

Proteus Simulation

  • Now design a small circuit in Proteus, you will need Arduino Library for Proteus to use Arduino in Proteus, as shown in the below figure:
  • Now get the Arduino Hex File and upload it to your Proteus software.
  • Now run your Proteus Simulation and you will get something as shown in the below figure:
  • So, it's printed there that one is hardware serial and second is software serial and you can see the software serial is connected to Pin # 2 and Pin # 3.
  • Now when you write something in the Hardware Serial, it will also get printed in the Software Serial, that's the code which we have added in the loop() section.
So, that's all for today, I hope you have enjoyed this Arduino Software Serial example. Download the Simulation from the above button and try to design it on your own. Take care and have fun !!! :)

How to use Arduino Serial Flush?

Hello friends, I hope you all are fine and having fun with your lives. Today, I am going to share a very quick and basic tutorial in which I will show you How to use Arduino Serial Flush. I hope you guys are going to like it. We have seen How to use Arduino Serial Write? In that tutorial, we have sent some data over the Serial Port so you must recall that tutorial because we are going to use the same example today. Moreover, it's also good if you have a look at How to use Arduino Serial Monitor because that's also related.

Moreover, I hope that's going to be my last lecture on Arduino Serial Port because I have covered it in full detail. Although I am going to summarize all the Arduino Serial Post Commands in a single Post. You should also have a look at How to do Arduino Serial communication because, in that tutorial, I have combined all Arduino Serial Projects. Anyways, let's get started with How  to use Arduino Serial Flush:

Where To Buy?
No.ComponentsDistributorLink To Buy
1Arduino UnoAmazonBuy Now

How to use Arduino Serial Flush?

  • Arduino Serial Flush is used to flush the data sent through Arduino Serial Port.
  • When we send data on a serial port through Arduino then we use the command Serial.print() or Serial. write().
  • So when the data is sent it is sent using interrupt and when we use Arduino Serial Flush command then it makes sure that all the data is sent through serial port and nothing's left in the stream.
  • In simple words, it clears the serial data stream and kind of refreshes it and also makes you ready to send the next data.
  • Here's the syntax to use the Arduino Serial Flush command:

Serial.flush();

  • It doesn't return anything that's why we haven't assigned any variable to it.
  • It's just a simple function that clears the data on the transmitting pin of Arduino.
  • Now, if you want to remove that on receiving pin of Arduino, then you just need to write this command:

while(Serial.available());

  • But you make sure while using the above command because if you are receiving the data continuously then your code will remain stuck and won't move forward unless you got the complete data.
  • So, here's the small code which I have also used in the Arduino Serial Write tutorial but now I am using the Arduino Serial Flush command too.
  • So, here's the code:
#include 
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
 
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.setCursor(1,0);
  lcd.print("www.TheEngineering");
  lcd.setCursor(4,1);
  lcd.print("Projects.com");
  lcd.setCursor(1,0);
  Serial.begin(9600);
 // lcd.clear();
  
  Serial.write("www.TheEngineeringProjects.com");
  Serial.flush();
}
 
void loop() 
{
}
  • In the above code, I have simply added the Arduino Serial Flush code and now it will take a little more time to complete because now it will make sure that all the data has been sent.
So, that's all for today. I hope you guys have enjoyed this short Arduino Serial Flush tutorial. If you have any questions, please ask in the comments. Take care !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir