1

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!

Maria
  • 11
  • 2
  • How does `int pushButton = 5;` map your switch to hardware pin P1.3? –  Feb 16 '16 at 13:02
  • According to the MSP430g2553 pinout ( http://energia.nu/img/LaunchPadMSP430G2452-v1.5.jpg ), Pin 5 corresponds to P1.3 hence I used that notation. – Maria Feb 16 '16 at 13:58
  • So you did not make any changes from [the tutorial](http://energia.nu/Tutorial_DigitalReadSerial.html)? Show a photo of your circuit. – CL. Feb 16 '16 at 14:05
  • 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. – Maria Feb 16 '16 at 14:13
  • Well from https://github.com/energia/Energia/blob/master/hardware/msp430/variants/launchpad/pins_energia.h ... `static const uint8_t P1_3 = 5;`so the pin looks OK. Measure actual voltages on that pin next... –  Feb 16 '16 at 14:14
  • 2
    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. – Tom Carpenter Feb 16 '16 at 15:37
  • The problem was the button orientation, now it works correctly! Thank you for the help! – Maria Feb 17 '16 at 10:35

1 Answers1

1

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.

Passerby
  • 72,580
  • 7
  • 90
  • 202