How to use Input output pins of MSP430G2 LaunchPad

In this tutorial, you will learn how to use input output ports of MSP430G2553 microcontroller which comes with MSP430G2 launchPad and how to declare them as a output and input in energia IDE. MSP430 microcontroller series usually has 8 bit ports numbered as Port 1, Port2, Port3 etc. MSP430G2553 microcontroller has only two input output ports  Ports 1 and PORT 2. As shown in pinout of MSP43052553 microcontroller each port is of 8 bits. Its mean each port has 8 input output pins and each pin has multiple functionalities which I will discuss in later tutorials.  In this  msp430g2 launchpad tutorial,  I will discuss how to use them as a digital output and digital input. For example if you want to blink an LED, GPIO pin will be used as a digital output and if you want to connect push button with input output pin, it will be used as a digital input.  I have already discussed the pin mapping of MSP43052553 microcontroller with MSP43052 lanuchpad in last tutorial on MSP430G2 launchPad.

MSP430G2553 microcontroller pin out

As discussed earlier, MSP43052553 microcontroller  has two 8 bit ports Port1 and Port 2 . The pins P2.6 and P2.7 of Port 2 are also used for connecting the 32Khz crystal oscillator, So if you are using an external crystal, pins P2.6 and P2.7 will become unavailable and you cannot use them as a digital input or output.  So now lets see how to use GPIOs or general purpose input outputs pins of MSP430G2 launch pad.

Digital Pins on MSP430G2 LaunchPad

The pins on the MSP430G2 LaunchPad can be configured  either inputs or outputs Now I will explains the functioning of the pins in those modes. Digital pins can be used either as INPUT or OUTPUT. Changing a pin from INPUT TO OUTPUT with pinMode() drastically changes the electrical behavior of the pin. So now lets see how declare these pins either as a input or output using Energia IDE. If you don’t know how to use Energia IDE. I recommend you to use fist tutorial on MSP430G2 LaunchPad.

How to declare I/O pins of MSP430G2 LaunchPad as an input

As you already know pinMode function is used to declare I/O pins either as input or output. pinmMode function accepts two argument one is for pin number and other is either to declare pin as input or output.  For example this function pinMode in this line will declare the  pin number two as a digital input

pinMOde( 2  , INPUT);

How to declare I/O pins of  MSP430G2 LaunchPad as an output

Now lets see how to declare digital input output pins as a digital output. We can use same function to declare these pin as a digital output. In this line function pinMode will declare the  pin number three which is P1.1 as a digital output. Similarly, you can declare other pins of MSP430G2 as a digital output pins.

pinMOde( 3  , OUTPUT);

So now lets take two examples to see How to use these pins as digital output for example LED blinking and second example of push button interfacing with MSP430G2 launchPad where it Input output pins will be used as a digital input.

LED Blinking using MSP430G2 LaunchPad 

Now I will show you, how to program MSP430 launchpad to blink an LED using Energia IDE.  As explained earlier MSP430G2 launchpad have 16 GPIO pins and we can use any pin as a digital output pin.  pinmode function is used to initialize GPIO pins either as an input or output.  First argument to the pinMode function is a pin number to which we want to declare as a digital output and second argument is type either OUTPUT or INPUT. Because same function is used to declare I/O pins as a output or input.

pinMode( Pin_number, OUTPUT);

We initialize everything inside a setup function. DigitalWrite function is used to make pin low or high and it is always written inside the loop function because we want to perform this action again and again. This line will make the pin number 14 high.

digitalWrite(14, HIGH);

This line will make the pin number 14 low

digitalWrite(RED_LINE, LOW);

Now lets see how to make circuit diagram of LED blinking program we will need one LED and one 220 ohm resistor. Now pause the reading and Make the circuit connections according to this circuit diagram.LED blinking using MSP430G2 LaunchPad

In this circuit we are using pin number 14 of MSP430 launchpad to blink an LED. Connect pin number 14 of MSP430 with positive leg of LED and through 220ohm resistor. Here resistor is used to limit the current. Because the maximum current any pin of MSP430 can provide up to 40mA so it is used for protection.

How to write code of LED blinking using MSP430G2 launchPad

Now lets see how to write code for LED blinking and how code works. In the program everything starts with double back slash are comments and just for readability of the code.  In the program , the first thing you do is to initialize the pin to which led is connected. This line assign a name to pin number 14 of MSP430 “ LEDPIN”

Int ledpin=14;

This line initialize the led pin as a output pin

pinMode(ledpin, OUTPUT);

In the main loop, you turn the LED on with the line:

digitalWrite(ledpin, HIGH);

This supplies 3 volts to pin 14. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line:

digitalWrite(RED_LINE, LOW);

That takes pin 14 back to 0 volts, and turns the LED off. In between the on and the off, you want enough time for a person to see the change, so the delay() commands tell the MSP430 to do nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else happens for that amount of time.

int ledpin=14; // assigning a name to pin number 14

// the setup routine runs once when you press reset:
void setup() 
{ 
// initialize the digital pin as an output.
pinMode(ledpin, OUTPUT); 
}

// the loop routine runs over and over again forever:
void loop() 
{
digitalWrite(ledpin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledpin, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

How to use push button with MSP430G2 Launchpad

So now in this tutorial,  you will learn how to use push button with MSP430G2 Launchpad, I will show you how to use push button to turn on LED by switching a push button. So in this tutorial, you will also use the concept of last section on LED blinking in which we saw how to use GPIO pins of MSP430G2 Launchpad as a digital output.  In this section, we will see how to use GPIO pins of MSP430G2 Launchpad as a digital input and digital output.

Now let’s see how to connect push button with MSP430G2 Launchpad. We need to use a pull up resistor or pulldown resistor to connect push button with MSP430.  Any GPIO pin can be connected with push button, in this tutorial I am using pin number 5 to connect a push button and pin number 14 to connect an LED as we seen this in last video.pull up and pull down resistor

Now Lets see how to make a circuit of push button interfacing with MSP430. Now pause to read and make connections according to this circuit diagram.push button interfacing with MSP430G2 launchPad

In this circuit diagram push button one terminal is connected with resistor and other terminal of resistor is connected with 3.3 volt.  Same terminal of push button is connected with pin number 5 of MSP430.  Other terminal of push button is connected with o volt or Ground.

Push buttons or switches connect two points in a circuit when you press them. When the pushbutton is open (unpressed) there is no connection between the two legs of the push button, so the pin is connected to VCC (through the pull-up resistor) and reads as HIGH, or 1. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to ground, so that the pin reads as LOW, or 0. So when the push button is not pressed, we will be getting 3.3 volt or logic high signal on pin number 14 through push pull resistor and when push button is pressed, we will get logic low signal on pin number 14. Also LED is connect with pin number 14 through a current limiting resistor.

How to write code for Push button interfacing with MSP430G2 LaunchPad

In first line we have give name to pin number 5 as push button now instead of pin number 5  we will using pushbutton through the program and similarly ledpin name is given to pin number 14. Another integer variable button state is declared which is used to store the state of push button. Inside the setup function we have initialized pushbutton as an input and ledpin as an output through pin mode function.

Inside the loop function we have new function digitalRead(); Digital read function accepts one arugment as a parameter which is being initialized as a input. In this example we digital read function read the state of push button and stores its state in button state variable. Output of digital read function will be either logic High or logic low.  When the push button is open, we will get logic high and when the push button is close we will get logic low .

After that button state is used to turn on or turn off LED.

int pushButton = 5;  // digital pin 14 has a pushbutton attached to it

int ledpin  = 14;     // digital pin 15 has a pushbutton attached to it

int buttonState;      // this variable is used to store the state of button either low or high

// the setup routine runs once when you press reset:

void setup()

{

  pinMode(pushButton, INPUT); // make the pushbutton's pin an input

  pinMode(ledpin, OUTPUT); // make the lepin as a output

}

void loop()

{

  // read the input pin:

  buttonState = digitalRead(pushButton);

  if( buttonState==LOW)

  digitalWrite(ledpin, HIGH);

  else

  digitalWrite(ledpin,LOW);

}

Now upload the code to MSP430 by clicking on upload button and you can see as soon as I pressed the push button LED turn on and as soon as I left the push button LED turns off. So this is how we can use GPIO pins of MSP430 as a digital input or output. GPIO pins has many applications like seven segment control, LCD interfacing, keypad interfacing and many others.

Leave a Comment