Button input always reading HIGH

hello, I am currently having trouble with another seemingly simple project.
my project is supposed work a relay at pin 8 which is activated by a pushbutton at pin 13 with a 1k resistor (formerly a 10) . after 1 hour and several youtube tutorials, I have found that everything is wired correctly, the problem is the button is always reading high,( and it surprisingly isn't bounce),keeping the relay permanently on. here is my code

int relay=8;
int button=13;
int buttonread;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(button,INPUT);
pinMode(relay,OUTPUT);
}

void loop() {
delay(10);
buttonread= digitalRead(button);
// put your main code here, to run repeatedly:

if (buttonread=1){
Serial.println(buttonread);

digitalWrite(relay,HIGH);}
else{
Serial.println(buttonread);

digitalWrite(relay,LOW);

}}

thank you all for your time

Try...

if (buttonread == 1)

same result as before

try...

pinMode(button,INPUT_PULLUP);

Post your new code

what does it do? I'm curious. it worked

Words are pretty useless at describing circuits

ahahaha (I am laughing)
we all made this same mistake !

you must put a pullup resistor on any input pin, when your button is connected to ground (0v); you can put a real resistor, or the internal one, as the instruction INPUT_PULLUP does

attention, pin 13 is used as an OUTPUT for the led on the circuit ; prefer another pin to connect your button

red_car gave the first part of the solution, too!

Ah, but you see he is probably using a UNO since that is what the Arduino project wishes to sell you as its generally inconvenient and impractical "flagship" product. :roll_eyes:

In which case pin 13 is either INPUT or OUTPUT and the op-amp does not interfere with the operation in either case. If not connected and not otherwise specified in the code, it is common to notice the LED randomly flickering.

Yes, I know. But for those who are new to the uno, it is better to get into the habit of keeping this pin to use the led as a means of testing a program under development, for example. That's what I usually do.

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