Transfer stored integer by using software serial

I am new to Arduino. I want to send the stored integer value from nano to Mega. Due to some reason, i can't change the nano board and the software serial is the only option. Here is the code for transmitter board

#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);

void setup()  
{
  softSerial.begin(9600);
} 
void loop()  
{ 
int h = 500;
 softSerial.write(h);
  delay (100);
};

and the code for receiver board is:

#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);
char ip;
void setup()  
{
  //softSerial.begin(115200);
  Serial.begin(9600);
  Serial1.begin(9600);
} 
void loop()  
{ 
  if (Serial1.available())
  {
    ip=Serial1.read();
    //Serial1.print(ip);
    Serial.print(ip);
    
  }
}

The output at the receiver looks like

This writes one byte (binary). See the Serial Input Basics tutorial elsewhere in the forum for some better ideas.

You will be using softSerial.print() to format the data in human readable form.

Thank you, it will be better if you provide any link or something as i am new to this topic.

@kk1107 Introductory Tutorials is a section for posting tutorials for beginners, hence the NAME.
As you are not posting a tutorial for beginners I have moved your post here.

You may want to read this before you proceed:-
how to get the best out of this forum

The write function will write one byte. A byte can hold values from 0 to 255. 500 will not fit in a byte.

The serial input basics tutorial is a really good start for serial communication.

1. You have the following number to send from NANO to MEGA using soft UART (SUART) Port.
int h = 500;

2. Note that the value of a variable is always saved as binary in memory location. So, 500 has been saved as 0000000011110100. In hex-base, it is 01F4 which is written as a matter of convenience. Here, the upper byte is 01 and the lower byte is F4.

3. When you have executed softSerial.write(h), the NANO has sent 11110100 (the lower byte, F4) to MEGA. write() method always handles 8-bit (1-byte) data.

4. To receive/display the number (F4) of Step-3 by the MEGA, we have to execute the following sketch at the MEGA side:
(Check that D10-pin of NANO is connected with TX1-pin of MEGA; D11-pin of NANO is connected with RX1-pin of MEGA.). GND-pin of NANO is connected with GND-pin of MEGA.

//#include <SoftwareSerial.h> no need; you are using hardwae UART1 Port
//SoftwareSerial softSerial(10, 11);  no need; you are using hardware UART1 Port
byte ip;  //data are coming as binary bits and not in ASCII
void setup()  
{
  //softSerial.begin(115200);
  Serial.begin(9600);
  Serial1.begin(9600);
} 
void loop()  
{ 
  if (Serial1.available())
  {
    ip = Serial1.read();
    //Serial1.print(ip);
    Serial.println(ip, HEX);   //or Serial.println(ip, BIN); to see 11110100
  }
}

5. To send 500 from NANO and to receive/display by MEGA, we can execute the following sketches.

For NANO:

#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);

void setup()  
{
  softSerial.begin(9600);
} 
void loop()  
{ 
  int h = 500;    //01F4
  softSerial.write(lowByte(h));  //F4 is sent
  softSerial.write(highByte(h));  //01 is sent
  delay (1000);  //test interval
};

For MEGA:

//#include <SoftwareSerial.h>
//SoftwareSerial softSerial(10, 11);
byte ipL, ipH;
void setup()  
{
  //softSerial.begin(115200);
  Serial.begin(9600);
  Serial1.begin(9600);
} 
void loop()  
{ 
      byte n = Serial1.available();
      if (n == 2)    //2 bytes are accumulated in the seial BUFFer
      {
            ipL = Seial1.read();    //ipL holds F4
            ipH = Serial.read();    //ipH holds 01
            int h = ipH<<8 | ipL;  //ip holds 01F4
            Serial.println(ip, DEC);   //shows: 500
      }  
 }

Instead of taking the value F4 and 01 as ipL and ipH respectively, it takes only FF in both the case.

The issue is resolved.
This is the code for MEGA.
//#include <SoftwareSerial.h> //SoftwareSerial softSerial(10, 11); byte ipL, ipH; void setup() { //softSerial.begin(115200); Serial.begin(9600); Serial1.begin(9600); } void loop() { byte n = Serial1.available(); if (n == 2) //2 bytes are accumulated in the seial BUFFer { ipL = Serial1.read(); //ipL holds F4 ipH = Serial1.read(); //ipH holds 01 int h = ipH<<8 | ipL; //ip holds 01F4 Serial.println(h, DEC); //shows: 500 //Serial.println(ipH, DEC); } }

Thank you very much GolamMostafa. :smiley: :smiley:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.