Mega 2560 R3 and ESP-01

Hi everyone, I’m trying to connect an ESP-01 module (https://www.canadarobotix.com/products/1788) to an Arduino Mega 2560 R3, but am having some difficulties. I am powering the ESP module with 3.3V via a bench power supply. I’m using a logic level converter (https://www.canadarobotix.com/products/1066) to connect TX on the Mega to RX on the ESP module.

Here is an image of my set-up:

Connections:

  • Bench power supply is connected to right power rails

    • ESP module’s VCC pin connected to 3.3V (positive power rail)

    • ESP module’s CH_PD pin connected to 3.3V (positive power rail)

    • ESP module’s GND pin connected to negative power rail

  • ESP module’s TX pin connected to Mega’s RX pin

  • ESP module’s RX pin connected to logic level converter LV1

  • Mega’s TX pin connected to logic level converter HV1

  • Logic level converter LV connected to positive 3.3V power rail, GND connected to negative power rail

  • Mega’s 5V pin and GND pin connected to left power rails

  • Logic level converter HV connected to positive 5V power rail, GND connected to negative power rail

When I turn the bench power supply on, the red LED on the ESP module lights up. To my understanding this is the power LED? The blue LED occasionally comes on but I don’t really understand what it means and why it only comes on occasionally. I was following this tutorial (https://projecthub.arduino.cc/hibit/wi-fi-integration-with-esp8266-module-2a0706) and uploaded the code from this tutorial to the Mega. However, when I set Both NL & CR and a baud rate of 115200 in the serial monitor and send “AT”, I don’t receive anything back.

I’m not sure if I have connected something wrong or if I should be uploading different code or something. Any help would be much appreciated! I have never used an ESP module before and only started using Arduino boards this year, so I feel a bit lost. Thank you!

A picture of your hand drawn wiring diagram would be much better than the word salad and photographs. Draw the actual connected wires, not from the instructions.

Noted, does this help?

It helps a lot. Do you think the green wire is ok, it is connected at one end to a 5V pin and at the other a 3.3V pin. Should the green wire look more like the yellow, I think they are RX and TX.

I thought that it was ok, but not 100% sure. It’s connecting the TX pin of the ESP-01 module to the RX pin of the Mega (I’m using Software Serial), so my understanding was that I don’t need to convert the signal?

If this schematic is correct it will not work as pins 2 and 3 are PWM pins.

https://detailspin.com/wp-content/images/get-hzhy-Arduino-Mega-2650-Pinout.jpg

The Arduino has 4 serial ports pined out as follows:

Arduino Mega supports four hardware Serial Communication interfaces. Digital IO pins 0 and 1 are used as Serial RX0 and TX0 pins to receive and transmit serial data. These pins are connected to the serial pins of the on-board USB to Serial Converter IC.

Similarly. Digital IO pins 19 and 18 as RX1 and TX1, 17 and 16 as RX2 and TX2 and 15 and 14 as RX3 and TX3 respectively.

Digital IO Pins 20 and 21 can be configured as SDA (20) and SCL (21) to support I2C or I2C or Two Wire Interface (TWI) communication.

The final communication interface is the SPI. Digital IO Pins 50, 51 52 and 53 can be configured as SPI pins MISO, MOSI, SCK and SS respectively.

After you correct it generate an annotated schematic, that will make this type of problem more visible.

Why when you have a choice of 4 pairs of real serial pins? (well 3 anyway)

BTW, SoftwareSerial won't work at 115200. It should work OK at 38400. One might get away with 57600 but not 115200.

I need the other pairs of serial pins for some sensors that I’m using. I’m just not showing them because I’m trying to get the ESP module working. Is there a different set of pins I can use?

Why would it be ok for pin 2 when pin 3 goes through the level converter? If you are happy to apply 5V to your 3.3V board, then go for it.

There should not be any 5V applied from the Mega pin 2 INPUT to the Tx OUTPUT of the ESP-01.

Pin 2 will not work for receive on a Mega.
Note the following from https://docs.arduino.cc/learn/built-in-libraries/software-serial/

I don't think that article is telling you enough. It assumes that, when you configure ESP, you will use the adapter they tell you to use, but you aren't. I believe the wiring therein is Rx to Rx and Tx to Tx, while your wiring is Rx to Tx etc., i.e. as you should when using ESP for communications. Further, ESPs are programmed at 9600, not 115200. This article is very specific about pin connections and baud rate, and it is not the only one i have seen like this. Also note that it runs ESP off Uno's 3.3v, also common, but I would have doubts about that.....
Your green Mega Rx cable is correct as far as voltage is concerned, but both need to be on pins kosher for serial. A 1k/2k divider on the Mega Tx is all that is required. Arduino can read ESP's 3.3v signal just fine, and post #9 is unnecessarily alarmist.
My 2c...

you may be better off investing in a Mega-WiFi_R3_ATmega2560_ESP8266 which has the Mega and ESP8266 on a single module with associated level converter etc
the ESP8266 is connected to the Mega Serial3 hardware serial port

or take an alternative approach, dump the Mega and switch to an ESP32 which has onboard WiFi, Bluetooth Classic and BLE

Not all of them support Bluetooth Classic. The S3 for example does not - as I found out when trying to configure it. There is supposidly a BLE serial that can be used in BLE mode (I have never tried it).

only the original ESP32 - S2 None the S3 C3 C6 etc just BLE
I mainly use the original ESP32 unless there is some functionality not supported, e.g. RMT synchronization is not supported on the original ESP32 so I use ESP32S3

File>Examples>BLE>UART will communicate with Android Serial Bluetooth Terminal app in Device>Bluetooth LE mode

As someone already mentioned swSerial is not reliable for reception at 115200. I don't know about the pins that are supported on the Mega, but there are 4 hwSerial ports available, and you should use one if them if possible.

If you do use swSerial and have found supported pins, you could send the command for changing the baudrate to 9600 to the ESP-01 first, then switch to 9600bps and take it from there.

Best practice is to first verify the ESP-01 is working as it is supposed to using just a simple USB to TTL or Serial passthrough on the Mega (at 115200bps) to make sure the level shifting is working the way you intend it to.

To use AT-commands issued by the Mega to control the ESP-01 is not a very efficient way to work. The ESP8266 can be programmed directly and has a much more powerful processor then the Mega does.

If it's a genuine Mega, it's 3.3v pin will usually suffice (test it, the less components the better)

That is good. Usually a simple voltage divider will also suffice but use 3x 1K resistors (not 10K) like Tx -> 1k -> Rx ->1K -> 1k -> GND

That was written for use with an UNO, hence the use of swSerial and pins 2 & 3, but there is no way you would get a reliable answer back from the ESP-01. The adapter that is shown can work for direct communication but i have not managed to use it for uploading code to the ESP-01 directly.

Would you be able to explain more about how to do this? I have a USB to TTL adapter that I purchased from Amazon but I’m unsure of how to use it.

To everyone else, thank you for letting me know that I can’t use Software Serial on pins 2 and 3, I did not realize that. I am going to try again with different pins. As well, for those suggesting I use a different baud rate, when I purchased this ESP module, the description states that I should use a baud rate of 115200 bauds and ArduinoDocs says that Software Serial supports 115200 bauds. That’s why I thought it would be ok but could someone clarify why it’s not reliable?

At 115200 bps swSerial is not reliable for reception, and you will receive incorrect characters at times mixed with the correct ones. The processor is simply not fast enough and an interrupt has to be triggered for every bit, and it may be late if the processor is in another ISR (or for something else temporarily disabling interrupts). The ESP-01 will run it's AT-command communication at 115200 bps by default,(there are firmware versions that use a lower baud-rate by default) but there is a command for switching the baud-rate. So if you send that command at 115200 first, and then switch the swSerial to that same slower baud-rate (9600 bps is usually the best) you can continue with that and it will be much more reliable also for reception.

It uses a CP2102 which i think is a 3.3v device, so no need for any level shifting.
I suggest you simply connect Tx to ESP-Rx and Rx to ESP-Tx, GNd to ESP-GND, 3.3v To ESP-Vcc & ESP-CH_PD
Plug in the USB, open the Serial monitor at 115200 on the port that shows up newly in the IDE and set line-ending to Both CR & NL and type AT and wait for a response. AT-instruction set can be found in chapter 3.

How fast is the baud rate for the sensors? If it is slow enough, then why not use the hardware port for the ESP-01 and SoftwareSerial for the sensor?

The ESP32 does have RMT the peripheral, what do you mean by synchronization ?

You should really provide more details on this. There may even be the option of some of the sensors sharing a port if they have an enable pin. Also there is still the UART0 which is connected to the USB to TTL converter (which normally speaking should be a 16u2) but which can also be used if not used for anything else.