Digital Read Serial

This example shows you how to monitor the state of a switch by establishing serial communication between your LaunchPad and your computer over USB.

Hardware Required

  • MSP-EXP430G2 LaunchPad

  • A momentary switch, button, or toggle switch

  • 10k ohm resistor

  • Breadboard

  • Hook-up wire

Relevant Groundwork

Any input pin can be used to connect to an external pushbutton. To see what pins support external pushbuttons, refer to the hardware pin mapping guide.

For on-board pushbutton:

You can use the pushbutton on the LaunchPad to complete this exercise.

For an external pushbutton:

Connect three wires to the LaunchPad board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 3 volt supply and ground. The third wire goes from digital pin 5 (P1.3) to one leg of the pushbutton. That same leg of the button connects through a pull-up resistor (here 10 KOhms) to VCC (~3V). The other leg of the button connects to ground.

Pushbuttons 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 pushbutton, 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.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is ''floating'': that is, it doesn’t have a solid connection to voltage or ground, and it will randomly return either HIGH or LOW. That’s why you need a pull-up resistor in the circuit.

Circuit

DigitalReadSerial bb

Image developed using Fritzing. For more circuit examples, see the Fritzing project page.

Schematic

DigitalReadSerial schem

Code Explanation

In the program below, the very first thing that you do will in the setup function is to begin serial communications, at 9600 bits of data per second, between your LaunchPad and your computer with the line:

Serial.begin(9600);

Next, initialize digital pin 5, the pin that will read the output from your button, as an input:

pinMode(5,INPUT_PULLUP); //For on-board we need to enable internal pullup resistor
pinMode(5,INPUT); //For external pushbutton we need to use an external pullup resistor

Now that your setup has been completed, move into the main loop of your code. When your button is pressed, current will freely flow through your circuit, and when it is not pressed, the input pin will be connected to ground through the 10-kilohm resistor. This is a digital input, meaning that the switch can only be in either an on state (seen by your LaunchPad as a ''1'', or HIGH) or an off state (seen by your LaunchPad as a ''0'', or LOW), with nothing in between.

The first thing you need to do in the main loop of your program is to establish a variable to hold the information coming in from your switch. Since the information coming in from the switch will be either a ''1'' or a ''0'', you can use an int datatype. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 5. You can accomplish all this with just one line of code.

int sensorValue = digitalRead(5);

Once the LaunchPad has read the input, make it print this information back to the computer as a decimal value. You can do this with the command Serial.println() in our last line of code:

Serial.println(sensorValue);

Now, when you open your Serial Monitor in the Energia environment, you will see a stream of ''1s'' if your switch is open, or ''0s'' if your switch is closed.

Code

Code Example 1: With on-board pushbutton

/*
  DigitalReadSerial with on-board Pushbutton
  Reads a digital input on pin 5, prints the result to the serial monitor

  This example code is in the public domain.
*/

// digital pin 5 has a pushbutton attached to it. Give it a name:
int pushButton = 5;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the on-board pushbutton's pin an input pullup:
  pinMode(pushButton, INPUT_PULLUP);
}

// 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
}

Code Example 2: With external pushbutton

/*
  DigitalReadSerial with External Pushbutton
 Reads a digital input on pin 5, prints the result to the serial monitor

 This example code is in the public domain.
 */

// digital pin 5 has a pushbutton attached to it. Give it a name:
int pushButton = 5;

// the setup routine runs once when you press reset:
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
}

Working Video

Try it out

See Also

Guide Home