Serial communication between Arduino Mega 2560 & NodeMCU

I'm a newbie coder who trying some programming to improve my skills and do projects. I was trying to send data from the Arduino Mega board to the NodeMcu and found issues in the program. the sent data value(from Arduino) was 50 and the received and printed data(to nodemcu) is not the required value. the output result is varying from 150 to 300 values. I don't know what is the problem and need some help from experts. thank you.
pins were connected as follows
D6 ---------------------- Tx0 (1)
D5 ---------------------- Rx0(0)

Arduino code

#include <SoftwareSerial.h>
SoftwareSerial s(0,1);
 
void setup() {
s.begin(9600);
}
 
void loop() {
int data=50;
if(s.available()>0)
{
 s.write(data);
 delay(500);
}
}

NodeMCU code

#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);
int data;
void setup() {
s.begin(9600);
Serial.begin(9600);
}
 
void loop() {
  s.write("s");
  if (s.available()>0)
  {
    data=s.read();
    Serial.println(data);
    delay(500);
  }
 
}

Why in the world would you use a software serial port on the hardware serial pins (even if it would work on pin 0 on a Mega)?

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

Why in the would would you use a software serial port when you have 3 extra hardware serial ports available? See here.

No ground? There must be a ground.

ok sir, I will refer to the article. thank you very much for helping me

A nodemcu board is 3.3V only. You shouldn't directly connect the 5V MEGA2560 to the 3.3V nodemcu. Look for information on resistor voltage divider to reduce the voltage from MEGA2560 TX to a proper value for nodemcu.

1 Like

ok sir, thanks, and I will.