Cannot Get Serial Communications From WEMOS D1 Mini to Arduino Mega 2560

Hello,
Recently i have been having a programming problem with my Arduino Mega 2560.
I have connected a Wemos D1 Mini to an Arduino Mega 2560 via the 2nd Serial port on the mega.

MEGA/WEMOS
TX (pin 16) / Logic Level Shift Converter / RX
RX (pin 17) /TX
5v/5v (It is stepped down on the Wemos)
Gnd/Gnd

Here is a sample code

void setup() {
  // put your setup code here, to run once:
Serial2.begin(115200);
Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
 if(Serial2.available()>0)    //Checks is there any data in buffer 
  {
    Serial.print("We got:");
    Serial.print(char(Serial2.read()));  //Read serial data byte and send back to serial monitor
  }
  else
  {
    Serial2.write("AT\r\n");
    delay(1000);                      // Wait for a second
  }

}

Attached is the way it is connected

When i have only the Arduino Mega plugged in, i get this through the serial monitor

Nothing.

When i only plug in the Wemos D1 Mini i get this:

WIFI CONNECTED
AT


OK
AT


OK
WIFI GOT IP
AT


OK
AT


OK
AT


OK
AT


OK
AT


OK
AT


OK
AT


OK
AT


OK
AT


OK
AT


OK

I am really stuck here, any help would be greatly appreciated!
Thank you soo much.
John

EDIT:
Sorry:
I should of said that i have the ESP8266 Flashed with AI thinker.
I wanted to have the Mega send data to the ESP8266, then the mega print what the ESP8266 responded with

And here is the picture.

I also tried to get the Serial data basics working, didnt have much luck.
This was the code i used

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(115200);
    Serial2.begin(115200);
    delay(1000);
    Serial.println("<Arduino is ready>");
    Serial2.write("AT\r\n");
}

void loop() {
    recvWithEndMarker();
    showNewData();
    delay(1000);
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial2.available() > 0 && newData == false) {
        rc = Serial2.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

Image from Original Post so we don't have to download it. See this Simple Image Guide

...R

I can't read the pin labels in your diagram. A photo of a simple pencil drawing would probably be better.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

No luck with the serial basics - read edit.

John_Sak:
No luck with the serial basics - read edit.

What edit?

I hope you did not make big changes to your Original Post?

...R

The part that says "EDIT:"
I just clarified what i was doing and tried to use the serial basics code.
Do you think there is a problem with the logic converter?
Thanks

John_Sak:
The part that says "EDIT:"
I just clarified what i was doing and tried to use the serial basics code.

For the future please add that sort of thing as a new Reply in the correct chronological order. It makes it so much easier to follow the conversation in the Thread.

Do you think there is a problem with the logic converter?

You did not respond to my request for a readable diagram of your connections.

And you have not said exactly happened when you tried my tutorial code?

...R

The example gives the following: (i dont plug in both at the same time)

(ARDUINO SERIAL MONITOR)

<arduino is ready>

No response to commands

(WEMOS SERIAL MONITOR)


(nothing)

Responds to AT Commands

Also here is a labeled diagram

John_Sak:
The example gives the following:

I presume you are referring to the "Example 2" code you added into your Original Post?

(i dont plug in both at the same time)

I don't understand that and I suspect it may be significant.

What are "both"?

...R

Well i have two seperate usb cables, one plugged into the wemos and the other in the arduino. I only plug in one at a time because i hear that if you plug in both it may damage your computer or the wemos/mega.
And yes, the example code was in the edit

There will be no problem if you connect the two boards with USB cables to the same PC. And it will help if you can see the output of both boards at the same time.

My ability to help is greatly limited because I don't have your hardware.

Are you sure you have the voltage converter connected properly? Post a link to its datasheet.

...R

This is the datasheet:
https://www.jaycar.com.au/medias/sys_master/images/9077185085470/XC4486-dataSheetMain.pdf
Thats what was on the website i got it from.
My module has the same components and layout but just a different colour, same model number though.
The datasheet seems very limited.
Thanks for the help.

Please draw (with a pencil or pen) a diagram showing how you have everything connected with clear labels on all the connections and post a photo of the drawing.

My suspicion is that you have wrong connections to the converter - but I can’t be certain with your Fritzing diagrams.

…R

Dont mind my awful paint schematics.
I forgot to include the regulator i had in the last drawings.
I tried to make sure everything had a common ground.
Also just changed Wemos to run off a 9600 baud rate.
Thanks for the help

I know I probably seem like a PITA, but you have not shown the labels on all the pins on the level converter - and that is what I really need to see now - so I can compare it with the online info about the device.

And what is the "step-down" thing that was not shown at all in your earlier diagrams.

Is the diagram in Reply #13 complete? Or are there other parts or other connecting wires that you have not shown?

...R

No, i just cant easily describe the pinouts.
I have added pictures to the same or similar devices to better show how its connected
Also as shown in the first post,
MEGA TX = 16
MEGA RX = 17
WEMOS TX = GPIO1
WEMOS RX = GPIO 3
I added the step down to deal with the current draw rather than having it directly plugged into the arduino.
Thanks.

Thank you for the better diagram. It would be nice if we could find a proper instruction document for the logic converter. As it is all I can do is guess - so I may be wrong.

I thought I had a sensible guess, but when I re-considered it did not seem sensible.

Sorry, I don't know how the thing works or whether you are using it correctly.

Why not replace it with a pair of resistors wired as a voltage divider - like this

   (5v Tx) ----- 3300ohm ----------- 5600ohm --------(GND)
                              |
                         (3.3v Rx)

...R

I have tried a voltage divider like this with no luck.
(may be slightly inaccurate from memory)

 (5v Tx) ----- 1000ohm ----------- 2000ohm --------(GND)
                                        |
                                  (3.3v Rx)

I currently do not have those resistors so i cant really try it.
I will look for a datasheet.
Thanks for your help!

I found a datasheet on the similar model:

For some reason, all of these models are discontinued. Maybe it dosent work?
I was considering making some kind of decoder/encoder to send signals through analog pins.
like this:
letter a -> 0.5v -> letter a
MEGA SIGNAL WEMOS

But i dont think it is very practical considering software serial.
Thanks

John_Sak:
I have tried a voltage divider like this with no luck.
(may be slightly inaccurate from memory)

 (5v Tx) ----- 1000ohm ----------- 2000ohm --------(GND)

|
                                 (3.3v Rx)




I currently do not have those resistors so i cant really try it.
I will look for a datasheet.
Thanks for your help!

Those resistor values should work fine.

If the system is not working with them then there is another problem.

Can you write programs for both the Arduino and the Wemos so that you can test one-way communication in both directions - maybe everything works in one direction but not the other?

...R