I have uploaded a simple code to control a 8 channel relay by turning individual relays on and off When I upload the code it works properly, the code works properly and the relays turn on and off in the desired pattern. However when i disconnect the USB cable from my computer and reconnect it, the code isnt working anymore, there is no visible action happening. Is the code erased from memory after power is cut?

int pins[] = {D0, D1, D2, D4, D5, D6, D7, D8};
int i;

void setup() 
void loop() 
    digitalWrite(pins[i],LOW);//Turns this relay on
//Turn the previous relay off

My circuit:
NodeMCU | Relay
D0 IN1
D1 IN2
D2 IN3
D4 IN4
D5 IN5
D6 IN6
D7 IN7
D8 IN8

  • 1
    Your usage of D0 is forcing bootloader mode to run on boot...?
    – Majenko
    Commented Nov 11, 2018 at 12:15
  • @Majenko D0 is io16
    – Juraj
    Commented Nov 11, 2018 at 14:50
  • Well, whatever random D number they decided to assign to GPIO 0 then.... Why couldn't they just number them what they are, rather than randomly assigning other numbers to them?
    – Majenko
    Commented Nov 11, 2018 at 14:56
  • I had the same issue and solved the problem by the following link : github.com/esp8266/Arduino/issues/1074#issuecomment-258669539 Commented Jun 23, 2020 at 14:55

2 Answers 2


D3 (GPIO0) and D4 (GPIO2) both need to be high on startup for the user program to be executed. The relay board connected to those pins are preventing a sufficient high on those pins.

If you need to use those pins then try using a stronger pull-up resistor on those pins. Try 330-470 ohms.

Either that or you will need to add additional circuitry between the port pins and the relay driver. Also please be aware that this will place a high on the relay driver input and will try to turn on the relays. Your powerup conditions may not be what you expect.


Your code will block the loop for more than 5 seconds. For the NodeMcu I found that if the main loop is delayed for long time strange things will happen. So you may also try refactoring to get rid of the for loop and using a simple counter to toggle the relays in each iteration of the main loop. so that your main loop will only be blocked for less than a second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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