0

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() 
{
  Serial.begin(115200);
  for(i=0;i<8;i++)
  {
    pinMode(pins[i],OUTPUT);
    digitalWrite(pins[i],HIGH);
  }
}
void loop() 
{
  for(i=0;i<8;i++)
  {
    digitalWrite(pins[i],LOW);//Turns this relay on
    Serial.println(pins[i]);
//Turn the previous relay off
    if(i==0)
    {
      digitalWrite(pins[7],HIGH);
    }
    else
    {
      digitalWrite(pins[i-1],HIGH);
    }
    delay(700);
  }
}

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

  • 1
    Your usage of D0 is forcing bootloader mode to run on boot...? – Majenko Nov 11 '18 at 12:15
  • @Majenko D0 is io16 – Juraj Nov 11 '18 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 Nov 11 '18 at 14:56
3

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.

1

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, privacy policy and cookie policy

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