Introduction: Using Python (pySerial) to Control an RGB LED on a MSP432 LaunchPad

About: Electronics Engineer | Always in the phase of continuous learning

This quick tutorial shows the simple operation of an RGB LED on a MSP432 LaunchPad. A Python GUI is created that allows you to control the RGB LED by clicking on individual buttons.

Step 1: Software - Energia IDE, PyCharm

Step 2: Hardware: MSP432P401R LaunchPad

Step 3: Energia IDE

Energia is an open-source electronics prototyping platform started by Robert Wessels in January of 2012 with the goal to bring the Wiring and Arduino framework to the Texas Instruments MSP430 based LaunchPad.

Connect the MSP432 LaunchPad to one of your computer’s USB ports and open Energia IDE.

Step 4: Select the Appropriate COM Port and Board.

Step 5: Upload the Below Program to the LaunchPad by Clicking on the Upload Button.

//MSP432P401R
//LaunchPad Development Kit
//LED Control

const int LED1 = 75; //RED
const int LED2 = 76; //GREEN
const int LED3 = 77; //BLUE

char incoming_data = '\0';
  
void setup() {
  Serial.begin(9600);  
  
  pinMode(LED1, OUTPUT);  
  pinMode(LED2, OUTPUT); 
  pinMode(LED3, OUTPUT);

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);  
}

// the loop routine runs over and over again forever:
void loop() {
  //check for incoming data
  if (Serial.available() > 0)
  {
    //read the incoming data
    incoming_data = Serial.read();
    
    if (incoming_data == 'r')
    {
      //RED LED - ON
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
    }
    else if (incoming_data == 'g')
    {
      //GREEN LED - ON
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, LOW);
    }
    else if (incoming_data == 'b')
    {
      //BLUE LED - ON
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, HIGH);
    }
    else if (incoming_data == 'm')
    {
      setColor(255, 165, 0); // Orange Color
      delay(1000);
      setColor(0, 255, 255);  // Aqua Color
      delay(1000);
      setColor(255, 255, 255); // White Color
      delay(1000);
      setColor(128, 0, 128); // Purple Color
      delay(1000);
    }
    else if (incoming_data == 'q')
    {
      //LEDs - OFF
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
    }
  delay(1000); // wait for a second
}
}

void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(LED1, redValue);
  analogWrite(LED2, greenValue);
  analogWrite(LED3, blueValue);
}

Step 6: PyCharm

Before running the program below, make sure that the pySerial package is installed. PySerial is a Python library which provides support for serial connections over a variety of different devices.

To install any package in PyCharm, follow the below steps:
1. File -> Settings.
2. Under Project, select Project Interpreter and click on the “+” icon.
3. In the search bar, type pySerial and click on Install Package.

NOTE:
Make sure the same COM port number is used in the Python program.

import serial
import time
import tkinter as tk
window = tk.Tk()
window.configure(background="gray14")
window.title("MSP432 - LED CTRL")
msp432 = serial.Serial('COM4', 9600)
def led_control():
    print(">>> LED CTRL - PROGRAM <<<\n")
    def green():
        print(">Green<")
        msp432.write(b'g')
    def red():
        print(">Red<")
        msp432.write(b'r')
    def blue():
        print(">Blue<")
        msp432.write(b'b')
    def multiblink():
        print(">Multi<")
        msp432.write(b'm')
    def quit():
        print("\n** END OF PROGRAM **")
        msp432.write(b'q')
        msp432.close()
        window.destroy()
    b1 = tk.Button(window, text="GREEN", command=green, bg="lime green", fg="gray7", font=("Comic Sans MS", 20))
    b2 = tk.Button(window, text="RED", command=red, bg="firebrick2", fg="ghost white", font=("Comic Sans MS", 20))
    b3 = tk.Button(window, text="BLUE", command=blue, bg="dodger blue", fg="ghost white", font=("Comic Sans MS", 20))
    b4 = tk.Button(window, text="MULTI", command=multiblink, bg="MediumOrchid3", fg="ghost white", font=("Comic Sans MS", 20))
    b5 = tk.Button(window, text="EXIT", command=quit, bg="gold", fg="gray7", font=("Comic Sans MS", 20))
    b1.grid(row=1, column=1, padx=5, pady=10)
    b2.grid(row=1, column=2, padx=5, pady=10)
    b3.grid(row=1, column=3, padx=5, pady=10)
    b4.grid(row=1, column=4, padx=5, pady=10)
    b5.grid(row=1, column=5, padx=5, pady=10)
    window.mainloop()
time.sleep(2)
led_control()

Step 7: GUI

A simple GUI opens with 5 buttons - GREEN, RED, BLUE, MULTI and EXIT.

The GUI allows you to control the RGB LED that is on your MSP432 LaunchPad. Change the color by clicking on the individual buttons. The EXIT button turns off the on-board RGB LED and ends the program execution.

References:

Energia IDE: https://energia.nu/

PyCharm: https://www.jetbrains.com/pycharm/

pySerial: https://pyserial.readthedocs.io/en/latest/shortintro.html

Guide to MSP432P401R LaunchPad (MSP-EXP432P401R): https://energia.nu/pinmaps/msp-exp432p401r/

MSP-EXP432P401R Documents: http://www.ti.com/tool/MSP-EXP432P401R