LED with button

Hi. My Arduino kit recently came in so I am obviously a beginner but I am trying to control an LED with a button using this code below, but I can’t actually execute turning on the LED. But to the point, if anyone can diagram a good way to set it up on a breadboard I’d really appreciate it or if you could post a link to something good. THANK YOU!

ETHAN :slight_smile: :slight_smile:

const int LED = 12;
const int button = 4;
void setup() {

  pinMode(LED, OUTPUT);
  pinMode(button, INPUT);
}

void loop()
{
  if (digitalRead(button) == HIGH)
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
}
/code]

Button & LED

Thank you @ApexMOEng ! The real thing was how to connect them for I had looked at that. Is there a tutorial on arduino.cc for that? Thx Ethan

Those tutorials are somewhat unhelpful.

There is a LED already on pin 13, so we needn't worry about that too much.

Just connect your button between port 4 and ground. Note that if you have a "Tact" switch as shown, it may not be obvious as to which connections are which, so connect your wires to diagonally opposite pins. You do not need a resistor for the button.

Then run this code:

const int LED = 13;
const int button = 4;
void setup() {

  pinMode(LED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(button) == HIGH)
  {
    digitalWrite(LED, LOW);
  }
  else
  {
    digitalWrite(LED, HIGH);
  }
  delay(50)
}

This should control the LED on the board. If your LED and resistor are connected to port 13 (not 12), it should also operate.

So all I need is two wires, one from gnd(will any of the uno's three work?) to the button and another from pin 4 to the button. Where would the resistor go and what type? THANKS, Ethan :) :) :)

All three ground pins are the same connection.

What resistor? You need a 330 ohm resistor in series with your LED.

Oh, but if I am just using the built int LED, there would be no need for a resistor? THANKS!

ETHAN :) :) :)

So - is it working or not?

YES! Thank you! @Paul__B At first I had to change one thing that didn't affect the code but then I had to switch two things around but after like a minute I got it to work! Thank you,

Ethan:) :) :)