Search code examples
buttonarduinoled

Pushbutton works intermittently (Arduino)


I'm playing with Arduino and I've tried a simple circuit where an LED lights up when you press a pushbutton, using the attachInterrupt() function.

Here is the code:

const byte LED = 11;
const byte Pushbutton = 2;

// run setup code

void setup() {
  pinMode(Pushbutton, INPUT);
  pinMode(LED, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(Pushbutton), LEDManager, CHANGE);
}

void loop() {}

// <summary>LED manager HIGH or LOW</summary>
void LEDManager() {
  digitalWrite(LED, digitalRead(Pushbutton));
}

My circuit works but intermittently or when I touch the wire connected to pin D2.

I don't understand why and I need some help!

Update

I've tried many configurations and when I use new wires it works! The problem was welds, they were not well done and had probably damaged the wires (my soldering iron was too hot).

(Spoiler, a new fail! Wow!)

Update 2!

This time, I changed the circuit and connected pin D2 to the COM port of the pushbutton. The COM port is connected to a 10k ohm resistor that is connected to the Gnd pin and 3.3V pin is connected to the ON port of the pushbutton (I was inspired by that, thanks ingo).

It's finally working, alleluia !


Solution

  • I changed the circuit and connected pin D2 to the COM port of the pushbutton. The COM port is connected to a 10k ohm resistor that is connected to the Gnd pin and 3.3V pin is connected to the ON port of the pushbutton (I was inspired by that, thanks ingo).