Tag: User Interface

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 - info@gushh.net

#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.