Creating and Using UDFs to Read and Plot Temperature and Humidity Data
This in-class activity guides students through creating and using User-Defined Functions (UDFs) in MATLAB to read and plot real-time temperature and humidity data from a Grove DHT22 (Temperature & Humidity Sensor Pro v1.3) connected to an Arduino Uno R4 WiFi. Students will submit three deliverables: a MATLAB script, a temperature and humidity plot, and a screenshot of the Command Window output.
Purpose of project
Imagine you are part of a design team developing a small-scale environmental monitoring system for smart devices or autonomous systems. Your goal in this activity is to prototype a data-acquisition and visualization setup using MATLAB and Arduino that displays real-time ambient temperature and humidity. The DHT22 sensor simulates the type of environmental sensor used in indoor climate control, weather stations and smart home technologies.
You will write a MATLAB script that uses a main function and two subfunctions (UDFs). One subfunction communicates with the Arduino to read the DHT22 sensor data and the other converts the temperature from Celsius to Fahrenheit. Through this activity, you will practice modular programming, function creation and real-time data visualization, which are essential skills for engineering systems that integrate MATLAB, sensors and microcontrollers.
Your Task
Demonstrate that you can use MATLAB to collect and visualize temperature and humidity data from the Grove DHT22 sensor connected to the Arduino Uno R4 WiFi by creating and using User-Defined Functions (UDFs) and accomplish the following:
- Connect the Grove Temperature & Humidity Sensor Pro (DHT22) to port D2 on the Grove Base Shield attached to the Arduino Uno R4 WiFi.
-
Write a MATLAB program that includes a main function and two subfunctions (UDFs):
- readTh(sp) to communicate with the Arduino and read one line of data formatted as "T:24.70,H:42.30."
- cToF (tempC) to convert temperature from Celsius to Fahrenheit
- Collect a series of temperature and humidity readings (suggested N = 10) at regular time intervals (~2.5 seconds)
- Print the results in a formatted table in the Command Window showing reading number, temperature (Celsius and Fahrenheit) and humidity (%RH)
- Create a plot that displays temperature (Celsius) and humidity (%RH) versus reading number using dual y-axes
- Label the plot with a clear title, axis labels, legend and grid lines
-
Save and submit the following deliverables:
- MATLAB script (.m file)
- Temperature and humidity plot (.png image)
- Screenshot of the Command Window output
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 the figure axes and title in MATLAB. (For example, “Temperature and Humidity Readings – Arduino Uno R4 WiFi.”)
- Include comments and section headers in your MATLAB script to separate logical parts such as initialization, parameters, calculations, results and cleanup.
- Insert the circuit diagram, showing the Grove DHT22 sensor connected to port D2 on the Grove Base Shield attached to the Arduino.
- Add the final MATLAB code and a photo of the Arduino setup with the sensor connected.
- Paste a screenshot of the Command Window output showing at least five temperature and humidity readings.
- 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 of the team. 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 Arduino setup.
- Name the Word file: ENGR132_ICA3_UDFs_DHT22_LastNames.docx
-
MATLAB script (.m): Final code that uses UDFs to read the DHT22 sensor data, convert temperature from Celsius to Fahrenheit, and generate a real-time plot.
- Name the script: ICA3_UDFs_DHT22_LastNames.m
-
MATLAB figure (.png): Exported temperature and humidity plot with labeled axes, legend, title and grid.
- Name the figure: ENGR132_ICA3_UDFs_DHT22_LastNames.png
- Submit all files through the designated Brightspace In-Class Activity Drop Box.
Background / Technical Content
The Grove Temperature & Humidity Sensor Pro (DHT22) measures ambient temperature and relative humidity and sends digital data through the Arduino’s serial interface. In this activity, the Arduino Uno R4 WiFi collects sensor readings and transmits them to MATLAB for real-time visualization.
By using User-Defined Functions (UDFs), students separate the program into smaller, well-defined parts for data reading, conversion, and plotting. This modular approach reflects professional programming practices that make code easier to organize, debug, and reuse.
References
- MathWorks. (2024). Read and Plot Temperature and Humidity Data from Arduino. MATLAB Documentation.
- Seeed Studio. (2024). Grove – Temperature & Humidity Sensor Pro (DHT22) Wiki. https://wiki.seeedstudio.com/Grove-Temperature_and_Humidity_Sensor_Pro/
Learning Objectives
|
Learning Objectives |
Did you address this? |
|---|---|
| MAT01 – Develop MATLAB code that follows professional programming standards (headers, comments and organized structure) | |
| MAT02 – Create and use User-Defined Functions (UDFs) in MATLAB to modularize code | |
| 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 | |
| PC05 – Fully address all parts of the assignment by following instructions and submitting all deliverables |