logo elektroda
logo elektroda
X
logo elektroda

[Solved] Why is the button for the ESP8266 not working properly and how to fix it?

pier 1689 11
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18188427
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    Welcome.

    A seemingly simple issue but it doesn't want to work.
    It needs a button for the esp8266. Button connected under D6, gpio12, pulled up with 10k resistor to +3.3V. The button short circuits to ground.
    I tried a few different configurations because I thought the problem was there but it's always the same, even though the button is not pressed the program responds as if it was.
    There are no short circuits on the board.

    What could be the cause of this?

    Code: C / C++
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #2 18188512
    oskar777

    Level 26  
    Posts: 1264
    Help: 76
    Rate: 243
    Have you tried simply giving
    Code: C / C++
    Log in, to see the code
    .
    Company Account:
    Oskar-info
    Gidzińskiego 24/1, Warszawa, 02-293 | Tel.: 501XXXXXX (Show) | Company Website: http://oskar-info.pl
  • ADVERTISEMENT
  • #3 18188542
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #4 18188594
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    oskar777 wrote:
    Tried to give simply
    Code: C / C++
    Log in, to see the code
    .


    I have tried in various ways always the same result. It also sends a zero on the serial.
    I don't know how it can see zero if it is physically pulled up with a resistor to plus.

    khoam wrote:
    .
    pier wrote:
    but it is always the same, even though the button is not pressed the program reacts as if it is.

    The variable 'temp' refers to the temperature value, or the state of the button? You have this variable declared twice in the code, once globally and once in the loop().

    Code: c [expand] [select all].


    int temp = htu.readTemperature();



    Code: c [expand] [select all].


    temp = digitalRead(SW);
    .

    Yes I know this is a configuration from some example from the web. I already knocked it out but still no change.
  • #5 18188600
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #6 18188611
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    Code: C / C++
    Log in, to see the code
    .
  • #7 18188695
    Anonymous
    Level 1  
  • #8 18190179
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    khoam wrote:
    Change to:
    Code: C / C++
    Log in, to see the code
    Replace to.
    and knock out the external pull-up resistor.
    .

    Done. No effect...
  • #9 18190197
    Anonymous
    Level 1  
  • #10 18190234
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    I have a suspicion that it is the SPI that may be messing with something on this pin.
  • #11 18190262
    Anonymous
    Level 1  
  • #12 18193714
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    Definitely SPI. I have flipped the DC display pin from GPIO0 to GPIO12 and the display works so all is well with the pin. The button will fly to GPIO0 and it will be ok.

    I messed up myself and used the SPI pin for something else because it suited me so well on the board.

Topic summary

✨ The discussion revolves around troubleshooting a non-functional button connected to the ESP8266, specifically GPIO12 (D6). The user reports that the button, which is pulled up to +3.3V with a 10k resistor, consistently registers as pressed (LOW) even when not activated. Various suggestions are made, including checking the pin configuration, using `pinMode(SW, INPUT_PULLUP)`, and ensuring no other components interfere with GPIO12. The user later discovers that the issue stemmed from using the SPI pins incorrectly, which conflicted with the button's functionality. The button was successfully relocated to GPIO0, resolving the problem.
Generated by the language model.

FAQ

TL;DR: On ESP8266, 4 SPI pins (GPIO12–GPIO15) can hijack button reads; “Are you using SPI on pins 12–15?” Move the button off D6 or reassign the display. [Elektroda, khoam, post #18190262]

Why it matters: This FAQ helps ESP8266 makers using TFT_eSPI fix buttons that read pressed when they aren’t.

Quick Facts

  • D6 on NodeMCU maps to GPIO12 (HSPI MISO); avoid sharing it with a button if SPI/TFT is active. [ESP8266 Pinout Reference]
  • HSPI uses GPIO12–GPIO15; GPIO15 has an internal pulldown and is unsuitable for pull-up buttons. [ESP8266 Pinout Reference]
  • TFT_eSPI drives pins defined in User_Setup.h; verify CS/DC/RESET and SPI lines before wiring. [TFT_eSPI README]
  • Using INPUT_PULLUP lets you wire the switch to GND; no external 10 kΩ is typically needed. [Arduino pinMode Reference]
  • Mechanical switch bounce is typically 5–20 ms; debounce in code or hardware. [Ganssle, 2014]

Why does my ESP8266 button on D6/GPIO12 read LOW when unpressed?

Because the TFT or SPI control signals are driving GPIO12. On NodeMCU, D6 equals GPIO12, which your display library may toggle. The original poster confirmed the display pin assignment caused the false presses: “Definitely SPI.” Move the button to a non-SPI pin or reassign display pins. [Elektroda, pier, post #18193714]

Which ESP8266 pins are safe for buttons?

Use GPIO4 (D2) and GPIO5 (D1) widely. You can also use GPIO12–GPIO14 if SPI is unused. Avoid GPIO15 for pull-up buttons due to its internal pulldown. Use GPIO0 only if it stays HIGH during boot. Check your board’s mapping before wiring. [ESP8266 Pinout Reference]

Should I enable INPUT_PULLUP or keep my external 10 kΩ pull-up?

Enable INPUT_PULLUP and remove the external pull-up for a simple GND-wired button. This reduces wiring and avoids conflicts with other drivers. A contributor recommended switching to INPUT_PULLUP and removing the external resistor during troubleshooting. [Elektroda, khoam, post #18188695]

How do I quickly test a button on ESP8266 with INPUT_PULLUP?

  1. Set your button pin as INPUT_PULLUP.
  2. Wire the button between the pin and GND.
  3. Print digitalRead() over Serial; expect LOW when pressed, HIGH when released. “I tested GPIO12 in ‘INPUT_PULLUP’ mode… everything was OK.” [Elektroda, khoam, post #18190197]

Could variable shadowing make my button look broken?

Yes. Reusing a variable name can mask values and mislead debugging. In the thread, the same name ‘temp’ was used for temperature and button state. Rename variables so sensor data and button state are separate, then retest. [Elektroda, khoam, post #18188542]

What does D6 actually map to on NodeMCU and D1 mini?

On common ESP8266 dev boards, D6 maps to GPIO12, which is also HSPI MISO. Always check your specific board’s mapping chart to avoid conflicts with SPI peripherals or libraries. [ESP8266 Pinout Reference]

Can I use GPIO0 for a button without breaking boot?

Yes, but keep it HIGH at reset. GPIO0 Low during boot puts the ESP8266 into flashing mode. Use an internal pull-up and wire the button to GND; avoid pressing it during reset. [ESP8266 Pinout Reference]

How do I confirm which pins TFT_eSPI is using?

Open TFT_eSPI’s User_Setup.h (or your board setup file) and check the defined CS, DC, RST, and HSPI pins. Ensure your button is not on any of these lines before wiring. [TFT_eSPI README]

Why does Serial always print 0 for digitalRead(SW) on my pulled-up pin?

A peripheral or wiring may be forcing the line LOW. In the thread, the pin read zero despite a pull-up until the display pin assignment was moved. Check for SPI or control signals on that pin. [Elektroda, pier, post #18188594]

What happens if I share SPI pins with buttons on ESP8266?

The SPI or display library will toggle those lines, so digitalRead() can show false presses. Keep buttons off HSPI pins used by your display, or reassign the display control pins. [TFT_eSPI README]

How should I declare the button pin: D6 or 12?

Use the board alias (e.g., D6) or the raw GPIO number consistently. A poster suggested defining SW as D6 to avoid mapping mistakes. Ensure your board variant matches the aliases. [Elektroda, oskar777, post #18188512]

How can I resolve a conflict between my button and the display’s DC/CS pin?

Reassign the display’s DC/CS in User_Setup.h to a non-button pin, then rewire. In the thread, moving DC freed the button line and restored proper reads. [Elektroda, pier, post #18193714]

Do I need to debounce the button, and by how much?

Yes. Mechanical switches can bounce 5–20 ms typically. Add a short software delay or state machine, or use RC hardware debounce, to prevent false triggers. [Ganssle, 2014]

Any gotchas with GPIO15 for buttons?

Avoid using GPIO15 with a pull-up button. It has an internal pulldown and is a boot strapping pin, which can prevent normal boot if mishandled. [ESP8266 Pinout Reference]
Generated by the language model.
ADVERTISEMENT