Reading Ultrasonic Sensor (HC-SR04) Data on a 128×128 LCD and Visualizing It Using Matplotlib

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

In this instructable, we will be using the MSP432 LaunchPad + BoosterPack to display an ultrasonic sensor’s (HC-SR04) data on a 128×128 LCD and send the data to PC serially and visualize it using Matplotlib.

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
MSP432 LaunchPad, Educational BoosterPack MKII, Servo Motor, Ultrasonic Sensor (HC-SR04), Jumper Wires, Mini Breadboard.

Step 2: Software

Energia IDE
Download: https://energia.nu/

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

Step 3: Hardware Setup

S1. Connect your BoosterPack on top of the LaunchPad.

S2. Connect the Ultrasonic sensor (HC-SR04) -> BoosterPack.

Vcc -> pin 21
GND -> pin 22
Trig -> pin 33
Echo -> pin 32


S3. Connect the Servo motor -> BoosterPack.

Red -> POWER
Black -> GND
Orange -> SIGNAL (J2.19)


S4. Connect the MSP432 LaunchPad to one of your computer’s USB ports.

Step 4: Energia IDE

S1. Open Energia IDE.
S2. Select the correct serial port and board.
S3. Upload the below program to the LaunchPad by clicking on the Upload button.

Here’s what the program does:
P1. It rotates the servo motor from 0 to 180 degrees and back from 180 to 0 degrees in steps of 10.
P2. Calculates the distance (cm) reading from the ultrasonic sensor and displays it on the 128×128 LCD.
P3. If the distance (cm) is less than 20 turn on the Red LED else turn on the Green LED.
P4. Just to play around with the LCD screen space, the program also displays some geometrical shapes.

Step 5: Energia IDE - Sketch

The above sketch can be downloaded from here.

Step 6: Plotting the Data

You can use any Python IDE, in this case I’m using 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. Matplotlib is a plotting library for Python.
S2. NumPy is the fundamental package for scientific computing in Python.
S3. PySerial is a Python library which provides support for serial connections over a variety of different devices.

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.

The above program can be downloaded from here.

Step 8: Final!

Depending on your surrounding area, you should start seeing the distance measured (cm) between various objects on the LCD display as the servo motor rotates from 0 to 180 degrees and back from 180 to 0 degrees. The Python program shows a live plot of the ultrasonic sensor’s reading.

References

Matplotlib:
https://matplotlib.org/
PySerial:
https://pyserial.readthedocs.io/en/latest/shortintro.html
Numpy:
https://numpy.org/devdocs/user/quickstart.html
Ultrasonic Distance Sensor - HC-SR04:
https://www.sparkfun.com/products/15569
MSP432 LaunchPad:
http://www.ti.com/tool/MSP-EXP432P401R
Educational BoosterPack MKII:
http://www.ti.com/tool/BOOSTXL-EDUMKII
Servo Motor:
https://www.sparkfun.com/products/11965

Be the First to Share

    Recommendations

    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    Discussions