Troubles connecting ESP8266-01 with Arduino MEGA 2560

Hi,

I am struggling since quite some time now, using the ESP8266-01 Module for WiFi connection in my Arduino IDE Project, where I use the MEGA 2560.
Before I am really giving up on it, I thought maybe someone in this forum can guide me to the mistake I am apparently making.

I connected the ESP8266 module with my Arduino via the attached it the wiring layout (see attached image ESP8266_basic_bb.png), so essentially it is
ESP <-> MEGA
GND --- GND
TXD --- PIN 52
CH_PD --- V3.3
VCC --- V3.3
RXD -- PIN 51 via Voltage Reducer from 5v to 3.3v

'ESP8266_basic_bb.png'

My Arduino IDE code for trying the basics of the connection is this:

#include <SoftwareSerial.h>
const byte rxPin = 51; // Wire this to Tx Pin of ESP8266
const byte txPin = 52; // Wire this to Rx Pin of ESP8266
 
SoftwareSerial ESP8266 (rxPin, txPin); // RX, TX
 
void setup() {
  Serial.begin(9600);
  ESP8266.begin(9600); 
  Serial.println("Ready"); 
}

bool okReceived = false;
 
void loop() {
  if (Serial.available() > 0)  {
    String command = Serial.readStringUntil('\n');
    Serial.println("Command Sent: " + command);
    ESP8266.println(command);
  }
  int responseCounter = 0;
  if (ESP8266.available() > 0)  {
    while (ESP8266.available() > 0)    {
      if (responseCounter == 0)      {
        Serial.println("Response Received:");
      }
      String response = ESP8266.readStringUntil('\n');
      Serial.println(response);
      responseCounter++;
    }
    Serial.println("============");
  } 
}

My goal is (for now) to communicate via AT commands with the ESP8266 Module on the MEGA.
I've tested with the USB programming board, and that worked fine, just I cannot get it to work on the MEGA. I was also able to flash it with the most recent firmware through that USB programming Board. But although I tried many variants of PIN connections (e.g. Combinations of 18/19) I cannot get responses from the Module, when I issue AT commands via the Serial Monitor of the Arduino IDE.

Anyone can point me to the one (or many) thing(s), that I am doing wrong?
Something in the wiring must be wrong, but that is like a summary of what I found via google....

Thanks !
Jijaji

use hardware Serial1 on pins TX1,RX1. and the esp-01 will not burn if you don't use a voltage divider. for permanent connection you can later use a proper level shifter.
wire RX to TX
use the SerialPassthrough sketch from Communication examples at 115200 baud to communicate with AT firmware from Serial Monitor

Then I recommend my WiFiEspAT library

ESP8266_basic_bb.png

What is this "voltage reducer" that you are using?

What baudrate have you selected?

@Paul___B

That is MRC1708 (I draw wrong PIN connection and fixed that - it is supposed to be 5V IN from MEGA in the middle PIN and 3.3V OUT on the right PIN to ESP-01).

The baudrate is 9600, both Serials.

  1. One very common mistake is that people do not use a dedicated 3.3V power supply for the ESP-01 module. Using the Mega's 3.3V can be unreliable and inconsistent.

  2. No need to use SoftwareSerial since the Mega has 3 hardware serial ports in addition to the main one! The default baud rate of the ESP-01 AT firmware is 115200.

.

1 Like

Juraj:
use hardware Serial1 on pins TX1,RX1. and the esp-01 will not burn if you don't use a voltage divider. for permanent connection you can later use a proper level shifter.
wire RX to TX
use the SerialPassthrough sketch from Communication examples at 115200 baud to communicate with AT firmware from Serial Monitor

Then I recommend my WiFiEspAT library

Thanks for the reply!

I removed the voltage devider and connected RX from ESP to TX1 on MEGA PIN 18 and TX from ESP to RX1 on PIN 19.

Using the basic example, I cannot get any response from the ESP.

The ESP itself is working as I checked that with the ESP USB Board.

I seem to be missing some very basic level stuff.
Can anyone point me to possible things I could be doing wrong?

ieee488:

  1. One very common mistake is that people do not use a dedicated 3.3V power supply for the ESP-01 module. Using the Mega's 3.3V can be unreliable and inconsistent.

  2. No need to use SoftwareSerial since the Mega has 3 hardware serial ports in addition to the main one! The default baud rate of the ESP-01 AT firmware is 115200.

.

Thanks ieee488.
Maybe the use of MEGA 3.3V as source is the issue. I will check with other supplies.

did you try this part of my comment?

"use the SerialPassthrough sketch from Communication examples at 115200 baud to communicate with AT firmware from Serial Monitor"

hw Serial can run at 115200 baud so let the AT firmware at 115200 baud

Yes, I did.
No communication.

Here is a sketch for an Arduino Mega2560, with an ESP-01 connected to Serial 1.

I recently used this to add WiFi to a Mega. It uses the WiFiEsp library, so make sure your IDE has it available.

#include "WiFiEsp.h"

// ********************************************************* DEFINITIONS *********************************************************
char ssid[] = "********";             // your network SSID (name)
char pass[] = "********";             // your network password
int status = WL_IDLE_STATUS;          // the Wifi radio's status

// ********************************************************* INITIALIZE *********************************************************
void setup() {
  Serial.begin(115200);                                     // initialize serial 0 for debugging
  Serial1.begin(115200);                                    // initialize serial 1 for ESP module
  WiFi.init(&Serial1);                                      // initialize ESP module
  if (WiFi.status() == WL_NO_SHIELD) {                      // check for the presence of the shield
    Serial.println("WiFi shield not present");              // If shield not present, don't continue
    while (true);
  }
  while ( status != WL_CONNECTED) {                         // attempt to connect to WiFi network
    Serial.print("Attempting to connect to WPA SSID: ");    // Print message to serial monitor
    Serial.println(ssid);                                   // Print SSID to serial monitor
    status = WiFi.begin(ssid, pass);                        // Connect to WPA/WPA2 network
  }
  Serial.println("You're connected to the network");        // Print success message to serial monitor
}

// ********************************************************* MAIN LOOP *********************************************************
void loop()
{
  // check the network connection once every 10 seconds
  Serial.println();
  printCurrentNet();
  printWifiData();
  
  delay(10000);
}

// *********************************************************  FUNCTION DEFINITIONS *********************************************************
// Print WiFi data to serial Monitor  --------------------------------------------------------
void printWifiData()
{
  // *** print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // *** print your MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  Serial.print("MAC address: ");
  Serial.println(buf);
}

// Print WiFi connection details to serial Monitor  --------------------------------------------------------
void printCurrentNet()
{
  // *** print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // *** print the MAC address of the router you're attached to
  byte bssid[6];
  WiFi.BSSID(bssid);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
  Serial.print("BSSID: ");
  Serial.println(buf);

  // *** print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI): ");
  Serial.println(rssi);
}

Thanks, I will try this.

Can someone confirm, that the wiring is correct?

jijaji:
Thanks, I will try this.

Can someone confirm, that the wiring is correct?

what wiring?
is your esp-01 a new esp-01S or classic esp-01?

for esp-01S it is (Mega/esp-01)
3.3 V to Vcc
Gnd to Gnd
RX1 to TX
TX1 to RX
(gnd to io 0 for flashing)

classic esp-01 additionally requires
3.3 V pull-up on CH-EN
3.3 V pull-up on io 0 for normal mode (pull-down for flashing)

Minimal wiring is:

ESP TX -----To Mega RX (Serial 1)
ESP CH_PD - To 3.3v power supply's +
ESP RST --- Can leave open (although I tie it to the 3.3v power supply's + with a 10kOhm resistor)
ESP VCC --- To 3.3v power supply's +

ESP GND --- Connect it to both the Mega's GND, and also to the 3.3v power supply's GND
GPIO2 ----- Can leave open
GPIO0 ----- Can leave open (although I tie it to the 3.3v power supply's + with a 10kOhm resistor)
ESP RX -----To Mega TX (Serial 1)

I connect the RST and GPIO0 pins to the 3.3v power supply's + with the 10kOhm resistors, so those inputs aren't floating. Since one is the Reset, and the other is the Program pin.

For initial testing, I see you are using the Arduino Mega's 3.3v pin as the power supply. This really isn't a great idea, as the ESP8266 chip can pull a bit of power when transmitting. So while it may be ok during your learning phase of just trying to the two talking to each other, you really need a separate 3.3v power supply for the ESP chip.

FYI: The ESP8266 can pull up to 300mA, although somewhere around 60 - 200 is more likely.

But the Mega2560 has a limit of 50mA on the 3.3v pin.

Specs of the ESP-01:

Specs of the Mega:

And as more FYI, here is the wiring diagram I refer to when I'm integrating one into my projects. I setup a push button for the RST pin, and I also setup a slide switch for GPIO0 ... in case I want to program after it's already integrated.

ESP-01_Wiring.jpg

ESP-01_Wiring.jpg

Mega R3 has 150 mA on 3.3 V pin. esp8266 max is 180 mA at very high rf tx power

Juraj:
Mega R3 has 150 mA on 3.3 V pin. esp8266 max is 180 mA at very high rf tx power

Where do you see the 150mA spec?

The Arduino site states 50mA for Mega R3

https://store.arduino.cc/usa/mega-2560-r3

jijaji:
That is MRC1708 (I draw wrong PIN connection and fixed that - it is supposed to be 5V IN from MEGA in the middle PIN and 3.3V

I can find on Duck2Go no such thing as an MRC1708. I strongly suspect it does not exist. :roll_eyes:

Juraj:
the esp-01 will not burn if you don't use a voltage divider.

That is s very contentious assertion. :astonished:

The simplest level shifter is a diode with cathode to the 5 V device and anode to the ESP8266. You may or may not need a pull-up to 3.3 V on the ESP8266; Adafruit suggests you don't (but the diode should always be adjacent to the ESP8266).

Stoopalini:
Where do you see the 150mA spec?

The Arduino site states 50mA for Mega R3

https://store.arduino.cc/usa/mega-2560-r3

it is not updated. see spec of the 5 V to 3.3 V converter used on Mega R3

Thank you all for your support.

I am going to try with external power supply (TP 4056 with battery, charging right now) and come back.

  • powering externally with 18650 lithium battery
  • wired the ESP-01 (it's not ESP-01S) with the VCC and CH_PD on OUT +/- directly from TP 4056
  • using Hardware Serial TX1 / RX1 with baudrate 115200 and basic sketch SerialPassthrough
  • no voltage divider (used before: TIP 120 - MRC 1708)

... ESP still not responding.

I am going to reflash it, maybe there is an issue with the firmware.

Thanks for your posts.