Reading Light Sensor Data and Linear Regression in MATLAB
This in-class activity guides students through collecting real-time light intensity data using the Grove Light Sensor connected to an Arduino Uno and analyzing the data in MATLAB. Students will submit three deliverables: a MATLAB script, Voltage vs. Distance plot and a screenshot of the Command Window output.
Purpose of project
Imagine you are part of a design team developing a light-based sensing system for a small robot or an automated device. Your goal in this activity is to prototype a simple data-acquisition and analysis setup using MATLAB and Arduino. You will measure the voltage output from a light sensor at different distances from a light source and build a linear model that describes the relationship between distance and sensor output. This type of modeling is commonly used in robotics, automation and sensing systems to understand how sensors behave in real environments.
You will use MATLAB to communicate with the Arduino, collect measurements, compute a linear regression model and generate a clear plot showing your results. Through this activity, you will practice data acquisition, loop structures, plotting and model interpretation — core skills for engineering systems that integrate MATLAB, sensors and microcontrollers.
Your Task
Demonstrate that you can use MATLAB to collect and analyze voltage data from the Grove Light Sensor connected to the Arduino Uno and accomplish the following:
- Connect the Grove Light Sensor to the A0 port on the Grove Base Shield attached to the Arduino Uno
-
Run the provided MATLAB script to:
- read real-time voltage data at multiple distances,
- compute average sensor readings,
- create a linear regression model of Voltage vs. Distance,
- calculate the slope, intercept and R^2.
-
Generate a plot that shows:
- measured data points,
- the best-fit regression line,
- labels, title, legend and grid.
-
Save and submit the following deliverables:
- MATLAB script (.m file)
- Plot image (.png)
- Screenshot of Command Window output showing voltage readings, model and R^2
Organizing work
Pay attention to how you format and organize your work in MATLAB and in your Word document. Follow these general guidelines:
- Clearly label plot axes and include an appropriate title (for example, “Light Sensor Voltage vs. Distance – Arduino Uno”)
- Include comments and section headers in the MATLAB script to separate logical parts such as initialization, parameters, data collection, linear regression, plotting and results
- Insert the circuit diagram or photo showing the Grove Light Sensor connected to the A0 port on the Grove Base Shield
- Add the final MATLAB code and a photo of Arduino setup with the light sensor connected
- Paste a screenshot of the Command Window output, including voltage readings, linear model and R^2
- Insert the MATLAB plot (.png) directly into the Word document
- Combine the MATLAB figure and Word file into a single submission whenever possible
Submission instructions
Complete this assignment as a team. One team member must submit work on Brightspace, listing each member of the team in the submission process. All team members should review and approve the submission.
Deliverables
-
Word file – Final MATLAB code, temperature and humidity plot, Command Window screenshot and a picture of the Arduino setup
- Name Word file: ENGR132_ICA_LightSensor_LastNames.docx
-
MATLAB script (.m) – Final code that reads voltage data from the Grove Light Sensor, computes a linear regression model and generates the Voltage vs. Distance plot
- Name script: ICA_ LightSensor _LastNames.m
-
MATLAB figure (.png) – Exported Voltage vs. Distance plot with labeled axes, legend, title and grid
- Name figure: ENGR132_ICA_ LightSensor _LastNames.png
- Submit all files through the designated Brightspace In-Class Activity Drop Box
Background / Technical Content
The Grove Light Sensor produces an analog voltage that increases with brighter light and decreases when the light source is farther away. In this activity, the Arduino reads the sensor voltage through the A0 port and MATLAB collects these values at different distances from a light source. You will analyze how the voltage changes with distance and create a linear regression model. This helps you understand basic sensor behavior, detect saturation and evaluate measurement quality using the slope, intercept and R^2.
References
- MathWorks. (2024). Read Analog Sensor Data Using MATLAB.
- Seeed Studio. (2024). Grove – Light Sensor Wiki.
Learning Objectives
|
Learning Objectives |
Did you address this? |
|---|---|
| MAT01 – Develop MATLAB code that follows professional programming standards (headers, comments and organized structure) | |
| PC05 – Fully address all parts of the assignment by following instructions and submitting all deliverables | |
| MAT05 – Create and execute MATLAB scripts that combine calculations, logic and plotting | |
| 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 |