nodeMCU Serial communication Serial.swap() explained

I have been helpen someone on another thread and ran into an issue which resulted in me using the nodeMCU's Serial.swap() function.
We were trying to accomplish full-duplex serial communication between an UNO and a nodeMCU, when it turned out that the moment you connect the nodeMCU through a resistor, it does not receive anymore, somehow there is an element which pulls the pin high, and a 1K resistor is not small enough to pull it low.
Since i always use a voltage divider to connect a 5v tx (UNO) to a 3.3v rx (nodeMCU) i ran into a bit of a problem. Anyway, the nodeMCU has a Serial.swap() function that swaps the normal TX/RX pair for an alternate pair being GPIO15/GPIO13 or D8/D7 as they are marked on the board.
It took a little while to get this going but i managed. And since my google efforts did not provide me with easy answers and i don't want people who search for this to have to look on page 4 of a thread that may not have a obvious title, here it is.
Don't connect anything until you have the sketches uploaded (i didn't succeed in uploading to the nodeMCU while the leads were connected)

  • Load 'Blink' onto the UNO
  • Open another instance of the IDE where you change the 'board' to nodeMCU and it's port.
  • Upload this sample sketch to the nodeMCU (i am assuming you have both connected through USB now)
void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Serial Echo, type % to swap()");
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    if (c == '%') {
      Serial.println("Swapping over");
      delay(10);
      Serial.swap();
      delay(10);
      Serial.println();
      Serial.println("Swapped to here");
    }
    else Serial.print(c);
  }
}

Now connect the leads as in the diagram for 'Testing through the SM'
nodeMCU Serial connections.JPG

  • Open both serial monitors from both IDE instances. And type something in the nodeMCU SerialMonitor-window and it will echo back what you write.
  • now type '%' and it will swap to the other pins. now you can type something in the UNO SerialMonitor and it will echo back there until you again type '%' to swap back.
    For trouble shooting do the usual, reset the boards, and or plu and unplug the USB and close and re-open the Serial-Monitor windows.

Are NodeMCU's GPIO pins not 5V tolerant? I have been always connecting UNO and NodeMCU directly without any problem. Am I performing a bad practice?

GolamMostafa:
Are NodeMCU's GPIO pins not 5V tolerant? I have been always connecting UNO and NodeMCU directly without any problem.

According to the documentation as far as i know they are not 5v tolerant, though while the pin is in input mode they seem to be. Biggest risk is reverse current while in output mode i think. I tend to prefer being on the safe side.

Deva_Rishi:
According to the documentation as far as i know they are not 5v tolerant, though while the pin is in input mode they seem to be. Biggest risk is reverse current while in output mode i think. I tend to prefer being on the safe side.

I appreciate your safety concern.

You can safely interface 5V and 3.3V peripherals with simple series resistors on the signal lines. The risk of direct connection is the difference in voltage can inject high current through the ESD diodes on the pins of the 3.3V device. Sufficient current will fry those diodes, feeding the higher voltage directly into the power busses within the chip, permanently damaging the internal circuitry. In most cases, those diodes can safely handle at least 1-2mA, so a resistor sufficient to ensure the current into the never exceeds 1mA will work without putting the chip at risk. The forward drop of the diodes is typically on the order of 0.6V, so the series resistor needs to drop ~1.1V@1mA. A 1.2K series resistor on the signal lines will do that safely.

Regards,
Ray L.

The forward drop of the diodes is typically on the order of 0.6V, so the series resistor needs to drop ~1.1V@1mA. A 1.2K series resistor on the signal lines will do that safely.

That is usually what i do (on ESP-01's) but somehow that makes the RX-pin not function properly, actually it will just stay high. Now later i found that putting a signal diode in series, kathode out, Grounding the pin out does pull it low and has 4mA running through it, and should protect it sufficiently from the 5v TX-pin of the UNO. Still the solution with the .swap() works just as well, since then a voltage divider can be used.

The other thing you need to be aware of is that D8 should be held low during boot otherwise it will try to boot from external flash. I have found that a ~2k2 resistor to ground works fine, other values might be OK too.

The other thing you need to be aware of is that D8 should be held low during boot otherwise it will try to boot from external flash. I have found that a ~2k2 resistor to ground works fine, other values might be OK too.

That is the TX-pin, i'm going from the pint of view that will only connect to a 'logical' input (either the UNO RX or the RX of the Serial-TTL. Isn't there an internal pull-down as well for GPIO15 ?

Deva_Rishi:
That is the TX-pin, i'm going from the pint of view that will only connect to a 'logical' input (either the UNO RX or the RX of the Serial-TTL. Isn't there an internal pull-down as well for GPIO15 ?

Like you, I had all sorts of trouble with an ESP8266 board until I worked out its quirks. I was about to give up and use something else when I discovered Serial.swap(), which solved most of my problems but I was still left with it randomly failing to boot up. I eventually discovered the need to hold GPIO15 low. Once I did that the problem of failing to boot stopped happening. I don't know if there's an internal pull down resistor, but I do know that adding one fixed that particular problem. Your aim in posting this was to help others that might encounter the same problems as you, I have highlighted another problem someone might have and the solution if they do.

++Karma; // For posting something very helpful to others who will read it.

I have highlighted another problem someone might have and the solution if they do.

Yeah thanx for that, my experiences are mainly with ESP-01's which don't present with these issues, though with some others.

io 15 doesn't have internal pull-down and must be pulled down at boot with external pull-down circuit

Deva_Rishi:
According to the documentation as far as i know they are not 5v tolerant, though while the pin is in input mode they seem to be. Biggest risk is reverse current while in output mode i think. I tend to prefer being on the safe side.

from here
It is no problem to connect the Arduino and the NodeMCU directly even if the operating voltage for the Arduino is 5V and for the NodeMCU is 3.3V because the NodeMCU digital pins are 5V tolerant and protected from over-voltage.