Search code examples
arduinoesp8266arduino-idenodemcunodemcu-build

Switch state not changing according to physical changes nodeMCU float switches


I am using float switches(3 Nos) as depicted below and a NodeMCU esp8266,

all three switches have been defined in the same way(input_pullup)

2 of these switches change state as the switch movement commands but one of these switches are not changing state and are stuck in LOW forever

please find the pin and code attached below with a picture of the switch in open and closed state.

uint8_t level1=D0; //switch with issue // have also tried with D8 as only these two pins are free
uint8_t level2=D1; //switch working fine
uint8_t level3=D3; //switch working fine

    pinMode(level1,INPUT_PULLUP);
      pinMode(level2,INPUT_PULLUP);
      pinMode(level3,INPUT_PULLUP);
    }
    int currlevel=0;
    void loop() {
    
      int level1Sensor=digitalRead(level1);
      int level2Sensor=digitalRead(level2);
      int level3Sensor=digitalRead(level3);
      Serial.println(level1Sensor);
      Serial.println(level2Sensor);
      Serial.println(level3Sensor);

CLOSED STATE PICTURE OPEN STATE PICTURE

NOTE: i am also using an nrf24L01 for communication in the same nodeMCU but the pins used for these switches are not overlapping.


Solution

  • As mentioned in the link @Juraj posted, D0 (GPIO16) does NOT have an internal pull-up. It has an internal pull-down. Use a different pin.