How do I do voltage divide for ESP8266 and MEGA2560

Hello! I am still really new to arduino, and I have got a project that requires the MEGA2560 and ESP8266 to both send and receive data, my MEGA2560 can receive data from ESP8266, but my ESP8266 does not receive data from the MEGA2560.
From what I have researched, the MEGA2560 outputs 5V and ESP8266 can only handle up to 3.3V, and the way to solve this is to do voltage divide or use a logic level converter, I am currently trying out the voltage divide method as I have never done soldering to setup the logic level converter and I'm scared to mess it up.
I have read some threads in this forum and some people had the same problem and they fixed it by doing the voltage divide method.

Here is my wiring


the purple cable is from the TX of MEGA2560 (pin 21 on breadboard)
the white cable (which is plugged in to the end of - on breadboard) is from GND on MEGA2560
the resistors are 1k ohm and 2k ohm

Here is my MEGA2560 code:
#include <ArduinoJson.h>

void setup() {

Serial.begin(9600);
//Serial1.begin(115200);
Serial2.begin(9600);
}
String fromEsp = "";
void loop() {

StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["From_mega"] = "Hi from mega";
root.prettyPrintTo(Serial);

Serial.println();
JsonObject& mega = jsonBuffer.parseObject(Serial2);
fromEsp = mega["From_esp"].as ();
Serial.println("From esp:" + fromEsp) ;

}

Here is my ESP8266 code:
#include <ArduinoJson.h>

void setup() {

Serial.begin(9600);
// Serial2.begin(115200);
Serial1.begin(9600);
}

String fromMega = "";

void loop() {

  StaticJsonBuffer<1000> jsonBuffer;
  JsonObject& esp = jsonBuffer.createObject();
  esp["From_esp"] = "Hi from esp";
  esp.prettyPrintTo(Serial);
  
  Serial.println();
  JsonObject& root = jsonBuffer.parseObject(Serial);
  fromMega = root["From_mega"].as<String>();
  Serial.println("From mega:" + fromMega) ;

}

I am using json because I need to send multiple types of data and put them in variables.
Please let me know if there is anything that you need, I need your help!.
Also, the esp8266 has been operating without resistors for at least 6 months, could the rx pins have fried already?

If the 8266 is transmitting at 3.3v the mega will be able to read that , even tho it’s 5v .
Just be careful not to set that Rx pin as an output !
As the mega is not going to transmit , don’t connect it Tx pin to the 8266 Rx pin and all will be fine

Interesting question is - the ESP is a far more powerful processor than the MEGA 2560, so why would you actually want to use a MEGA 2560?

Do it the proper way, especially if you build more then one, use a level translator. The resistor solution puts you on the edge of sometimes maybe it works. Remember parts have tolerances and power supplies do as well.

My project involves rfid scanner and line follower scanner that are connected to the MEGA2560, the objective is to be able to send the scanned rfid tag data to firebase via ESP8266

So why not just connect them to the ESP and drop the unnecessary Mega? :roll_eyes:

You may have already destroyed the ESP input pin.

my lecturer wants me to continue the development of smart trolley which involves motor drivers too, the esp doesn't have enough pins without the mega :sweat_smile:

For the record - logic level converters exist in DIP packages which you can just plug into a breadboard, or on pre-soldered breakouts.

How many do you need?

I can only see that there are lots plugged into the mega, im currently getting a new esp and gonna try with the level converter once they arrive

That's where you use a (one or more; you can easily use four) "port expander".

Not only is a Mega 2560 a very expensive version of a port expander, it is also a very clumsy one!

"see that there are lots"? This is someone else's project? Also, lots of what? What are those pins driving?

This is a project that has been worked on by previous team before me and I got the robot built already when i got the task, and the task that I got from my lecturer is to make it so that the robot can be controlled via firebase and can send its whereabout to firebase.

As for the used pin on the mega, it is connected to rfid scanner, line follower sensor and 4 dc motor that are used to operate 4 mechanum wheels.

I guess that is a reason to continue with the Mega, did you solve the serial level translation problem?

P.S. If you are in robotics you will need to learn to solder.

Still waiting for the items to arrive at the moment, and I will try to solder it.

I managed to make the esp8266 to receive data from Mega2560 by using the logic level converter, and the wiring is really simple using the level converter.

Now the esp can receive and send data from and to Mega2560, vice versa :smiley:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.