0

Wiring Diagram

I have a simple Arduino Project, 2 buttons and face a strange case that in the beginning status of buttons are 0, but after click on the button and release the status becomes 1 for long a time then back to 0, please what is the wrong??

Code:

int const BTN1_PIN=2;
int const BTN2_PIN=4;

void setup(){
   pinMode(BTN1_PIN, INPUT);
   pinMode(BTN2_PIN, INPUT);
   Serial.begin(9600);
}

void loop(){
   int status1=digitalRead(BTN1_PIN);
   Serial.print("BTN1 Status :");
   Serial.println(status1);


   int status2=digitalRead(BTN2_PIN);
   Serial.print("BTN2 Status :");
   Serial.println(status2);

 delay(250);
}

in the begining, the values is :
BTN1 Status :0
BTN2 Status :0
.
.

But after click on the button1 and release the status of button1 take long time to back to 0, the output like:
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :1
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0
BTN1 Status :0
BTN2 Status :0

5
  • now i can not take photo since i out of lap, but is there any error in the code?? Commented Sep 4, 2014 at 8:02
  • what does readDigit() look like ? Is that simple digitalRead() ? Your ground doesn't seem to be connected to anything. Also, have you tried using INPUT_PULLUP (e.g. pinMode(BTN1_PIN, INPUT_PULLUP);) ? Commented Sep 4, 2014 at 10:12
  • it's digitalRead but by mistake i write readDigit. Commented Sep 4, 2014 at 10:43
  • 1
    Read up on pull-up/pull-down resistors -- ladyada.net/learn/arduino/lesson5.html Commented Sep 4, 2014 at 20:19
  • The above link is useful ladyada.net/learn/arduino/lesson5.html thanks (Tim Cavanaugh), i have solved the issue, i have added other Resister 100oh between input pin (2 & GND) and also between input pin (4 & GND). Commented Sep 5, 2014 at 14:04

1 Answer 1

1

General pushbutton setup

The problem with your design is that, when no push button is pressed, your I/O pins are not connected to anything. This causes their values to kind of "float" around meaning they jump between 1 and 0. Usually you would connect the I/O pin directly to +5v via a high value resistor (i.e. 10K ohms) and then also connect the I/O pin to ground through a push button. This way, when you read the pin without the push button pressed, you get a solid +5v (and hardly any current because of the resistor), but when you press the button, you short to ground (through the resistor) and get a solid 0v. This gives you a very clean "on" and "off" where your pressed button state is 0v.

Arduinos are cool because they have these resistors to +5v built into the board itself. You just have to turn them on using pinMode(pinBUTTON, INPUT_PULLUP);. I have included a design of what your circuit layout should be above.

An important thing to remember with arduinos is you ALWAYS have to set your pinModes. This is an easy step to forget, and the arduino will "kind of" work without it, but it is a common source of odd results in your projects.

const int pinBUTTONONE = 2;
const int pinBUTTONTWO = 4;

void setup(){
  pinMode(pinBUTTONONE, INPUT_PULLUP);
  pinMode(pinBUTTONTWO, INPUT_PULLUP);
}

void setup(){
  if(digitalRead(pinBUTTONONE) == LOW){
    // Execute button one pressed code.
  }

  if(digitalRead(pinBUTTONTWO) == LOW){
    // Execute button two pressed code.
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.