Articles / Embedded

  • esp8266
  • gpio
  • strapping-pins
  • boot
  • hardware
  • debugging

Why Your ESP8266 Boots on USB but Hangs With a Sensor on D8

Your sketch works on the bench, then you power the finished prototype with its sensor attached and get nothing. On NodeMCU and Wemos D1 mini style boards, D8 is GPIO15, and GPIO15 must be low when the chip comes out of reset. A sensor that holds it high sends the ESP8266 looking for a completely different boot source.

Your code never runs, so no amount of Serial.println() in setup() will tell you anything.

Three pins choose what the chip boots from

The ESP8266 reads GPIO15, GPIO0 and GPIO2 as it leaves reset. These are the strapping pins, and afterwards they become ordinary GPIOs (with some baggage).

  • Normal boot from flash: GPIO15 low, GPIO0 high, GPIO2 high.
  • Serial upload: GPIO15 low, GPIO0 low, GPIO2 high.
  • SDIO boot: GPIO15 high. On an ordinary dev board there is nothing useful there, so it appears to hang.

Your board already has a resistor pulling D8 down. Your sensor, or the breakout it sits on, may have another resistor or an output driver pulling it up, and at reset the two sides fight.

The boot log tells you which pin lost

The boot ROM prints its verdict before your sketch starts, usually at the odd rate of 74880 baud. At 115200 it looks like line noise, which many of us have been cheerfully ignoring for years.

python -m serial.tools.miniterm --dtr 0 --rts 0 /dev/ttyUSB0 74880

That needs pyserial, and your port name may differ. Reset the board and look for a line shaped like this:

ets Jan  8 2013,rst cause:2, boot mode:(7,0)

Read the first number after boot mode:. The Arduino core's board documentation gives it as three bits:

mode = (GPIO15 << 2) | (GPIO0 << 1) | GPIO2

3      flash boot (what you want)
1      serial upload
4 to 7 SDIO boot, because GPIO15 was high

If you see 4 or higher with the sensor connected, and 3 without it, you have found the fault. If you see 3 both times, look elsewhere: power supply sag is a sensible next suspect.

USB works because the test was different

USB does nothing special to GPIO15. Something else usually changed between the run that worked and the run that failed, for example:

  • the sensor was unplugged during upload and connected after the sketch had started
  • the sensor and board powered up in a different order once they moved to the final supply
  • the sensor's output was low when you pressed reset, but starts high from cold

So test the thing you will actually do: remove all power, wait for the rails to discharge, then apply it with everything connected. ESP.restart() is not a substitute.

To log a true cold start, power the prototype from its real supply and connect only the RX and ground of a 3.3 V serial adapter to the board's TX and ground. Start the monitor first.

A 10k pull-up is already far too strong

The ESP8266EX datasheet puts a valid low at no more than 0.25 of the I/O voltage, which is about 0.825 V on a 3.3 V board. A valid high starts at 0.75, about 2.475 V.

Assume your board has a 10k pull-down on D8 (check its schematic, or measure with the power off and sensor removed). A sensor pull-up forms a voltage divider with it:

V(D8) = 3.3 * Rdown / (Rup + Rdown)

Rup = 47k    0.58 V   valid low
Rup = 30k    0.83 V   right on the limit
Rup = 10k    1.65 V   undefined
Rup = 4.7k   2.24 V   undefined, close to high

"Undefined" can mean it boots on your desk and fails with a different board, temperature or supply. One successful boot at 1.65 V proves very little.

Quick detour: I2C breakouts are an easy way to walk into this, because I2C needs pull-ups and many breakouts helpfully include them. Stack a few modules on one bus and those resistors sit in parallel, making the combined pull-up stronger still.

Moving the wire is the best fix

For an I2C sensor, use D2 for SDA and D1 for SCL. Those are GPIO4 and GPIO5, neither of which selects the boot mode.

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin(D2, D1);  // SDA, SCL

  Wire.beginTransmission(0x76);  // change to your sensor's address
  byte err = Wire.endTransmission();
  Serial.println(err == 0 ? "sensor answered" : "no answer from sensor");
}

void loop() {}

Then repeat the cold start several times with the sensor attached. You want boot mode 3 and an answer from the sensor each time.

Don't just swap D8 for D3 or D4. They are GPIO0 and GPIO2, the other two strapping pins, and they have the opposite requirement: they need to be high for a normal boot.

If D8 is all you have, isolate it until boot finishes

A stronger pull-down looks tempting, but it is a poor bargain. Against a 4.7k pull-up, even adding 2.2k alongside the existing 10k leaves D8 at about 0.92 V, still not a valid low. On I2C it also drags down the idle high level that the bus depends on.

Your options depend on what the signal does:

  • Use D8 as an output to something that doesn't pull it up at reset, such as a suitable logic-level MOSFET gate with a pull-down.
  • Buffer a sensor output with a 3.3 V tri-state buffer such as a 74LVC1G125. Pull its active-low enable pin high so it starts disconnected, then enable it from a spare non-strapping GPIO once your sketch is running.
  • Remove the breakout's pull-up only if the signal doesn't need it. For I2C it does, so move the bus.

Hang on, why is such an awkward pin offered at all? GPIO15 is also MTDO and the usual hardware SPI chip select. Chip select normally idles high, which is exactly what GPIO15 must not do during reset. If an SPI module has its own pull-up on that line, use another free GPIO as chip select and let your library toggle it.

Whichever fix you choose, measure D8 with a meter while holding the board in reset, with the sensor powered. It should be comfortably below 0.8 V. A steady reading won't reveal a brief pulse as the sensor powers up, so use an oscilloscope or logic analyser if cold starts still fail.