1

I'm trying to create a watering system using a NodeMCU 1.0, a relay and some pumps.

Currently I've port D0, D1, D2, D5, D6, D7, D8 already in use. I'm trying to use the port D3 (GPIO0) as follows, but when I plug or reboot the NodeMCU, sometimes (!) at boot the relay starts, the pump starts watering and the board does not finish the boot.

Simplified Schema

The only way to start the board are the following:

  • Unplug the current from relay
  • Unplug D4 until boot ends
  • Disconnect the pump from relay

I tried to substitute the pump with a led and a resistor and I've no problem. I tried also to put a resistor before the pump and it starts (but the pump does not work).

At boot my first command (after defining variable and object is setting D3 as HIGH)

#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define A0 17
#define TX 1
#define RX 3
#define MSG_BUFFER_SIZE 128


// Initialize clients
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
char msg[MSG_BUFFER_SIZE];
int iteration = 0;
int ID = -1;
int first_run = 1;

void setup() {
  //Initial setup
  digitalWrite(D3, HIGH);
  pinMode(D3, OUTPUT);
  delay(4000);
  Serial.begin(9600);
  ...
}

I really don't understand where is the problem.

4
  • what do you expect the top relay to do? ... where is the pump? ... where is the power supply?
    – jsotola
    Commented Mar 15 at 5:20
  • D3 is io 0 the boot mode selection pin. it must be HIGH for normal boot. NodeMCU has a pull-up on it.
    – Juraj
    Commented Mar 15 at 6:24
  • @jsotola the ralay should turn on and off the water pump when I receive a specific mqtt message. The pump is the white stuff in the picture and is physically nearby the sensor. The power is provided via USB to nodemcu
    – Timmy
    Commented Mar 15 at 8:05
  • @Juraj I set D0 as high, is it wrong?
    – Timmy
    Commented Mar 15 at 8:05

1 Answer 1

1

D3 is connected to the FLASH button on the board. It is used to control the boot mode on startup. If this pin is LOW on startup it will stay in flash mode, ready to receive a new program. If it is high on startup it will then execute its saved program (normal boot).

What probably happens here is, that your relay board pulls D3 to LOW, when its connected (probably only a bit stronger than the NodeMCUs builtin pullup resistor). Probably it has a pulldown resistor on its input pin (to keep the relay open by default). The NodeMCU then sees D3 being LOW on startup and stays in flash mode, never executing your code. To let the NodeMCU execute your code, D3 needs to be HIGH at startup! If this happens randomly, then the pullup and pulldown resistors are probably have similar values, so that the noise in the system can switch the state of the pin.

You now have different possibilities:

  • You could remove the pulldown resistor from your relay board, so that the pullup resistor on the NodeMCU can pull the pin to HIGH during start, until your program will set it to LOW again. Then you need to live with the fact, that the relay will be ON during startup of the NodeMCU, running your pump in that time.
  • You can do the above, but also move the pump connections to the NC (Normally Closed) contact of the relay. This means the pump is activated, when D3 is LOW, and deactivated, if it is HIGH, so the logic is inverted. D3 being HIGH on startup would then mean the pump isn't running. The caveat is, that if the pumps power is on, which the NodeMCU/relay power is off, the pump will be running.
  • You can use a different pin for this function. One that is free of a special boot function. We don't know, what you are doing with the other pins. D3 would be a good fit for an input pin, that needs the pullup resistor (like for connecting a button). Or an output pin, where the state at startup is not dictated by the outside device.
4
  • Thank you for this precious answer. The first option (remove pulldown) seems fine to me. How can I remove the pulldown and exactly what is it? I've a relay SRD-05VDC-SL-C
    – Timmy
    Commented Mar 15 at 17:20
  • I tested to set NC the relay but I get the same result. Sometimes (actually often like 4 times out of 5) the board does not start and remains in flashing. Keep testing the other 2 ways
    – Timmy
    Commented Mar 16 at 13:53
  • That part number is only the relay itself, not the relay module, that you use. Can you provide a product link for that? And when doing my second point, you still need to remove the pulldown resistor from your relay module. Using the NC port of the relay doesn't change anything on the signal line. My second point is only to prevent the pump to run during the NodeMCUs startup and its a tradeoff. Best would really be to use a different pin.
    – chrisl
    Commented Mar 16 at 22:27
  • I bought this relay (it.aliexpress.com/item/1005005865597217.html). It's the model with 4 channels
    – Timmy
    Commented Mar 18 at 23:00

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.