5V Out causes reset?

I'm doing a basic push button circuit. When I unplug my arduino R3 from usb and connect it to 9V battery, the 5V output causes my arduino to reset when I push my push pin. My LED never turns on. When I push my normally closed button, the voltage jumps to 0V.

circuit(1).png

EDIT/SOLUTION: I’m a total dummy and thought the internally connected part of the push button were the parts that get connected when you depress it The aforementioned link help me understand it better. I was essentially connecting 5V, DIO2, and a resistor (already to gnd) to gnd all in parallel when I depressed the button. Push Button/Tactile Switch Pinout Connections, Uses, Dimensions & Datasheet

circuit(1).png

Here's my code

const int wire = 5;
bool state= false ;
int cycle=0;
bool prevState=false;

void setup() {
    pinMode(2,INPUT);
    pinMode(wire, OUTPUT);
    analogWrite(9,255);

    Serial.begin(9600);
}
 
void loop() {
  state = digitalRead(2);
  if(input==LOW)
  {
    if(state!=prevState)
    {
      Serial.println("Button Pressed");
      cycle=cycle+1;
      if(cycle%2)
      {
        analogWrite(wire,255);
      }
      else
      {
        analogWrite(wire,0);
      }
      if(cycle>9)
      {
        cycle=0;
      }
    }
  }  
  prevState=state;

High current draw thru the 100 ohm resistor takes down the 5V line, thus reset.

Do instead: Connect the button between D2 and Gnd.
Change this line: pinMode(2,INPUT);
to pinMode(2,INPUT_PULLUP);
Now the internal pullup will hold the pin at the HIGH level.

Remove the 100 ohm from the circuit.

CrossRoads:
High current draw thru the 100 ohm resistor takes down the 5V line, thus reset.

Do instead: Connect the button between D2 and Gnd.
Change this line: pinMode(2,INPUT);
to pinMode(2,INPUT_PULLUP);
Now the internal pullup will hold the pin at the HIGH level.

Remove the 100 ohm from the circuit.

My button is normally closed though, so arent I just then directly connecting 5V to ground if I remove the resistor. I tired doing what you mentioned to no success.
I see that the rated input into the i/o is 50 mA (0.05A). I replaced the 100 ohm resistor with a 100K resister, but it still causes it to reset for some reason. Any idea why?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.