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)
D3
for theEcho
input, and this causes the NodeMCU not booting up always.