1

I have bought a NodeMCU (1.0) and a JSN-SR04T waterproof ultrasonic sensor compatible with HC-SR04.

If I upload my program to NodeMCU it works but sometimes it measures a wrong distance - it is an another question, but it may connect to this question.

If I press the reset button my application not starts or if it is unplugged and plugged in to usb power it not starts.

If I unplug the ultrasonic sensor 3.3V and ground it starts.

It starts with the following code:

const int trigPin = D4;
const int echoPin = D3;

void flashLed() {
  digitalWrite(LED_BUILTIN, LOW);
  delay(400);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);
}

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  flashLed();
  Serial.begin(115200);
  flashLed();  
  delay(10);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);  
    delay(1000);    
  }
  Serial.println("");
  Serial.println("WiFi connected");

  server.begin();

  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);

  Serial.println(WiFi.localIP());

  digitalWrite(LED_BUILTIN, HIGH);  

}

I have uploaded the full source code to github project:

https://github.com/kisdaniel/nodemcu-wif-http-HC-SR04/blob/master/HomeDashboardUltrasonicHttp.ino

Why it not starts?

UPDATE:

I have make a minimal version of code to reproduce the problem:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);                      
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(2000);                 
}

Wiring:

Node MCU 3.3V -  Vcc (ultrasonic sensor)
Node MCU GND  -  GND (ultrasonic sensor)
Node MCU D4   -  Trigger (ultrasonic sensor)
Node MCU D3   -  Echo (ultrasonic sensor)
4
  • Why it not starts? Commented Nov 19, 2017 at 8:29
  • Please edit your question to include a Minimal, Complete, and Verifiable example of code, not just snippets. Minimal means you've stripped away irrelevant stuff, just leaving what's needed to show the problem. Complete means all the library names are shown, all the variable declarations, and all the function definitions – so people don't have to waste time guessing what you did or what you meant. Verifiable means it can be compiled and tested, allowing other people to test their theories about the problem
    – user31481
    Commented Nov 19, 2017 at 8:33
  • Full source can by download from github you can try it, if you need a minimal version I can remove all unnecessary code to reproduce the problem. Commented Nov 19, 2017 at 8:37
  • 1
    This post saved my day! All Ultrasonic sensor codes around the internet use D3 for the Echo input, and this causes the NodeMCU not booting up always. Commented Feb 3, 2021 at 15:12

2 Answers 2

3

To enable ESP8266 firmware flashing GPIO0 pin must be pulled low before the device is reset. Conversely, for a normal boot, GPIO0 must be pulled high or floating.

The D3 pin is equivalent GPIO0. For a normal boot, GPIO0 must be pulled high or floating while powering up or resetting the module. After that (normal boot), you can use the output for other purposes.

You can use a different output or try to use a pull-up resistor for the D3.

6
  • Please, expand your answer. How do you use the D3 pin for other purposes?
    – user31481
    Commented Nov 19, 2017 at 11:44
  • @Look Alterno If I add the following text will it be enough?: For a normal boot, GPIO0 must be pulled high or floating while powering up or resetting the module. After that (normal boot), you can use the output for other purposes.
    – AltAir
    Commented Nov 19, 2017 at 12:44
  • Yes! You have valuable information to share. Look at answers by Majenko, Code Gorilla and Nick Gammon as a guide.
    – user31481
    Commented Nov 19, 2017 at 12:53
  • 1
    If I connect the Echo to D2 it semms to be working. after reset. Commented Nov 19, 2017 at 21:18
  • This is the correct identification of the issue, but it's unlikely that adding a pullup resistor on D3/GPIO0 will help, as it's probably being driven low actively. Commented Nov 19, 2017 at 22:36
0

I had the same problem. Eventually I worked out what was going wrong with the information at "Startup Guide for ESP8266 NodeMCU WeMos"

It explains - "Using GPIO0 (D3), GPIO2 (D4) and GPIO15 (D8) can be bit tricky. There are certain reasons, for example, GPIO0 (D3) and GPIO2 (D4) are pulled HIGH (PULL-UP Resistor) for normal start up of ESP8266 while GPIO15 (D8) is pulled LOW (PULL-DOWN Resistor). It is recommended to not use these pins as INPUT."

The problem happens when one of the NodeMCU pins that goes HIGH in startup - D3 and D4 - is connected to the "ECHO" pin of the JSN-SR04T sensor while the supply and ground pins are connected. The "ECHO" pin has low impedance and reduces the voltage on either D3 or D4 to about 1 Volt. This prevents the NodeMCU from rebooting.

The problem doesn't happen if the D3 or D4 pin is connected to the "TRIGGER" pin of the JSN-SR04T sensor because it has high impedance and doesn't reduce the voltage on the NodeMCU pin connected to it.

This matches what the article I found recommended: using D3, D4 and D8 for OUTPUT is OK, but it is recommended to not use them for INPUT.

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.