MSP430 Launchpad: Using Buttons

Posted by on December 21, 2010


Sooner or later you’ll want a user interface, whether you’re working on a game or just trying to figure things out one of the simplest methods for human interaction would be a button. We’ll discuss how to setup and read input bits in the following paragraphs.

The code:

// MSP430 Launchpad - Blink onboard LED using onboard Switch (SW1).
//	GuShH -

#include  "msp430x20x2.h" // Include the necessary header for our target MCU

void delay_ms(unsigned int ms ){ 		// Not really a milliseconds delay!
      unsigned int i;
      for (i = 0; i < = ms; i++){ 		// The operator is <=, I've yet to fix this site bug.

void do_led( int led, int delay ) {
	P1OUT = led;
	delay_ms( delay );

void main(void) {

	WDTCTL = WDTPW + WDTHOLD;		// Hold the WDT (WatchDog Timer)
	P1DIR |= BIT0|BIT6;       		// Setup the appropriate output bits	

	while(1) {

		if ( !(P1IN & BIT3) ){		// If not (port one input AND bit3 (sw1))
			do_led( BIT6, 2000 );	// Turn on LED 2
		}else{				// Otherwise...
			do_led( BIT0, 100 );	// Turn on LED 1



The quick explanation:

We'll deal with inputs very much the same way we would with an output, except we're reading instead of writing. Notice how we define the bits we want as "high" on P1OUT to light up our LEDs, provided the bits are set on P1DIR to "high" then those bits will act as outputs and every time P1OUT has one of these bits "high", your outputs will also go "high".

Likewise with our inputs, if P1DIR is set "low" on our target bit then all we have to do is read P1IN to know whether a button is being held down or not. Simple eh?

To recap: On port direction bits low = Inputs, bits high = Outputs

A word on de-bouncing:

For those of you unaware of switch bounce, I would like to give you a simple explanation of what it is: whenever you actuate a switch it's contacts bounce back and forth very rapidly as the metal vibrates after it's been bent and pushed onto the opposite contact. This poses a problem with false triggering; You may press a button once but the software may detect 3 or more presses.

Notice how we don't perform any sort of de-bouncing in the code, this is for simplicity and in the future we will tackle this, but for now we're keeping it simple and down to the basics.

One simple way to de-bounce in software is to sample the input multiple times, hardware de-bounce is often proffered but it comes with it's costs. For cheap consumer products more often than not this action is carried in software.

Where do we go from here?

Now that you can manipulate both outputs and inputs; it is time to create something useful. In the next lesson we'll design a full blown bicycle safety light with what we've learnt!

For the next lesson you'll need:

  • 3x General Purpose NPN Transistor ( PN2222, BC337, 3904, etc. Almost any will do )
  • 3x 4k7 Ohm 1/4W Resistors
  • 3x 100 1/4W Resistors ( Could be up to 470 Ohms, or even 1K if high brightness LEDs are used )
  • 1x 10K Ohms 1/4W Resistor (Optional)
  • 1x Tact Switch (Optional)
  • 3x 5mm or 3mm LEDs, Could be all of the same colour or one Red, Green and Blue. Your choice.
  • 1x Breadboard or any other prototyping method of your choice.

Something to download:

The "Using Buttons" project files.

Stay tuned.

6 Comments on MSP430 Launchpad: Using Buttons


  1. Strynox says:

    hey long time no see
    i ordered mine from digikey and got it surprisingly fast!

    im trying to use hardware interrupts but im finding the concept hard to grasp are you going to write about interrupts soon?

    do you know if theres any code libraries for the launchpad like the arduino has?

    i cant find support and the ti wiki is not very useful

    • GuShH says:

      What’s up… Did you ever get the limiter working properly?
      As for the interrupts, it’s planned. Once the basics are covered I’ll go about proper coding techniques, concepts, etc.

      The TI Wiki is meh. You could visit the 43oh site if you want or the msp430 IRC channel at Freenode. There are at least two high level libs that I’m aware of but they’re both in alpha stages and I would dare say they’re rather unusable.

      Anyway what exactly are you finding hard to grasp regarding interrupts?, is it the concept or the execution?, are you using CCS?

      Let me know…

      • Strynox says:

        well i dont understand the constants the syntax etc. they dont have libraries like the arduino does!!

        i replaced the transistors on the limiter. it works fine.

        do you know where to find libraries for the launchpad?

        • GuShH says:

          As far as libraries go there’s MSPHere (don’t have an address right now, you can Google it) — sadly it’s not complete.

          Which syntax and which constants you don’t understand?, Take a look at the TI Wiki pages, there is some good material there. Otherwise just ask and if I know the answer I’ll be glad to help you.


  2. Daniel says:

    Where is the __delay_cycles() contained?

    • GuShH says:

      Intrinsic functions are more often than not part of the compiler itself, in this case the compiler needs to generate code to spend exactly the given amount of cycles; nothing more and nothing less. For this it has to have an intimate knowledge of how the system works and how to map and inline the instructions appropriately, this is the reason why you can only use a constant value and why you may not be able to find an implementation of the call, even though there may be a header declaring them for documentation purposes.
      The way the call is used in the example is merely to provide somehow of a delay, the timing does not matter for the purpose of the example and thus we didn’t bother to take the processor speed into consideration nor did we calibrate for process variance either. If you need accurate and responsive input I suggest you use interrupts with de-bouncing either in hardware, software or both.

      I have quite a few examples on using the hardware functions of these micro-controllers, however what really kills me is the time it takes to prepare each entry, making sure it all works and that it’s all accurate… These were however part of a beginners series to the msp430 (which I’ve yet to complete!) hence the inherit simplicity.