ESP8266 (NodeMCU and Wemos D1 mini) won't boot or upload with particular devices

Hi all. I've come across a weird and frustrating issue. I have a simple reed switch attached to a NodeMCU and the code uploads and executes with no problems UNLESS the reed switch is activated (ie the presence of a magnet means the switch is closed at the time of the upload). If I open the switch by moving the magnet away, no problem. If I try to upload with the switch closed the upload fails every time. Once the code loads, the reed switch and the code work perfectly until the device is rebooted. Again, if the magnet has closed the reed switch when the device boots, it fails to load any of its code.

I thought this was a quirk of the reed sensor I was using until I built another project that uses a 3.3v relay. If the relay is powered from the NodeMCU's 3.3v pin when the device boots or an upload is attempted, it fails in exactly the same way that the reed switch module does.

To get the code successfully uploaded, I have to disconnect the relay power then reconnect it again after the upload has completed and the device has rebooted. Then it all works fine ... until the device reboots if it loses power for any reason. Again, it will not start at all if the relay is being powered from its 3.3v pin at the time of rebooting. It acts exactly the same as the reed switch does if it is in a closed state.

My initial thoughts were that the computer couldn't provide enough power, but if the device is connected to an external 2 amp USB charger it behaves the same on startup.

I can think of two things to try. One is to have a separate power supply altogether for the reed switch and the relay, except this is really undesirable. The second idea was to look into whether a capacitor could be used that delays the relay from starting until after the device has started. The problem with that is that it probably won't help with uploads.

OTA uploads fail in exactly the same way as USB uploads, and of course the device fails to reboot properly anyway after an OTA update.

Both modules have a buzzer, which makes a very high-pitched but low volume noise during both upload failure and reboot failure. The buzzer makes the same noise during a successful upload and immediately stops when the upload completes. I don't know if that is significant or not.

The Wemos D1 mini behaves exactly the same as the NodeMCU. I've tried multiple MCUs and D1s and they all act the same.

I'm using a Mac with Arduino IDE 1.8.5. I haven't tried the sketches out on an Uno or Mega yet to see if it is just ESP8266 related. Before I go through a lengthy testing process I thought I'd reach out to the community to see if anyone has had the same problem and solved it, or if there is an obvious cause that I've missed. Thank you in advance if you can help!

You're confusing voltage with current.

I suspect your relay when closed is drawing more current than the NodeMCU board can supply.

to which pins are buzzer and relay connected?

The relay is connected to NodeMCU pin 8 (GPIO15) and the buzzer to pin 5 (GPIO14)

With the reed sensor sketch, the sensor is on pin 4 (GPIO2).

I've tried other pin combinations but get the same result. I suspect ieee488's comment might be right.

12mA is the NodeMCU's source current per pin but there are no specs for the relay or reed switch in terms of their respective current draws.

As both of them operate from 3.3v to 5v could I use the NodeMCU Vin pin instead, as my understanding is that draws directly from the 2 amp USB power source?

deej99:
I use the NodeMCU Vin pin instead, as my understanding is that draws directly from the 2 amp USB power source?

You should be able to if the schematics are to be believed.
There is a zener diode drop from VU to VIN.

deej99:
The relay is connected to NodeMCU pin 8 (GPIO15) and the buzzer to pin 5 (GPIO14)

With the reed sensor sketch, the sensor is on pin 4 (GPIO2).

I've tried other pin combinations but get the same result. I suspect ieee488's comment might be right.

12mA is the NodeMCU's source current per pin but there are no specs for the relay or reed switch in terms of their respective current draws.

As both of them operate from 3.3v to 5v could I use the NodeMCU Vin pin instead, as my understanding is that draws directly from the 2 amp USB power source?

GPIO2 should be tied pulled up and GPIO15 should be pulled down when the ESP8266 is booting.
Source.

BJHenry:
GPIO2 should be tied pulled up and GPIO15 should be pulled down when the ESP8266 is booting.
Source.

BJHenry:
GPIO2 should be tied pulled up and GPIO15 should be pulled down when the ESP8266 is booting.

Thanks - I did not know any of that. So I've accidentally used 2 of the 3 pins that require "special handling". If I change the pins to use 4, 5, 12, 13 or 14 then will that solve the issue?

deej99:
Thanks - I did not know any of that. So I've accidentally used 2 of the 3 pins that require "special handling". If I change the pins to use 4, 5, 12, 13 or 14 then will that solve the issue?

Full disclosure, I'm not very familiar with the ESP8266. I only know about the boot pin issue because of a project that I've been involved in that uses the ESP32.
Having said that, it seems that as long as you avoid GPIO 0,2,6-11 and 15 you should be ok.

deej99:
Thanks - I did not know any of that. So I've accidentally used 2 of the 3 pins that require "special handling". If I change the pins to use 4, 5, 12, 13 or 14 then will that solve the issue?

https://tttapa.github.io/ESP8266/Chap04%20-%20Microcontroller.html

.