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);
  digitalWrite(ledPin, HIGH);

  // If this line is present, the led never turns on, not even the first time.
  // Removing it makes the led blink, as expected.

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.



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.

  • 1
    \$\begingroup\$ Put Serial.begin() in the setup() and try it. \$\endgroup\$ Mar 19, 2017 at 11:58
  • \$\begingroup\$ I've already tried that. As I explained, I put it in loop() to make sure that it happened after turning the led on. \$\endgroup\$ Mar 19, 2017 at 12:52
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Mar 19, 2017 at 13:31
  • \$\begingroup\$ Are you using Arduino IDE serial monitor? It may pull the RTS and DTR lines throwing your chip into reset. \$\endgroup\$
    – Dejvid_no1
    Mar 19, 2017 at 14:09
  • 1
    \$\begingroup\$ @ 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. \$\endgroup\$
    – Octopus
    Mar 22, 2017 at 5:00

5 Answers 5


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:

Electrical Connection Diagram

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.

  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – JRE
    Mar 22, 2017 at 11:54
  • \$\begingroup\$ I have already tried that and this did not help. \$\endgroup\$ Mar 22, 2017 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)

  for (;;)

  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.

My advice would be:

  • 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

  • \$\begingroup\$ 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 ? \$\endgroup\$ Mar 23, 2017 at 16:47
  • \$\begingroup\$ Did you try running it without any wires on the Tx,Rx pins? \$\endgroup\$
    – Octopus
    Mar 23, 2017 at 17:05
  • \$\begingroup\$ Yes, I already tried that, and it did not seem to change anything. \$\endgroup\$ Mar 23, 2017 at 17:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – Octopus
    Mar 23, 2017 at 17:46
  • \$\begingroup\$ That's not the issue, because even if I remove the LED initialization, I still get no output on the serial port. \$\endgroup\$ Mar 30, 2017 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

  • \$\begingroup\$ 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. \$\endgroup\$ Mar 30, 2017 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/
  • \$\begingroup\$ 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. \$\endgroup\$
    – Transistor
    Jan 7, 2018 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.