Why does my pushbutton never work?

LED still not comin on, even after push button is HIGH

dis is d code below:

int button;
void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(12, OUTPUT);
}

void loop()
{
  button = digitalRead(2);
  if(button = HIGH){
  digitalWrite(12, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  }else {
  digitalWrite(12, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}
}
if(button = HIGH)

Use = to assign a value to a variable
Use == to test the value of a variable

exactly wat i did initially. it didnt work, just did now, its not workin

Pay close attention to how the switch in the below is wired. How is yours different?

image
dis is d image you sent


dis is my current circuit

dis is d current code

int button;
void setup()
{
  pinMode(4, INPUT);
  pinMode(12, OUTPUT);
}

void loop()
{
  button = digitalRead(4);
  if(button == HIGH){
  digitalWrite(12, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  }else {
  digitalWrite(12, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}
}


it still didnt work
its fraustratin me :dizzy_face:

Is that actually the type of breadboard that you have or do the long power rails have breaks in them indicated by breaks in the red and black lines alongside them ?

i dont get it. please, can you explain?

Please post a photo of your project that clearly shows the markings on your breadboard

Some breadboards have gaps in the long power rails along their edges. You may have one of these


d current photo of my breadboard

With respect, that is not a 'photo of your breadboard,

im sorry i dont understand

Please take a 'photo using a camera, the one on your 'phone perhaps, and post the 'photo in a new reply

your LED may be mounted wrong.
Invert the LED and test.

Your code reflects using an internal pullup resistor but your drawing reflects and external pulldown resistor. Assuming your switch has correct orientation what were you expecting to happen? Next your drawing is exactly what I see here. This is why you have been asked for an actual picture of your wiring.

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground through 220 ohm resistor
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 13;    // the number of the LED pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

That is the code which goes with the picture you posted. Try the code sample that is associated with the layout you posted and again make sure the button orientation is correct.

Ton

Two suggestions:

  1. Use the builtin LED. To do this only change the LED PIN number to 13 in your sketch and run it again. If you see the orange LED in the Arduino flashing then check the orientation of the external LED. Some breadboards have a disconnect between their two halves in the side rails. Make sure that both the button and the LED are connected using in the same half. Check back with pin 12. Try with another LED. Make sure you are using a resistor in the order of a couple hundred ohms to 1 K

Another shot in dark → Or the pushbutton should be "turned" (90deg plus or minus).

It still didn't work.
I don't know why. Even when u follow the exact steps in videos, it won't work

The page I linked to and provided a code sample from is not a video so I have no clue what video(s) you are talking about.

Next if you have a DMM (Meter) you should be able to look at your input pin and see changes on button press. Finally as mentioned is your LED configured correct as to anode and cathode? Does your code load error free? Again why are you using internal pullup when your diagram shows an external pullup? This should be a simple exercise.

Ron

I'm gonna laugh so hard when the circuit is only in Tinkercad and not in real life.

Tinkercad is buggy. Sometimes it helps to delete the controller and re-integrate it, re-route the lines and copy the saved programme back into the code area.