I have a string of 12V LEDs attached to a 9V DC adapter through an ESP8266 NodeMCU. The LEDs light up OK. I'm trying to introduce a switch to the circuit. Currently, I have a simple spst temporary push button switch with 2 pins. One pin is connected to pin D4 on the node MCU and the other to ground through a 10k resistor. The LEDs are attached to pin D7. It's my understanding that D4=GPIO 2 and D7=GPIO 13. Here is the sketch I'm using:
const int BUTTON = 2; // the number of the pushbutton pin
const int LED = 13; // the number of the LED pin
void setup() {
// initialize the LED pin as an output:
pinMode(LED, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(BUTTON, INPUT);
}
void loop() {
int stateButton = digitalRead(BUTTON); //read the state of the button
if(stateButton == 1) { //if is pressed
digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
} else {
digitalWrite(LED, LOW);
}
}
Pushing the button has no effect on the LEDs. Can anyone help?