Why does my button, wired to 5V, GND, and digital port, turn off Elegoo Uno R3 WHen pressed

When I click a button on my breadboard, it turns off the Elegoo for some reason. I have a video I will attach in a comment. IT also doesnt do its proper function

const char bluePin = 9; // The pin of the blue LED
const char greenPin = 2; // The pin of the green LED

const char blueButtonPin = 11; // The pin of the button for team blue
const char greenButtonPin = 12; // The pin of the button for team green

const char redPins[] = {3,4,5,6,7,8}; // Each red pin, going from green to blue

void setup() 
{
  Serial.begin(9600);
  
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  
  pinMode(blueButtonPin, INPUT_PULLUP);
  pinMode(greenButtonPin, INPUT_PULLUP);

  for(int redPin = 0; redPin < sizeof(redPins)/sizeof(*redPins); redPin++) // For each red pin
  {
    pinMode(redPin, OUTPUT);
  }
}

void loop() 
{
  int blueButtonValue = digitalRead(blueButtonPin);
  Serial.println(blueButtonValue);
  if(blueButtonValue == HIGH)
  {
    Serial.println("Blue Button Pressed");
    digitalWrite(bluePin, HIGH);
  }
}

Just connect it to the ground, not the 5v too. You are probably grounding out all the power you are supplying to the Arduino if you don't have a resistor on it. Pin -> button -> ground

Ok that worked but now it doesn't turn on the led I asked it to. I am sending a picture of the setup

I'm new to this too, so forgive me if I'm wrong, but I believe your button will read high when not pressed, and low when pressed, so to read when it is pressed you want this:

if(blueButtonValue == LOW)

That didn't seem to work

I made a tinkercad design

of it if it makes it easier

Welcome to the group.

FYI


You should get into the habit of using correct data types.
const byte bluePin = 9;


Use diagonal terminals on those switches.


You only have one wire on the right hand switch.

Sorry, Im kinda new to this. What exactly does that mean. Google didn't show good results for "Diagonal Terminal"

On a four pin button the terminals diagonal to each other will never be connected without pressing the button, whereas the neighboring ones sometimes are. If that makes sense.

1 Like

Wire to the diagonal terminals on those square switches

image

I don't think I understand but atleast it works. Thanks. But now it is doing the if function, but not turning on the led?

What gets printed on your serial monitor ?


Confirm the flat side of the LEDs is connected to ground (0V).

No errors, just what I made it print when I click. I am rewiring right now to see if it is an error in wiring

Repeating what was mentioned:

Confirm the flat side of the LEDs is connected to ground (0V).


Also, if your switch is large enough straddle the centre of the breadboard with it.

I trimmed down the code and hardware to just what we are dealing with now and this works. It starts with the LED off, and it turns on once pressed.

const byte bluePin = 9; // The pin of the blue LED

const byte blueButtonPin = 11; // The pin of the button for team blue

void setup() {
  Serial.begin(9600);

  pinMode(bluePin, OUTPUT);
  pinMode(blueButtonPin, INPUT_PULLUP);
}

void loop() {
  int blueButtonValue = digitalRead(blueButtonPin);
  Serial.println(blueButtonValue);
  if (blueButtonValue == LOW)
  {
    Serial.println("Blue Button Pressed");
    digitalWrite(bluePin, HIGH);
  }
}

The error was just something not plugged in right. I think the conductor. The switch is large enough but what is the positive in doing so?

Wire switches similar as seen here.

It removes the possibility that the switch is shorting itself out. I believe that is all.

Two terminals (on each side) of the switch are internally connected together.

Diagonal wiring connections makes sure you are using the proper legs/terminals.