Home TI-Nspire Scripting HQ STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 3

 

Lesson 3: Getting Serial - writing to and from our board

Energia Reference page

     
           
     

Communication - in Both Directions!

The serial port is a great way to communicate with your board, and to receive feedback from your sketch. The standard serial port for the LaunchPad is the USB port, and Energia offers a "serial monitor" for watching what is happening. Initialising the serial port is easy - just add Serial.begin() to your setup routine, and give a baud speed (9600 seems fairly standard, and for Energia the fastest is 115200).

Let's add serial support to a simple button and LED sketch.

int buttonState = 0;

void setup()
{

pinMode(RED_LED, OUTPUT);
pinMode(PUSH1, INPUT_PULLUP);
Serial.begin(9600); }

void loop()
{
buttonState = digitalRead(PUSH1);

if (buttonState == LOW) {
digitalWrite(RED_LED, HIGH);
Serial.println("Button 1");
Serial.println("Red LED");
}
else {
digitalWrite(RED_LED, LOW);
}
}

Try changing the baud rate from 9600 to 115200: what do you notice? (You will need to change the dropdown menu at the bottom right of the serial monitor window to the correct rate). The response time should be faster, but otherwise all still works.

Change Serial.println to just Serial.print - "println" forces a new line for each statement. "print" runs the strings together.

So in this way, we can use the Serial port to give us a running commentary on our sketch - very useful when debugging and trying new things!

  
  

Writing TO the board

Now things can really start to get interesting: controlling the board with commands is a key element in all future work. The Energia Serial Monitor comes with an entry line at the top - here you can type text which will be sent to the connected board.

In our "challenge" sketch from the previous lesson we ran through the colours, red -> blue -> green by pressing push button 1, and the reverse by pressing button 2. In our next sketch, we achieve the same result by simply entering "1" or "2" in the serial monitor window.

The fun happens in the loop: first define a character variable (here called "recvChar") - serial communication works by sending a series of string characters. Next, we apply a test - if Serial is available, then read the serial port into the recvChar variable, and print it to the serial monitor so that you can see when this happens.

Now an important new feature: "switch". Similar to an "if .. then .. else" loop, study this structure and see how it works. Each "case" elicits an action. So typing "1" or "2" and pressing enter simulates pressing button 1 or button 2. Easy! Then just adding a few Serial.println statements to watch what is happening, and we have established simple two-way communication between our board and the serial monitor.

  
  

In the same way, we can define new actions: for example, try adding a command such as "r" to turn the red LED on, "g" for green and "b" for blue. Then perhaps a "0" to turn all LEDs off.

  
  

  
  

MSP430 sketch

int buttonState1 = 0;
int buttonState2 = 0;

void setup()
{

pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);

pinMode(PUSH1, INPUT_PULLUP);
pinMode(PUSH2, INPUT_PULLUP);

Serial.begin(115200);
}

void loop()
{
buttonState1 = digitalRead(PUSH1);
buttonState2 = digitalRead(PUSH2);

char recvChar;

if(Serial.available()){
recvChar = Serial.read();
Serial.println(recvChar);
}

switch(recvChar) {

case '1':

buttonState1 = LOW;
break;

case '2':

buttonState2 = LOW;
break;

}

if (buttonState1 == LOW) {

digitalWrite(RED_LED, HIGH);
Serial.println("Red LED ON");
delay(1000);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
Serial.println("Green LED ON");
delay(1000);
digitalWrite(GREEN_LED, LOW);
}

if (buttonState2 == LOW) {

digitalWrite(GREEN_LED, HIGH);
Serial.println("Green LED ON");
delay(1000);
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
Serial.println("Red LED ON");
delay(1000);
digitalWrite(RED_LED, LOW);
delay(1000);
}

}

  

Hacking the Hub™

In order to send data via the serial port to the Hub, some jumper caps need to be added to the pins in the centre of the board as shown. The standard MSP432 comes with 4 of the central 6 pins covered by jumper caps, whereas all jumper caps have been removed from the Innovator Hub central pins, and you will need to source some more to cover four pins (you can leave the two middle pins) if you wish to send commands via the serial monitor. If you are unable to do this, continue to lesson 5, connect a BLE module and use this to communicate with your hub!

You might also try using a PUSH button to serve as the trigger for this activity!

Remember: You can always reflash your Innovator Hub to return it to its original state, using the tools that TI has made available (just follow the Resources tab, and then "Keep your Innovator up to date"). So it is possible to explore coding the Hub in new ways!

  

  
  

Back to Top

  

Home TI-Nspire Scripting HQ STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 3