Serial.begin on ESP8266 not working

I am trying to use the Arduino IDE to program an ESP8266 (ESP-12F). I am able to upload a simple program that blinks the built-in led. As soon as I put a reference to Serial.begin() anywhere in the code, my program no longer works. Even code that executes before the call to Serial.begin() fails to run.

I am using the following code:

const int ledPin = 2;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(2000);

// If this line is present, the led never turns on, not even the first time.
// Removing it makes the led blink, as expected.
Serial.begin(115200);
}


I am connecting the ESP as follows. It is probably not very relevant since I am able to upload and run code successfully as long as it does not attempt to use the serial port. I have also tried replacing the connection to RTS and DTR with push buttons to GND, and the result is the same.

What am I doing wrong ? How do I use the serial port on the ESP8266 ?

PS: I know that Serial.begin() should not go inside the loop(). I've only moved it there so that it happens after turning the led on.

Update

I tried to flash the latest firmware using the official tool, but this process always fails with an "Invalid head of packet" error. It seems to occur when writing the last packet, because if I flash a large file, it gives the error only at 99%.

After these attempts, I have been unable to make any program work. Even the simple led blinker without any Serial reference seems to do nothing, although the serial monitor shows the ESP boot messages.

• Put Serial.begin() in the setup() and try it. – Peter Smith Mar 19 '17 at 11:58
• I've already tried that. As I explained, I put it in loop() to make sure that it happened after turning the led on. – Antoine Aubry Mar 19 '17 at 12:52
• It is quite likely that the compiler is finding the instantiation of a Serial object and moving the initialisation to the start of the function. The Arduino IDE hides a lot of things. I would be tempted to import it into Atmel studio where I can look at register setting in the debug view. – Peter Smith Mar 19 '17 at 13:31
• Are you using Arduino IDE serial monitor? It may pull the RTS and DTR lines throwing your chip into reset. – Dejvid_no1 Mar 19 '17 at 14:09
• @ Peter Smith, there's absolutely no reason why the compiler would do that. It's neither optimising speed nor memory to do such a thing. – Octopus Mar 22 '17 at 5:00

There are two options left for you to try based on all the information contained on this thread.

1. Fix the connections as follows:

You need to connect a few GPIO pins on the ESP-12 to 3.3V or Ground, to set it in the right mode for communicating with it. You can add capacitors and resistors where you feel they may be needed, however the AI modules have those internally already. In addition, remember to remove this setup when resetting the board to run your firmware, otherwise it will be booted into "Programming/Flashing Mode" which can have undesirable effects. Please note that this diagram uses a LM1117 voltage regulator, if you have a 3.3v power supply you can just substitute that in the diagram. Here are the connections you need to make:

ESP-12 <--------> TTL-USB

TX ----> RX

RX ----> TX

GND ----> GND

REST ----> RST (only if you plan on using the serial console to reset the board.)

ESP-12 <--------> POWER SUPPLY

VCC ----> 3.3V Power supply (Vout of Voltage Regulator or Power Supply)

GND ----> Ground of power supply

CH_PD ----> HIGH (3.3V)

GPIO2 ----> HIGH (3.3V)

GPIO15 ----> LOW (GND)

GPIO0 ----> HIGH or Floating for AT Mode (3.3V) [ * if you want to flash completely different firmware then you must connect it to ground ] It appears you have this connected to HIGH which is why you may be having problems flashing Arduino Sketches.

2. Reflash the AT firmware using ESPTOOL

If you are still having problems after the reconfiguration of your connections, I highly recommend that you reflash the AT firmware from Espressif and start from scratch. If this doesn't fix your problem then I am afraid you have fried your board.

(For more detailed instructions see Instructables)

Use serial.begin before pinmode: https://cdn.arduino.cc/reference/en/language/functions/main/loop/ in setup.

• That might be the right answer, but the link doesn't support it. The example is coded that way, but doesn't say why or explain the order at all. – JRE Mar 22 '17 at 11:54
• I have already tried that and this did not help. – Antoine Aubry Mar 22 '17 at 22:15

I am tempted to think that your chip is defective.

When a program is compiled with the Arduino SDK it is really just like any other program written in C++ with a customized main() function:

int main(void)
{
init();
setup();

for (;;)
loop();

return 0;
}


It is completely irrelevant whether any particular block of code goes inside setup() or loop() apart from the fact that setup code runs once and loop code is run in an infinite loop.

It is also irrelevant whether you call pinMode() before or after Serial.begin() either ought to work. I have a sketch running currently that sets pinModes first then sets up the Serial communications. It is running on a NodeMCU though, so it is different hardware, but the particular hardware shouldn't matter.

• make sure you have selected the correct 'Board' under the 'Tools' menu in the Arduino SDK. This will affect how it compiles your code. I'm not sure which selection corresponds to your ESP-12F, you'll need to check the documentation.

• double check that your wiring is correct. try removing the wires from the Tx, Rx pins and see if your code runs while still including the call to Serial.begin(). This might indicate whether loads on those pins are causing issues.

• make sure that the LEDpin you are using is not also wired to the Tx or Rx pin. Many micro boards indeed do have an LED on board for monitoring serial activity.

• try a completely different ESP-8266 chip

• Previously I tried with an ESP-01 that I had, and I encountered the same problems. I thought that the ESP was defective, and that's why I purchased the ESP-12, but it seems that the problem persists. Could my USB to serial connector be the cause of my problems ? – Antoine Aubry Mar 23 '17 at 16:47
• Did you try running it without any wires on the Tx,Rx pins? – Octopus Mar 23 '17 at 17:05
• Yes, I already tried that, and it did not seem to change anything. – Antoine Aubry Mar 23 '17 at 17:28
• It is possible that the hardwired LED pin that you are using is shared by the Tx or Rx pin. That would indeed be an issue. I added that to the answer as another point worth checking. – Octopus Mar 23 '17 at 17:46
• That's not the issue, because even if I remove the LED initialization, I still get no output on the serial port. – Antoine Aubry Mar 30 '17 at 9:57

My answer is based on past experience with USB to TTL converter for arduino , i burned several chips , before realizing that the TX and RX are inverted . what that means is that the USB to TTL RX is not the pins it uses to receive data , it is the pin that should be connected to the micro-controller RX pin .

So what i do these days is that i always use 330 to 1k resistor in series with TX and RX lines , to prevent damage if connected the wrong way.

As for your problem , you cannot tell for sure , unless you start off with new working parts. because your chip might be damaged. Using Serial.begin initialize the port to serial mode and the pins to Input and output , thats why after this initialize if the connection is wrong , the port starts to SINK or Source more current than it should be ....

Thats's just my 2 cents hope it helps

• The TTL to USB converter is correctly wired since I am able to see the bool loader's output in the serial monitor, and I am also able to upload programs. – Antoine Aubry Mar 30 '17 at 9:56

In this website it is explained in step by step how to give connections and how to flash esp8266 to avoid garbage values

1. To flash ESP8266: https://mybtechprojects.tech/microcontrollers/esp8266/flash-esp8266-with-arduino/
2. To connect esp8266 with arduino: https://mybtechprojects.tech/microcontrollers/esp8266/interface-esp8266-01-with-arduino/
• Thank you for your post. On EE.SE we like the answers to stand by themselves so that they are still relevant when the links die. You should include the essential parts of those articles in your answer. – Transistor Jan 7 '18 at 13:35