-->
Page 1 of 1

Switch question

PostPosted: Tue Apr 10, 2018 10:00 pm
by James Dickinson
New here, so please forgive me if this is not the right place for this question...
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?

Re: Switch question

PostPosted: Thu Apr 12, 2018 5:11 am
by btidey
Your switch connects to ground through a 10K resistor but is connected to a GPIO input which just has a high input impedance so when the switch is open then the state of the GPIO is undetermined and may well be seen as a low as well.

You should change this to either

change the mode to INPUT_PULLUP which internally pulls up to 3.3V through a ~36K resistor. Then just connect the switch to this pin and directly to ground. Open will be high and closed will be low.

or

Add an external pull up from the pin to 3.3V (say 10K) and connect switch to the pin and ground.

Second method is to be preferred if the switch is connected by longer wires as the lower value resistor makes it less susceptible to noise.

I would also put a small delay of a few millisecond in the loop to avoid spinning round at maximum cpu speed.

Re: Switch question

PostPosted: Thu Apr 12, 2018 11:09 am
by mrlightsman
I don't have the code right in front of me, but I would suggest using the Switch.h library (I think it is now avw_Switch, or something like that) then use code more along these lines:

you need to set button as a switch in your global settings along with your other variables.
Code: Select allSwitch.button(Button1)


Then in your void loop:
Code: Select allPoll(button1);

if (pushed.button1) {
in State = digitalRead(LED);
digitalWrite(LED, !State);
}

The syntax is not right and I don't have the reference to correct it right now, but in short, the using the switch library and poll function will constantly check the status of Button. And, when pressed, it will toggle the LED to the opposite state (either high to low or low to high). The way you have your code right now, I think LED will only be high for the brief moments you are pressing button. Hit GitHub up for avd_Switch.h. There are examples there.

Prior suggestions about using a pullup resistor are important. Good luck.