Electromagnetic interference

I have connected a two terminal button with an ESP8266, one terminal to the D0 and another to the 3V and uploaded the following code.

// ESP8266 Fixed Button Counter
// Properly detects each button press and increments count

const int buttonPin = D0;  // Pin where button is connected
int count = 0;             // Counter variable initialized to 0
int buttonState = 1;       // Current button state
int lastButtonState = 1;   // Previous button state (1 = not pressed, 0 = pressed)

void setup() {
  // Initialize serial communication at 9600 baud
  Serial.begin(9600);
  // Set button pin as input with internal pull-up resistor
  pinMode(buttonPin, INPUT_PULLUP);

  // Read initial button state
  lastButtonState = digitalRead(buttonPin);

  Serial.println("ESP8266 Button Counter - Fixed Version");
  Serial.println("Button connected to D0");
  Serial.println("Press button to increment counter...");
  Serial.println();
  Serial.print("Initial Count: ");
  Serial.println(count);
  Serial.print("Button State: ");
  Serial.println(lastButtonState);
  Serial.println();
}

void loop() {
  // Read current button state
  buttonState = digitalRead(buttonPin);

  // Debug: Print button state changes
  if (buttonState != lastButtonState) {
    Serial.print("Button changed from ");
    Serial.print(lastButtonState);
    Serial.print(" to ");
    Serial.println(buttonState);

    // Check if button was pressed (changed from 1 to 0)
    if (lastButtonState == 1 && buttonState == 0) {
      count++;
      Serial.print("*** Button PRESSED! Count: ");
      Serial.println(count);
    }

    // Check if button was released (changed from 0 to 1)
    if (lastButtonState == 0 && buttonState == 1) {
      Serial.println("Button RELEASED");
    }

    // Update last button state
    lastButtonState = buttonState;

    // Debounce delay
    delay(50);
  }

  // Small delay to prevent excessive polling
  delay(10);
}

The code simply increment the count variable everytime the button is pressed. It works fine but I noticed an interference when I touch the staple pins(I use these to make my circuit look cleaner) on the breadboard.

I tried adding a resistor to solve this problem but it didn't worked well.

Please suggest some possible solutions.

It can't work with input_pullup.
Connect it to GND instead of 3.3V

It is working perfectly.

Video

If you say so..
Doesn't fit to my head how could it ever have any other state than HIGH.

Doesn't look like it is.

Not if you have INPUT_PULLUP and the button is connecting HIGH (3.3V) when pressed.

I get that it should be connected to GND since the pinMode is initialised with INPUT_PULLUP, but the button perfectly increments the count when toggled. Also, when I connect the other terminal of the button to GND, it does nothing. Please explain if there is something I am not able to understand.

I think you know the explanation, the button must be pulled HIGH then the pressing grounds the pin and you detect that with if LOW.

Use more words. When you posted "It is working perfectly." I assumed you had taken the device to wire the switch correctly.

Here

If you got it, why don't you do that? The switch wired between the INPUT_PULLUP input pin and ground.

And just be clear what you are now doing and talking about.

You said you tried a resistor and it did not work out for you. A drawing of what you tried may help someone see what you did wrong.

Always assume we are not in the room with you and cannot see or hear anything and that we rely on you for details.

a7

1 Like

D0 is GPIO16 and is the only pin that does not have a pull-up. That is why it "kind of" works and why you have interference.

Use a different pin with pullup and connect button to GND.

1 Like

Yes, now pressing the button makes the count increment and prints

image

And when I release the button

image

It is working perfectly. Thank you so much!

I will KIM next time, thank you for your response!