Nodemcu and LED

Hi guys I am using NODEMCU for turning on and off the led using a switch, nodemcu is resetting it automatically thus disabling itself to get into the loop. How to solve this issue?

Code:

#include <ESP8266WiFi.h>
#define ledPin 6 // choose the pin for the LED 
#define switchPin 7 // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup(){
  Serial.begin(115200);
  Serial.println("begin");
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(switchPin, INPUT); // declare pushbutton as input
}
void loop(){
  val = digitalRead(switchPin); 
  Serial.println(val);
  if (val == 0)
  { 
    digitalWrite(ledPin, LOW); 
    Serial.println("Low");
    delay(200);
  } else {
    digitalWrite(ledPin, HIGH); 
     Serial.println("High");
     delay(200);
  }
}

Error on Serial Monitor:
ets Jan 8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
vac02aff5
~ld
begin

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).


➜ please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

--

Are you using the following NodeMCU Board (Fig-1)? If yes, then where are the GPIO 6 and 7. If not using the board of Fig-1, please post the picture of your board.


Figure-1:

I am using this NodeMCU and I have tried pin no 4 and 5 too but still not working. It's Nodemcu V3

They are the same board. Do you mean D6 and D7 in the following definitional directives?

#define ledPin 6 // choose the pin for the LED
#define switchPin 7 // 

Yeah for defining them.

Then change your directives as follows and re-run your sketch. If not working come back here for help.

#define ledPin D6        // IO pin number as marked on the bord; pin for the LED
#define switchPin D7 //

Or

#define ledPin 12        // GPIO number; choose the pin for the LED
#define switchPin 13 //

Thanks a lot, Golam, its really working I used the second directive. Thanks a lot :slight_smile:

Have a fun! Print Fig-1 of post #3 and paste it in your NodeMCU Work/Exercise Book.

1 Like

your board lists D0, D1,... why don't you use that.. D6 and D7 would make the code easier to read and the wiring clearer...

image

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.