NodeMCU and MEGA communication problem

I'm doing serial communication with nodeMCU and the Mega board. I have connected the 19th Rx pin to D5 and the 18th Tx pin to the voltage divider (to get 3.3v) and then D4. I have uploaded these below codes to the microcontrollers and always get "nope" in both serial monitors. it means that the serial. available isn't working. please help me to fix this issue.
Arduino Mega 2560

#include <SoftwareSerial.h>
SoftwareSerial s(19,18);
 
void setup() {
s.begin(9600);
Serial.begin(9600);
}
 
void loop() {
 
int data=50;
if(s.available()>0)
{ delay(3); 
 s.write(data);
  Serial.println("vfdvf");
}else {Serial.println("nope");}
}

nodemcu

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

  }
  else {Serial.println("nope");}
}

The Mega has 3 extra hardware serial ports, why use SoftwareSerial?

You did not read the reference for SoftwareSerial or you would have seen:

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).
Pins 18 and 19 are not on that list. Those pins are, however, the RX and TX for the Serial1 hardware serial port.

Connect the ESP TX to Mega pin 19 and ESP RX to Mega pin 18. Change SoftwareSerial to Serial1.

1 Like

The code that I modified will not compile so I needed to delete it. Sorry.

Just use Serial1 instead of SoftwareSerial.

Connect the ESP TX to Mega pin 19 and ESP RX to Mega pin 18 through a level shifter. Change SoftwareSerial to Serial1.

1 Like

This Mega code compiles and uses Serial1 port instead of SoftwareSerial.


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

void loop()
{

   int data = 50;
   if (Serial1.available() > 0)
   {
      delay(3);
      Serial1.write(data);
      Serial.println("vfdvf");
   }
   else
   {
      Serial.println("nope");
   }
}
1 Like

I have done as u said, sir. now mega code is working. but the node MCU has always "nope". I think this below line needs to be changed.

SoftwareSerial s(5,4);

can u give me a hint to change that sir? thank you very much for ur valuable time.

sir, is there any articles or tutorials that compare differences between softwareSerial vs hardwareSerial ports using coding examples. i'm a newbie user and sorry for the trouble caused. thank you very much.

Look here.

1 Like
SoftwareSerial s(5,4);

That refers to GPIO pin 4 & 5 if you want to refer to the pins D4 & D5 you should use

SoftwareSerial s(D5,D4);

(eg GPIO 4 == D2 & GPIO5 == D1 )

1 Like

Connect the ESP TX (pin 4) to Mega pin 19 (RX) and ESP RX (pin 5) to Mega pin 18 (TX) through a level shifter to drop the Mega TX 5V to 3.3V for the ESP RX

thank you sir, didn't knew about that

pin 4 means the D4 and pin 5 means the D5 right.

Refer to the pinout provided by @ Deva_Rishi, please.

1 Like

Yes use a voltage divider always on a nodeMCU, except when connecting to the pin marked 'RX' which usually actually is 5v tolerant, and may not respond when it is provided with a 3.3v logic level. Mind you i would try it first with a level shifter, but if that fails, go without. Not really relevant to this case, but info to keep handy for the future.

What is written on the board as D4 & D5 should be referred to as such in the code. The GPIO pin numbers can be referenced by just the number. If you leave the wiring as it is (or was) you can either write

SoftwareSerial s(D5,D4);

or

SoftwareSerial s(14,2);
1 Like

I have used this.

SoftwareSerial s(D1,D2);

so it means

SoftwareSerial s(5,4);

right. now Arduino is available but nodemcu isn't available. it shows nope in the monitor

thank you very much sir

UART Communication between NodeMCU (Sender) and MEGA (Receiver)
1. Connect NodeMCU and MEGA as per following diagram of Fig-1.


Figure-1:

(1) NodeMCU has only one 'Hardware UART (simply UART)" Port (RX/TX Pins) which is usually engaged with Serial Monitor and IDE for debugging and sketch uploading.

(2) A Software UART Port (SUART Port) is automatically created when the following lines/codes are included in a sketch at the appropriate places.

#include<SoftwareSerial.h>   //contains ready-made rotines and meanings of symbols
SoftwareSerial UART(D2, D1);  //SRX = D2 = GPIO-4; STX = D1 = GPIO-5
UART.begin(9600);  //UART works well at Bd = 9600

2. Upload the following sketch in NodeMCU (your one with slight modification). (Tested)

#include <SoftwareSerial.h>
SoftwareSerial SUART(D2, D1); //SRX = D2 = GPIO-4; STX = D1 = GPIO-5

void setup() 
{
     Serial.begin(9600);
     SUART.begin(9600);
}
 
void loop() 
{
    while(SUART.available()>0) //show message received from MEGA via SUART Port
    {
         char x = SUART.read();     //charcater has arrived; get it out from Serial Buffer
         Serial.print(x);     //Show it on SM1 (Serial Monitor 1)
    }
    SUART.println("Hello! I am NodeMCU!");   //send Hello message to MEGA 
    delay(1000);  //test interval
}

3. Upload the following sketch in MEGA (your one with slight modification). (Tested)

void setup() 
{
     Serial.begin(9600);
     Serial1.begin(9600);
}
 
void loop() 
{
    while(Serial1.available()>0) //show message received from NodeMCU via Serial1 Port
    {
         char x = Serial1.read();     //charcater has arrived; get it out from Serial Buffer
         Serial.print(x);     //Show it on SM1 (Serial Monitor 1)
    }
    Serial1.println("Hello! I am MEGA!");   //send Hello message to MEGA 
    delay(1000);  //test interval
}

4. Check that NodeMCU's SM1 shows the following message:

Hello! I am MEGA!
Hello! I am MEGA!
Hello! I am MEGA!
Hello! I am MEGA!

5. Check that MEGA's SM2 shows the following message:

Hello! I am NodeMCU!
Hello! I am NodeMCU!
Hello! I am NodeMCU!
Hello! I am NodeMCU!
1 Like

it isn't working bro. i don't know what I'm doing wrong

No idea why. One of the suggestions is always to disconnect all, and reconnect all step by step. Verify the connections with a multimeter.
I noticed in your Mega-sketch that you never do

s.read():

So there will always be something in the input buffer. (in case of overflow the character that is received next is just simply not copied to the buffer, check the core..)
@GolamMostafa Love the sketch and explanation. I was considering trying the alternate UART pins (15 & 13) and use .swap(), but if the OP can't get this to work, i don't see a point to that.

Neither do i. You could try and confirm that the mega is actually sending something, by taking MEGA TX1 and connect it to RX2 and see if something comes in on that port. The explanation that was provided is correct and should work. If it doesn't there must be a hardware issue somewhere.

2 Likes

finally, it began to work bro. ur code and my hardware were fine. I need ur help to send 8 variables data from nodemcu to Arduino. these 8 variables in nodemcu were connected to the firebase and always changing. so I need to send those variable data to mega 8 variables and need to update it always. is there any tutorial or sample code there to help me with this? thank you very much for ur support.

now all fine bro. both received the message. now I'm trying to send variable data from nodemcu to Arduino as previous message showed. sample code or a tutorial will much helpful for a newbie like me.