Sending Data via Software Serial

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.

*/

// 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

}

  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

int ValSensor0;

A variable of type int with global scope

Then in the loop() function

  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 ?

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,

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

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

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

@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
Figure-1: UNO and NANO connection using SUART Port

2. Upload the following sketch in UNO

#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

#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

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

sm47.png

@GolamMostafa

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

Warm Regards,
Sachin

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

Serial_Receive.ino (1.68 KB)

  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?

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?

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 mySerial?

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

Apprecaite your help.

Thanks,
Sachin

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.

Paul Sir,

Sorry for my ignorance. My apologies

https://www.arduino.cc/en/Reference/softwareSerial

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

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 ?

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.

@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.

@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 (1.68 KB)

Serial_Succesful.ino (2.02 KB)

@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

sm50.png

sm49.png

sm50.png

@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

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