Visualizing Ambient Light Sensor (OPT3001) Data Using Matplotlib + MSP432 LaunchPad

Introduction: Visualizing Ambient Light Sensor (OPT3001) Data Using Matplotlib + MSP432 LaunchPad

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

In general, this is the output response of any sensor on a serial monitor (for example: Arduino) i.e. separated by a new line:
………
4079
4071
4072
4025
4035
4011
4014
4005
………
It would look nice if we can represent this sensor data graphically. Here’s how you can do this in a few steps!

We will be using a MSP432 LaunchPad + BoosterPack (TI BOOSTXL-EDUMKII) to send light sensor (OPT3001) data to PC serially and visualize the data in Matplotlib. A light sensor is a sensor that measures the intensity of visible light.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Hardware

What you will need:

1. MSP432 LaunchPad
(
http://www.ti.com/tool/MSP-EXP432P401R)
2. Educational BoosterPack MKII
(http://www.ti.com/tool/BOOSTXL-EDUMKII)

Step 2: Software

1. Energia IDE
Download: https://energia.nu/
2. PyCharm
Download: https://www.jetbrains.com/pycharm/

Step 3: Connections

Step 4: Energia IDE

    S1. Open Energia IDE.
    S2. Select the correct serial port and board.
    S3. Energia comes preloaded with the example code for OPT3001.
    S4. Upload the below program to the LaunchPad by clicking on the Upload button.

    Step 5: Energia IDE - Sketch

    #include <Wire.h>
    #include "OPT3001.h" #define USE_USCI_B1

    opt3001 op; int ledBlue = 37;

    void setup() { unsigned int readings = 0; Serial.begin(115200); delay(1000); op.begin(); }
    void loop() { // Variables uint32_t readings; // Read OPT3001 readings = op.readResult(); Serial.println(readings, DEC); //LUX Readings if (readings < 100) { digitalWrite(ledBlue, HIGH); //TURN ON - Blue Led } else if (readings > 100) { digitalWrite(ledBlue, LOW); //Turn OFF - Blue Led } delay(500); }

    Step 6: PyCharm

    Before you begin, make sure that the following prerequisites are met:
    -> You have installed Python. You can get it from: https://www.python.org/downloads/
    -> You are working with PyCharm Community.

    I. Creating a Python script in PyCharm

    S1. Let’s start our project: if you are on the Welcome screen, click Create New Project. If you have already got a project open, choose File -> New Project.
    S2.
    Select Pure Python -> Location (Specify the directory) -> Project Interpreter: New Virtualenv Environment -> Virtualenv tool -> Create.
    S3.
    Select the project root in the Project tool window, then select File -> New -> Python file -> Type the new filename.
    S4. PyCharm creates a new Python file and opens it for editing.

    II. Install the following packages: PySerial, Numpy and Matplotlib.

    S1. PySerial is a Python library which provides support for serial connections over a variety of different devices.
    S2. Matplotlib is a plotting library for Python.
    S3. NumPy
    is the fundamental package for scientific computing in Python.

    III. To install any package in PyCharm

    S1. File -> Settings.
    S2. Under Project, select Project Interpreter and click on the “+” icon.
    S3. In the search bar, type the package you wish to install and click on Install Package.

    Step 7: Python Program

    #NOTE: Make sure the COM port number and the baud rate is the same as that in the Energia sketch.
    import serial
    import matplotlib.pyplot as plt plt.style.use("dark_background") import numpy as np

    #In interactive mode, pyplot functions automatically draw to the screen. #Interactive mode may also be turned on via matplotlib.pyplot.ion(), and turned off via matplotlib.pyplot.ioff(). plt.ion()

    msp432 = serial.Serial('COM4', 115200) #(port number, baudrate) - create a serial object
    i = 0 x0 = [] y0 = []

    while True: msp432Serial = msp432.readline() lightSensorArray = int(msp432Serial) x0.append(i) y0.append(lightSensorArray)
    i += 1
    plt.xlim(left=max(0, i-20), right=i+20) #set the x-limits of the current axis plt.ylim(0, 12000) #set the y-limits of the current axis plt.yticks(np.arange(0, 12000, step=500))

    plt.ylabel('LUX Readings', fontname='Comic Sans MS', color='white', fontsize=14) #set the label for the y-axis plt.title('AMBIENT LIGHT SENSOR DATA', fontname='Comic Sans MS', color='white', fontsize=16) #set a title

    p1, = plt.plot(x0, y0, color='r', linewidth=2, marker='o') #plot x0 versus y0 - red line
    plt.legend([p1], ['OPT3001'], loc='upper right', frameon=True) #place legend in upper right corner of the chart

    plt.show() #display the figure plt.pause(.000001) #pause for interval seconds

    Step 8: Running Your Application

    Depending on your lighting condition you should start seeing a plot of the sensor’s lux reading.

    Normally for bright indoors the lux value can be in the range of 1001-5000 while for pitch black conditions the range can be between 0-10. I have also added a small piece of code in the Energia sketch that turns the blue led on when the lux value falls below 100.

    Feel free to play around with the code and note that this approach of visualizing data can be extended to almost any type of sensor reading!

    References

    Matplotlib:
    https://matplotlib.org/


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

    Numpy:
    https://numpy.org/devdocs/user/quickstart.html

    Understanding and Interpreting Lux Values:
    https://docs.microsoft.com/en-us/windows/win32/sensorsapi/understanding-and-interpreting-lux-values

    OPT3001 Ambient Light Sensor (ALS):
    http://www.ti.com/product/OPT3001

    Be the First to Share

      Recommendations

      • Magnets Challenge

        Magnets Challenge
      • Raspberry Pi Contest 2020

        Raspberry Pi Contest 2020
      • Wearables Contest

        Wearables Contest

      Discussions