Serial Communication between Two Arduino Boards

I have written two codes one code for the sender and another code for receiver. I am facing a problem because i am new to Arduino that's why i looking for your support. if you know what is wrong with my code please help me.
Thank You.

answer the 2 questions in #10 by looking up the documentation for int and write?

1. Let us apply your codes of Post-8 on UNO-1 of Post-6.
(1) A variable resistor designated as Pot is connected with A1-pin of UNO-1. The Pot can supply a DC voltage from 0V to 5V.

(2) The voltage of Step-1 is converted into 10-bit binary value by the internal ADC of the MCU. The range is:

For Pot value: 0V (min)      the ADC value : 000000 00 0000 0000 binary = 0x0000 hex = 0 decimal
For Pot value: 5V (max)      the ADC value : 000000 11 1111 1111 binary = 0x03FF hex = 1023 decimal
For Pot value: unknown (Vin) the ADC value : (1023/5)*Vin = 16-bit = 2-byte (always positive)

(3) The 2-byte value of the ADC can be stored in the AnalogInput2 variable (taken from your sketch) by executing the following code:

unsigned int AnalogInput2 = analogRead(A1);   

4. Let us show the unknown voltage (Vin) supplied by Pot on SM1 of UNO-1

When 1023 (0x03FFF) is the ADC value, then the input volatge (Vin) is 5V
when AnalogInput2 (=analogRead(A1)) is the ADC value, then the input voltage (Vin) = (5/1023)*AnalogInput2 = (5/1023)*analogRead(A1)

5. As the Pot is rotated, the input voltage (Vin) has both "ineger part and fractional part" called floating point number. Now, we can write the equality relation of Step-4 as:

float Vin = (5.0/1023.0)*AnalogInput2;; //float data type refers to floating point number
Serial.println(Vin, 2);  //2-digit after decimal point.

6. Upload the following final sketch into UNO-1.

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

void loop() 
{
  unsigned int AnalogInput2 = analogRead(A1);
  float Vin = (5.0/1023.0)*AnalogInput2;  //float is a data type 
  Serial.println(Vin, 2);  //this function sneds ASCII codes of 3-digit/symbol (x.xx) to SM1
  delay(1000);    //test interval
}

7. Turn Pot slowly. Check that SM1 shows changing values. If you have a DVM, measure the DC voltage at wiper-point and compare with the readings of SM1.

1.21
2.43
2.47
2.47
5.00
1.62
1.39
1.25
1.12
1.12
1.12
1.08
0.99
1.22

8. To to send the wiper-point voltage from UNO-1 to UNO-2, connect them using SUART Port as per Fig-1 of Post-6. And then ...!

9. Read Arduino Reference Manual and know the differences among the following codes:

Serial.print('A');
Serial.write(0x41);
Serial.write("ABCD");
Serial.print("ABCD");
Serial.print(2.743, 2);
1 Like

this code is for UNO 1 as a sender, But you have not written data in `Serial. write(AnalogInput1), and please guide me on how can i read that data in UNO 2 (Receiver).

Thank You so much for your support

@GolamMostafa is sending the data in ASCII with

  Serial.println(Vin, 2);  //this function sneds ASCII codes of 3-digit/symbol (x.xx) to SM1

if you apply the tutorial you have read (at least twice and practiced), you would know by now how to receive ASCII data.

1 Like

Have you run the sketch of Section-6 of Post-24? If yes, then take a photo of your UNO-1/UNO-2 setup and post here. We would like to see that you are also working.

Sir i have use Adafruit Metro Mini Board which is similar to the Arduino UNO board.
I have connected Board-1 UART TX pin0 to Board-2 UART RX pin1 and Board-1 UART RX pin1 to Board-2 UART TX pin0.

https://learn.adafruit.com/adafruit-metro-mini/pinouts

Please see the connection

I wish I could

1 Like

Please see the code and result. Now sir tell me how can I read this data in another Board-2

Why do users choose to ignore the advice on posting code ? Pictures of code are a waste of time

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

What about connecting the GNDs’?

Good job!

We are now going to send the wiper-point voltage from UNO-1 to SM2 of UNO-2.

1. Please, remove the above connection between the Hardware UART Ports of the UNOs as these ports are engaged with IDE/SM for sketch uploading/debugging.

2. Because, the UNOs have no other UART Ports, we will create 'Software Based UART Ports (SUART Ports)" to connect UNO-1 and UNO-2. Connect UNO-1 and UNO-2 as per Fig-1 of Post-6; where --

(1) DPin-5 of UNO-1 is the SRX (SUART Receive Pin) will be connectected with DPin-6 of UNO-2 (the STX-pin = SUART Transmit Pin of UNO-2).

(2) DPin-6 of UNO-1 is the STX (SUART Transmit Pin) will be connectected with DPin-5 of UNO-2 (the SRX-pin = SUART Receive Pin of UNO-2).

(3) GND-pin of UNO-1 with GND-pin of UNO-2.

3. A SUART port is automatically created when the following lines are included at the appropriate places of a sketch.

#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6); //SRX = DPin-5, STX = DPin-6
SUART.begin(9600);  //always operate SUART Port at such low speed

4. Upload the following sketch in UNO-1.

#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6); //SRX = DPin-5, STX = DPin-6

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);  //always operate SUART Port at such low speed
}

void loop()
{
  unsigned int AnalogInput2 = analogRead(A1);
  float Vin = (5.0 / 1023.0) * AnalogInput2; //float is a data type
  Serial.println(Vin, 2);  //this function sneds ASCII codes of 3-digit/symbol (x.xx) to SM1
  //------------------------------------------
  SUART.print(Vin, 2);   //3-digit and 1-symbol (point) are sent as ASCII codes to UNO-2
  SUART.println();          //send a Line Feed charcater to UNO-2 to bring the cursor down
  //------------------------------------------
  delay(1000);    //test interval
}

5. Upload the following sketch to UNO-2

#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6); //SRX = DPin-5, STX = DPin-6

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);  //always operate SUART Port at such low speed
}

void loop() 
{
  byte n = SUART.available(); //check that at lleast one charcater has arrived from UNO-1
  if(n !=0)
  {
    char x = SUART.read(); //a character has arrived/save from UNO-1; read it and put in x
    Serial.print(x);   //show the charcater on SM2
  }  
}

6. Turn Pot and check that SM1 and SM2 show the same readings.

1 Like

Yes, it works.
I highly appreciate your help so much. I am grateful to you. I value the help you've given me.

I have another query:
I want to read six analog channels (A0-A5) of UNO1 by UNO2.
So, what changes will have to be done in the code.

Please Help us.

Thank You.

Follow advice of @J-M-L's Post-26.

Hello sir,
I have read J-M-L reply and try to resolve my problem for reading data through 6 analog channel but failed. Please sir edit your code for 6 analog channel.
Thank you so much for your help

So besides doing your homework, do you need us to do anything else for you? May be take the exam for you?… :cold_face: :innocent:

Come on, post your attempt, show that you have worked hard on this, understood the concepts and tried to get something working. We won’t do it for you.

@J-M-L has advised you in Post-26 to read and prectice the tutorials. Have you done it and understood the sequence of the placement of the code lines?

1. With the sketch of UNO-1 of Post-34, add one more channel as float Vin2. Show the value on SM1 and send it to UNO-2 after sending Vin. Now, you are sending 8 ASCII codes (Vin = x.xx and Vin2 = y.yy) to UNO-2.

2. Add codes with the sketch of UNO-2 of Post-34 so that you receive 8 bytes/charcaters from UNO-1 via SUART Port and only then show them on SM2.

3. Post your above codes (Step-1, 2) and show us that you are trying to do something following our advices.

1 Like

Thank You sir for your support. Now I am able to read 4 analog inputs (0-5v). Sir, i want to segregate each analog input. This means if A0 is 5 volt I can have high digital output HIGHotherwise it LOW. currently, all-analog data coming in one line(like-5.005.005.005.00) so how can i segregate this data according to analog input so according to i generate 4 digital output.
UNO-1 Code

#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6); //SRX = DPin-5, STX = DPin-6

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);  //always operate SUART Port at such low speed
}

void loop()
{
  unsigned int AnalogInput1 = analogRead(A0);
  unsigned int AnalogInput2 = analogRead(A1);
  unsigned int AnalogInput3 = analogRead(A2);
  unsigned int AnalogInput4 = analogRead(A3);
  float Vin = (5.0 / 1023.0) * AnalogInput1; //float is a data type
  float Vin1 = (5.0 / 1023.0) * AnalogInput2;
  float Vin2 = (5.0 / 1023.0) * AnalogInput3; //float is a data type
  float Vin3 = (5.0 / 1023.0) * AnalogInput4;
  Serial.println(Vin, 2);  //this function sneds ASCII codes of 3-digit/symbol (x.xx) to SM1
  Serial.println(Vin1, 2);
  Serial.println(Vin2, 2);
  Serial.println(Vin3, 2);
  //------------------------------------------
  SUART.print(Vin, 2);   //3-digit and 1-symbol (point) are sent as ASCII codes to UNO-2
  SUART.print(Vin1, 2);
  SUART.print(Vin2, 2);   //3-digit and 1-symbol (point) are sent as ASCII codes to UNO-2
  SUART.print(Vin3, 2);
  SUART.println();          //send a Line Feed charcater to UNO-2 to bring the cursor down

  //------------------------------------------
  delay(1000);    //test interval
}

UNO-2 Code

#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6); //SRX = DPin-5, STX = DPin-6

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);  //always operate SUART Port at such low speed
}

void loop() 
{
  byte n = SUART.available(); //check that at lleast one charcater has arrived from UNO-1
  if(n !=0)
  {
    char a = SUART.read(); //a character has arrived/save from UNO-1; read it and put in x
    Serial.print(a);   //show the charcater on SM2 
  }  
}

Sir, I have read data in UNO-2 it coming like 5.005.005.005.00. I want to split all data and convert it into integer data so I can compare this data in real-time and according to this data I can give digital output.