Search code examples
arduinoesp8266arduino-idearduino-esp8266

D1 mini ESP8266 pulldown button always HIGH


I am trying to connect a button to my d1 mini ESP8266. The button is connected with a pulldown resistor (10kOhm) to GND. And on the same side of the button to the D4 Pin. on the other side the button is connected to 3.3V.

I used this description for arduino on how to connect push button

Here is how I connected it:

enter image description here

Here is my code:

#define BUTTON_PIN 4

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  Serial.println(digitalRead(BUTTON_PIN));
  delay(10);
}

I also tried without the pulldown resistor. The button connected to GND on one side and to D4 on the other side. Then updated the code so it uses

pinMode(BUTTON_PIN, INPUT_PULLUP);

But on the Serial Plotter and in the Serial Monitor the button is always HIGH (1) and there is no change when I press the button. (button was tested with a continuity tester and works ok)

I dont get it, what am I doing wrong here.


Solution

  • On most ESP8266 boards (D1-mini included) the pins are labeled as D1, D2...etc but those numbers don't correspond to the Arduino GPIO pin numbers (the ones you are using in the code :-)

    It's always best to find the pinout diagram for the board you want to use, simply google the "board name pinout" and look for images.

    Here is the pinout for the D1 Mini

    • D0 - Arduino pin 16
    • D1 - Arduino pin 5
    • D2 - Arduino pin 4
    • D3 - Arduino pin 0
    • D4 - Arduino pin 2
    • BUILT_IN_LED - Arduino pin 2
    • PIN_D5 - Arduino pin 14
    • PIN_D6 - Arduino pin 12
    • PIN_D7 - Arduino pin 13
    • PIN_D8 - Arduino pin 15
    • PIN_RX - Arduino pin 3
    • PIN_TX - Arduino pin 1

    You have basically in your code specified that the button is connected to the pin D2 on the Mini

    If you are using D4 (i.e. Arduino pin 2) that one is also wired to the built in led so it may work bit funky, I tend to stick on the Mini to the D5 to D8

    Here's a picture of the D1-Mini pinout

    enter image description here

    Hope this helps