# Sending Data via Software Serial

**URL:** <https://forum.arduino.cc/t/sending-data-via-software-serial/575998>\
**Category:** Programming\
**Created:** [February 24, 2019, 2:44pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998 "2019-02-24T14:44:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 24, 2019, 2:44pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/1 "2019-02-24T14:44:34Z")

</div>

Dear All,

I am very new to Arduino Programming. I have connected 10K potentiometer to A0 and A1 of Uno. When I run the program, default Analog Read Serial, I get correct values of the potentiometer on the Serial Monitor.

I wrote another program to send data to another Arduino via Software Serial and when I run the program, I get junk data. When I change the A0 potentiometer, I get erratic values. Changing A0 pot also changes the A1 input. I tried putting delays, but didn't help. The default program example works but the one I wrote gives errors. I am sure I am making some syntax mistakes or my approach is wrong. I wrote this program by looking others program on the net. Could you please help me out with it?

* * *

Working Properly:

/\*  
AnalogReadSerial

Reads an analog input on pin 0, prints the result to the Serial Monitor.  
Graphical representation is available using Serial Plotter (Tools \> Serial Plotter menu).  
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.

> **[Analog Read Serial | Arduino Documentation](https://docs.arduino.cc/built-in-examples/basics/AnalogReadSerial)**
>
> Read a potentiometer, print its state out to the Arduino Serial Monitor.

\*/

// the setup routine runs once when you press reset:  
void setup() {  
// initialize serial communication at 9600 bits per second:  
Serial.begin(9600);  
}

// the loop routine runs over and over again forever:  
void loop() {  
// read the input on analog pin 0:  
int sensorValue0 = analogRead(A0);  
int sensorValue1 = analogRead(A1);  
// print out the value you read:  
Serial.println(sensorValue0);  
Serial.println(sensorValue1);  
delay(1000); // delay in between reads for stability  
}

* * *

Written by me, which gives errors:

/_To send these values to another Arduino usung Software Serial_/

#include \<SoftwareSerial.h\>  
SoftwareSerial mySerial(4, 5); //RX, TX, These pins will be used to send the data to another Arduino

/\* #### varialbles for potentiometers, handles ##########\*/

int sensor0 = 0; // analog pin used to connect the potentiometer  
int sensor1 = 1; // analog pin used to connect the potentiometer

int ValSensor0;  
int ValSensor1;

String strValSensor0;  
String strValSensor1;

void setup() {  
// put your setup code here, to run once:  
Serial.begin(9600);  
mySerial.begin(9600);  
Serial.setTimeout(10);  
mySerial.setTimeout(10);  
}

void loop() {  
// put your main code here, to run repeatedly:  
//########## code for potentiometers, handles #################  
ValSensor0 = analogRead(ValSensor0); // reads the value of the potentiometer (value between 0 and 1023)  
delay (50); // To Stabilize  
ValSensor1 = analogRead(ValSensor1);  
delay (50); // To Stabilize  
ValSensor0 = map(ValSensor0, 0, 1023, 0, 255); // Map Valuse  
ValSensor1 = map(ValSensor0, 0, 1023, 0, 255); // Map Valuse  
strValSensor0 = String(ValSensor0); // Convert to string  
strValSensor1 = String(ValSensor1); // Convert to string

//read figures in serial monitor  
Serial.println("handle Sensor 0: "+ strValSensor0);  
Serial.println("handle Sensor 1: "+ strValSensor1);

mySerial.print(":" + strValSensor0 + "," + strValSensor1 + ";"); // Send the values to another Arduino via Software Serial 4,5  
int ValSensor0=0 ;  
int ValSensor1=0 ;  
mySerial.flush();//Waits for the transmission of outgoing serial data to complete.  
delay(50);//allow some time for the serial buffer

}

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 24, 2019, 2:54pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/2 "2019-02-24T14:54:34Z")

</div>

```cpp
  ValSensor0 = analogRead(ValSensor0);

```

Check this line of code carefully

Which pin is being read ?  
Which variable will the answer be stored in ?  
What happens the next time the pin is read ?

also

Outside of any function

```cpp
int ValSensor0;

```

A variable of type int with global scope

Then in the loop() function

```cpp
  int ValSensor0 = 0 ;

```

A different variable with the same name of type int with its scope limites to the loop() function

Which of the two variables do you expect the program to use and where ?

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 24, 2019, 2:55pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/3 "2019-02-24T14:55:28Z")

</div>

After posting, I realised I was reading wrong Variable.

ValSensor0 = analogRead(ValSensor0); (Here it should be sensor0 instead of ValSensor0)  
delay (50); // To Stabilize  
ValSensor1 = analogRead(ValSensor1); (Here it should be sensor1 instead of ValSensor1)

But still getting the same error,

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 24, 2019, 3:04pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/4 "2019-02-24T15:04:02Z")

</div>

> still getting the same error,

Post the revised program but use code tags when you do to make it easier to copy to an editor.

See [read this before posting a programming question](http://forum.arduino.cc/index.php?topic=97455.0)

Please describe the error in more detail. What exactly do you receive ?  
How are you viewing the received data ?  
If it is a program on a second Arduino then please post that program

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 24, 2019, 3:19pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/5 "2019-02-24T15:19:15Z")

</div>

Thank you Sir for your response. Appreciate it. I'll revise the program again carefully and post with more details. Thanks a Ton!! 🙂

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [February 24, 2019, 4:26pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/6 "2019-02-24T16:26:19Z")

</div>

@OP

**1.** Please, try build your circuit between UNO-NANO (or UNO) as per following diagram. In this circuit, UNO will acquire signal from R1 and R2 pots and then will display their values (in hex format) on Serial Monitor-1. The UNO will send these two values to NANO (or another UNO) using 'Software Serial Port (SUART Port)'; the NANO will receive the data and will show them on the Serial Monitor-2. If keep changing R1 and R2 slowly, you will see the changed values on both Serial Monitors simultaneously.  
 ![uart-9y1.png](https://europe1.discourse-cdn.com/arduino/original/4X/c/4/e/c4e79e62d9e6eca5c08359b34e412687b91bea38.png)  
Figure-1: UNO and NANO connection using SUART Port

**2.** Upload the following sketch in UNO

```cpp
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); //RX, TX, These pins will be used to send the data to another Arduino

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  unsigned int ValSensor0 = analogRead(A0); //R1 pot
  unsigned int ValSensor1 = analogRead(A1); // R2 pot

  //read figures in serial monitor
  Serial.print("handle Sensor 0: ");
  Serial.println(ValSensor0, DEC);
  Serial.print("handle Sensor 1: ");
  Serial.println(ValSensor1, DEC);
  //--send R1 value to NANO------
  mySerial.write(0x01); //R1 Pot Data is next one
  mySerial.print(ValSensor0, DEC);
  mySerial.write(0x02);  //end-of-message
  delay(1000);
}

```

**3.** Upload the following sketch in NANO

```cpp
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); //RX, TX, These pins will be used to send the data to another Arduino
int i = 0;
char dataArray[50] = "";
bool flag1 = false;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  byte n = mySerial.available();
  if (n != 0)
  {
    if (flag1 == false)
    {
      byte x = mySerial.read();
      if (x == 0x01)
      {
        Serial.print("Received Sensor 0: ");
        flag1 = true;
      }
    }
    else
    {
      byte x1 = mySerial.read();
      if (x1 != 0x02)
      {
        dataArray[i] = x1;
        i++;
      }
      else
      {
        dataArray[i] = 0x00;  //nul-byte
        Serial.println(dataArray);
        i = 0;
        flag1 = false;
      }
    }
  }
}

```

**4.** Check that Sensor 0 (for Pot R1) appears on Serial Monitor of NANO.  
 ![sm47.png](https://europe1.discourse-cdn.com/arduino/original/4X/a/a/9/aa94029f503a9cf0034957e5629aca66bff3616a.png)

**5.** Add codes with Sketch of UNO and NANO so that Sensor 1 (Pot R2) value appears on SM-1 ans SM-2.

![uart-9y1.png](https://europe1.discourse-cdn.com/arduino/original/4X/c/4/e/c4e79e62d9e6eca5c08359b34e412687b91bea38.png)

![sm47.png](https://europe1.discourse-cdn.com/arduino/original/4X/a/a/9/aa94029f503a9cf0034957e5629aca66bff3616a.png)

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 25, 2019, 4:41pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/7 "2019-02-25T16:41:25Z")

</div>

@GolamMostafa

Thank you Sir. Just going through the code and trying it. Will get back to you soon. Appreciate it.

Warm Regards,  
Sachin

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 26, 2019, 2:37pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/8 "2019-02-26T14:37:27Z")

</div>

Hello All,

I had assigned some wrong variables. I got my mistake and rectified it. The Sender Arduino Uno is working perfectly fine. I checked on Logic Analyzer and it is sending in the format.

**:strValSensor0,strValSensor1;strValSensor2,strValSensor3;**

The Receiver is Arduino Mega

Attached is .ino file (mega, Receiver) and the Screen Cap of Arduino Uno Serial Monitor

I dont see anything in Serial Monitor of Mega.

Thanks and Regards,  
Sachin

![Serial Mon Uno.jpg](https://europe1.discourse-cdn.com/arduino/original/3X/8/e/8e6af8f750d9fbfd684d6d744d9815d694207064.jpg)

[Serial\_Receive.ino](https://forum.arduino.cc/uploads/short-url/7fZjywc9Pl20CbVfqTKBnNrzbk5.ino) (1.68 KB)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 26, 2019, 2:45pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/9 "2019-02-26T14:45:44Z")

</div>

```cpp
  SoftwareSerial mySerial(4, 5); //RX, TX

```

The Mega has 4 hardware serial ports. Why are you doing SoftwareSerial on the Mega?

Why are you doing SoftwareSerial on the Mega without reading the page on SoftwareSerial, and noting which pins on the Mega you can, and can not, use?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 26, 2019, 2:47pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/10 "2019-02-26T14:47:24Z")

</div>

```cpp
if(mySerial.available()){      //Read the values from another Arduino via Software Serial 4,5 

//data sent is a continues stream of ":strValSensor0,strValSensor1;strValSensor2,strValSensor3;"

    mySerial.readStringUntil(':');//is string start character 
    Serial.read();//skip start character 
    String strValSensor0 = mySerial.readStringUntil(',');//read first value 
    Serial.read();//skip separator 
    String strValSensor1 = mySerial.readStringUntil(',');//read second value 
    Serial.read();//skip separator 
    String strValSensor2 = mySerial.readStringUntil(',');//read 3th value 
    Serial.read();//skip separator 
    String strValSensor3 = mySerial.readStringUntil(';');//Read 4th value 
    Serial.read();//skip string stop character

```

Why are you mixing reads from the (useless) SoftwareSerial instance and the hardware serial instance? Which ONE is receiving the data you are trying to read?

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 26, 2019, 3:08pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/11 "2019-02-26T15:08:32Z")

</div>

Hello Paul Sir,

Eventually I intend to run the receive code on a Uno or a Nano, hence I am avoiding the Hardware Ports.

_Why are you mixing reads from the (useless) SoftwareSerial instance and the hardware serial instance? Which ONE is receiving the data you are trying to read?_

The Tx of the Sender(Uno) is connected to the Rx (Software Serial of the Mega)

Is this my mistake? I say mySerial.readStringUntil and I am trying to Serial.read? Should it be **my** Serial?

mySerial.readStringUntil(':');//is string start character  
**my** Serial.read();//skip start character

Apprecaite your help.

Thanks,  
Sachin

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 26, 2019, 3:14pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/12 "2019-02-26T15:14:21Z")

</div>

> The Tx of the Sender(Uno) is connected to the Rx (Software Serial of the Mega)

There are pins on the Mega that can be used for SoftwareSerial, and pins that can't. You MUST learn which pins are which.

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 26, 2019, 3:26pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/13 "2019-02-26T15:26:22Z")

</div>

Paul Sir,

Sorry for my ignorance. My apologies

> **[SoftwareSerial Library | Arduino Documentation](https://docs.arduino.cc/learn/built-in-libraries/software-serial)**
>
> The SoftwareSerial library allows serial communication on other digital pins of an Arduino board.

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for  
RX: 10, **11, 12,** 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

I was using **4,5** which is not listed. So can I use **11,12**

Otherwise do you feel the code is fine? Sorry for my stupid questions.

Regards,  
Sachin

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 26, 2019, 3:37pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/14 "2019-02-26T15:37:30Z")

</div>

> I was using 4,5 which is not listed. So can I use 11,12

Is there any reason why you are using SoftSerial and not Serial1, Serial2 or Serial3 on the Mega ?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 26, 2019, 3:42pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/15 "2019-02-26T15:42:42Z")

</div>

> So can I use 11,12

You have my permission to.

> Otherwise do you feel the code is fine?

How I feel is irrelevant. If the code works, it's fine. If it doesn't, it isn't.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [February 26, 2019, 4:25pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/16 "2019-02-26T16:25:57Z")

</div>

@OP

You have given the MEGA-Receiver Codes; you have not given the corresponding UNO-Sender Codes. We need codes for both Arduinos to set your given MEGA sketch. Please, post your UNO-Sender Codes.

**BTW:** I have tested the MEGA over SUART(11, 12) Port, and it works well with UNO SUART(4, 5) Port.

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 26, 2019, 5:01pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/17 "2019-02-26T17:01:00Z")

</div>

@UKHeliBob  
_Is there any reason why you are using SoftSerial and not Serial1, Serial2 or Serial3 on the Mega ?_

Sir, I read at many places that Software Serial is easier and less complicated.

@PaulS  
Sir, I connected 11,12 Rx, Tx. I was happy to see the data flowing. Atleast something started working although the  
data is meaningless. Looks like something is wrong with the code.

Please see tha attached image.

@GolamMostafa  
Sir, I have attached both the .ino files

Thank you all from the bottom of my heart for your time and help.

Regards,  
Sachin

\*\*\*\* Forgot to rename:  
Serial\_Succesful.ino is Tx (Uno)  
Serial\_Receive.ino is Rx (Mega)

[Serial\_Receive.ino](https://forum.arduino.cc/uploads/short-url/nGI95T7isEvWOalWDJL1mINPkXf.ino) (1.68 KB)

[Serial\_Succesful.ino](https://forum.arduino.cc/uploads/short-url/e6vGP5xd83bVU2ddDLcLTkbPvsN.ino) (2.02 KB)

 ![2.jpg](https://europe1.discourse-cdn.com/arduino/original/3X/b/5/b51f4f2eb5f6ebe03fa2de7e28a98e56a28d96ae.jpg)

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [February 26, 2019, 5:52pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/18 "2019-02-26T17:52:22Z")

</div>

@OP

The following screenshots indicate that there are some problems with you codes. Fix them and post the screenshots of the corrected program along with the programs.  
 ![sm49.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/0/8/d083f6202e9821198bd6f0b072e5bd0ab1fce1c6.png)

![sm50.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/1/1/91124bdc657be8b66d24203e7a237a9afecdff7c.png)

![sm49.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/0/8/d083f6202e9821198bd6f0b072e5bd0ab1fce1c6.png)

![sm50.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/1/1/91124bdc657be8b66d24203e7a237a9afecdff7c.png)

---

<div class="post-metadata">

**Author:** ![Salalit](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Salalit](https://forum.arduino.cc/u/Salalit)\
**Post date:** [February 26, 2019, 6:11pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/19 "2019-02-26T18:11:11Z")

</div>

@GolamMostafa

Sir, Initially I was getting errors for TX (Uno), but figured out my mistake. For (Rx) Mega I tried my best and did everything possible but I couldn't find my mistakes for the Receving Code. I am new to Arduino and don't know where I am going wrong.

Regards,  
Sachin

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 26, 2019, 6:33pm UTC](https://forum.arduino.cc/t/sending-data-via-software-serial/575998/20 "2019-02-26T18:33:29Z")

</div>

> Is there any reason why you are using SoftSerial and not Serial1, Serial2 or Serial3 on the Mega ?
> 
> Sir, I read at many places that Software Serial is easier and less complicated.

That surprises me.

Can you please provide some examples of where you saw this ?

Using the hardware serial interfaces involves no more than connecting to the correct pins (they are marked on the board) and using the .begin(baudRate) function as you would for the normal Serial interface. You do not need to #include a library and the hardware interfaces can run reliably at higher speeds than SoftwareSerial

[Next page](https://forum.arduino.cc/t/sending-data-via-software-serial/575998.md?page=2)
