Due: 2:00 p.m. next Monday.
Introduction
In our previous lab, we learned the importance of using driver circuits for power-hungry peripherals. This week, we will work with a component that has very different requirements: the MCP9808 digital temperature sensor. This sensor is highly efficient, consuming minimal power, which allows us to connect its VDD pin directly to a Raspberry Pi’s 3.3 V supply.
This lab is designed to be a practical exercise in reading a device datasheet. You will start by using a pre-written but limited Python module. Then, by interpreting the sensor’s documentation, you will write your own code to unlock its full precision.
Tasks
Instructions: Complete each task in order. Obtain an instructor’s signature after completing each major section to verify your progress. Remember to take screenshots and photos of your setup and results to include in your formal lab report.
Task 1: Workspace and I2C Setup
The first step is to prepare your lab directory and enable the Raspberry Pi’s I2C interface.
- Open the terminal and create a new directory for this lab (e.g.,
phys351_lab6). Navigate into this directory. - Enable the I2C interface using the Raspberry Pi Configuration tool.
sudo raspi-config
Navigate toInterface Options→I2Cand select<Yes>. Reboot when prompted.
Course Instructor or TA Signature:
Date and Time:
Task 2: Sensor Connection and Initial Reading
Now, you will wire the sensor and perform a basic temperature reading.
- With the Raspberry Pi powered off, connect the MCP9808 sensor using four jumper wires. Consult the pinout diagrams in the lecture notes and datasheet.
- Power on the Pi. In the terminal, run
i2cdetect -y 1. You should see the sensor’s default hexadecimal address (18) appear in the grid. If not, check your wiring. - Copy the provided file
mcp9808-mod.pyfrom a USB drive into your lab directory. - Create a new Python script named
lab6_main.py. In this script, importtimeand theMCP9808class from the provided module. - Write a loop that creates an instance of the sensor and prints the temperature to the console every 0.2 s. Use the
read_temperature()method from the imported class. - Run your script. What do you notice about the temperature values being displayed? Describe the limitation of the provided module in your lab notes.
Course Instructor or TA Signature:
Date and Time:
Task 3: Implementing High-Resolution Reading
The provided module is intentionally basic. Your main task is to write a new function that reads the temperature with a resolution of 0.125°C. This requires you to read the MCP9808 datasheet and correctly interpret the fractional part of the temperature register.
- Open the MCP9808 datasheet and navigate to the section describing the Ambient Temperature Register. Pay close attention to the bit layout, specifically the sign bit and the fractional bits.
- In your
lab6_main.pyscript, write a new function, for exampleread_temp_high_res(sensor). This function will take the sensor object as an argument. - Bitwise Logic: Inside your new function, you must read the raw data from the sensor. To achieve a resolution of exactly 0.125°C, you must use the integer part and figure out how many bits you need of the fractional part. Failure to use precisely the correct number of fractional bits will result in zero points for this task.
- Hint: Your solution will involve bitwise masking (
&) to isolate the required bits and arithmetic to convert the binary fraction to a decimal value. Remember to also handle the sign bit correctly for negative temperatures.
- Hint: Your solution will involve bitwise masking (
- Modify your main loop to call your new
read_temp_high_res()function instead of the old one. Verify that the output now shows temperature values with the correct precision (e.g., 24.125, 24.250).
Course Instructor or TA Signature:
Date and Time:
Task 4: Data Analysis and Visualization
With high-resolution readings working, you will now add features for data conversion, logging, and plotting.
- Modify your program’s output to display the temperature in both Celsius (°C) and Fahrenheit (°F). Ensure the conversion is accurate and the output is clearly labeled.
- Add a feature to set a critical temperature threshold (e.g., 28.0°C). If the measured temperature exceeds this value (e.g., when you warm the sensor with your finger), your program should print a distinct warning message to the console.
- Modify your loop to run for at least 100 measurements. Store each measurement (time and high-resolution temperature) in a Python list or NumPy array. The time should be the elapsed time in seconds since the program started.
- Generate a plot of your data, including:
- A smooth line showing temperature vs. time.
- Markers (points) for each individual data point.
- A descriptive title and clearly labeled axes with units.
- A horizontal dashed line indicating the critical temperature threshold you set.
- Finally, add code to save the collected time and temperature data to a
.csvfile namedtemp_data.csv. The file should have two columns with headers: “Time (s)” and “Temperature (C)”.
Course Instructor or TA Signature:
Date and Time:
Optional: Live Temperature Plot
For extra credit, modify your program to generate a live, animated plot that updates in real time as new temperature readings are taken.
- Use
matplotlib.animationor a similar technique to create the live plot. - The plot should display the most recent N seconds of data and scroll as new data arrives.
- Ensure the plot includes a title, labeled axes, and a legend.
- The animation should run smoothly without excessive lag.
Course Instructor or TA Signature:
Date and Time:
Submission Reminder
Your formal lab report must be submitted as a single PDF to Gradescope. Please ensure it includes the following components:
- ☐ A brief Introduction stating the goals of the lab.
- ☐ A Methods section describing your experimental setup, including photos of your wiring.
- ☐ A detailed explanation of your logic for the
read_temp_high_res()function. Explain precisely how you used bitwise operations to achieve 0.125°C resolution. - ☐ Your complete, commented Python script (
lab6_main.py). - ☐ The final temperature vs. time plot generated by your script.
- ☐ A sample of the data from your
.csvfile (e.g., the first 10 and last 10 rows). - ☐ A Discussion section summarizing your results. Discuss any challenges you encountered (e.g., in interpreting the datasheet or implementing the bitwise logic) and how you resolved them.
- ☐ If you completed this task, include a short video or animated GIF of your live plot in action.