Loops and Real-Time Data Visualization using the Joystick
This in-class activity guides students through creating a MATLAB script that continuously reads data from a Grove Thumb Joystick and plots its X–Y motion in real time. Students will submit three deliverables: a MATLAB script, a trajectory plot of the joystick movement and a screenshot of the output.
Purpose of project
Imagine you are part of a design team developing an interactive control interface for a robotic or navigation system. Your goal in this activity is to prototype a simple data-acquisition and visualization system using an Arduino Uno R4 WiFi and a Grove Joystick. The joystick simulates a user input device that provides two analog signals — one for the X-axis and one for the Y-axis — representing directional movement.
You will write a MATLAB script that repeatedly reads both joystick voltages inside a loop, stores them in vectors and produces a real-time plot of the joystick’s trajectory. Through this activity, you will practice professional programming standards by structuring code with clear sections, labeling variables and visualizing sensor data dynamically — building the foundation for projects that integrate loops, sensors and data visualization in engineering systems.
Your Task
Demonstrate that you can use MATLAB to collect and visualize data from the Arduino Uno R4 WiFi in real time using loops and accomplish the following:
- Connect the Grove Thumb Joystick to the Arduino through the A0 port on the Grove Base Shield (it uses A0 for the X-axis and A1 for the Y-axis internally)
-
Write a MATLAB script that repeatedly reads the joystick's X and Y voltages using a for loop and the commands:
- readVoltage(a,'A0') % X-axis
- readVoltage(a,'A1') % Y-axis
- Store the readings in two vectors (x and y) and update a plot in real time showing the joystick trajectory (X vs. Y)
- Label the plot with a clear title, axis labels and grid lines
- Display in the Command Window the number of samples collected and the average voltage of each axis
-
Save and submit the following deliverables:
- MATLAB script (.m file)
- Trajectory plot (.png image)
- Screenshot of Command Window output
Organizing work
Pay attention to how you format and organize work in MATLAB and the Word document. Below are some general instructions:
- Clearly label figure axes and title in MATLAB (e.g., “Joystick XY Trajectory – Arduino Uno R4 WiFi”)
- Include comments and section headers in the MATLAB script to organize code into logical parts (setup, parameters, loop, results)
- Insert a circuit diagram showing the Grove Joystick connected to the Arduino (A0/A1), final MATLAB code and a picture of the Arduino setup in the Word file
- Add a screenshot of the Command Window showing sample outputs (e.g., average voltages)
- Combine the MATLAB figure (.png) and Word document into a single submission, when possible
Submission instructions
Complete this assignment as a team. One team member must submit work on Brightspace, listing each member in the submission process. All team members should review and approve the submission.
Deliverables
-
Word file (include circuit diagram, final MATLAB code, joystick trajectory plot, Command Window screenshot and a picture of the Arduino setup)
- Name the Word file: ENGR132_ICA_Loops_Joystick_LastNames.docx
- MATLAB script (.m) (the final code that reads the joystick voltages using loops and generates the real-time trajectory plot)
-
MATLAB figure (.png) (exported voltage plot with labeled axes and title)
- Name the figure: ENGR132_ICA_PotSelection_LastNames.png
- Submit all files through the designated Brightspace In-Class Activity Drop Box
Background / Technical Content
Joysticks are commonly used in control and robotic systems as intuitive input devices for directional motion. Each axis (X and Y) acts as a potentiometer that converts movement into analog voltage signals, which can be read by the Arduino’s analog-to-digital converter (ADC).
In this activity, the Grove Thumb Joystick provides continuous analog readings that MATLAB collects through loops (for or while) and visualizes as a real-time trajectory plot. This demonstrates how repetitive data acquisition enables systems to track or respond to sensor inputs continuously — a concept widely applied in robotics, gaming and human–machine interfaces (Beer et al., 2020).
Loops are fundamental in engineering programming because they automate data collection, monitoring and control. Mastering them allows engineers to create dynamic systems that adapt to real-time feedback.
References
- Beer, R. D., Chiel, H. J., & Drushel, R. F. (2020). Control Systems Fundamentals. Oxford University Press.
Learning Objectives
|
Learning Objectives |
Did you address this? |
|---|---|
| MAT01 – Develop MATLAB code that follows professional programming standards (headers, comments and organized structure) | |
| MAT03 – Perform calculations and logical operations using relational and conditional statements | |
| MAT05 – Create and execute MATLAB scripts that combine calculations, logic and plotting | |
| MAT06 – Apply selection or repetition structures (for, while) to control the execution of code | |
| MAT09 – Test and track MATLAB code to verify correct data acquisition and expected plot behavior | |
| MOD01 – Model sensor behavior by interpreting joystick voltage readings as input data | |
| EPS01 – Communicate results professionally through labeled plots and clear written documentation | |
| EPS02 – Create professional-quality plots with appropriate titles, axis labels and units | |
| PC05 – Fully address all parts of the assignment by following instructions and submitting all deliverables |