1
\$\begingroup\$

I am writing code in Energia using the mso430g2553 launchpad and an external pushbutton with a pullup resistor. The code should simply display the state of the button on the serial monitor, 1 for pressed, 0 for unpressed. This is the code I am using:

int pushButton = 5;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
// NOTE this is different from the on-board pushbutton
pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {

// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1);        // delay in between reads for stability
}  

This is the circuit configuration I am using:

enter image description here

My problem is that when I run the program, I always get 0 no matter if the button is pressed or not. It seems like the program is not reading the input from the button, or I connected the button wrong way. However I can't seem to find the problem, I tried turning the button for 90 degrees and I connected opposite legs of the button to ground and to the pin. Would a solution be to add a debouncing capacitor maybe? Any help would be much appreciated!

\$\endgroup\$
  • \$\begingroup\$ How does int pushButton = 5; map your switch to hardware pin P1.3? \$\endgroup\$ – Brian Drummond Feb 16 '16 at 13:02
  • \$\begingroup\$ According to the MSP430g2553 pinout ( energia.nu/img/LaunchPadMSP430G2452-v1.5.jpg ), Pin 5 corresponds to P1.3 hence I used that notation. \$\endgroup\$ – Maria Feb 16 '16 at 13:58
  • \$\begingroup\$ So you did not make any changes from the tutorial? Show a photo of your circuit. \$\endgroup\$ – CL. Feb 16 '16 at 14:05
  • \$\begingroup\$ No I didn't make changes from the tutorial, I used the same circuit with 10k resistor and the same code for using external push button. Yet I can't seem to get it working, it prints just one value or it works correctly and then randomly stops working and prints just one value. \$\endgroup\$ – Maria Feb 16 '16 at 14:13
  • 2
    \$\begingroup\$ Have you got the button the correct way around? They have 4 pins, and if you happen to connect it at 90 degrees from where it should be you will find the input is shorted to ground. \$\endgroup\$ – Tom Carpenter Feb 16 '16 at 15:37
1
\$\begingroup\$

Per OP in comments, as questioned by @TomCarpenter, the issue was the orientation of the 4 pin tact switch OP was using. The switched connection was not being used, tying P1.3 to ground permanently.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.