Due: 2:00 p.m. on the next Wednesday or Thursday.
- Report Format: Submit a formal, typed lab report in PDF format. Handwritten lab reports will not be accepted. Hand-drawn circuit diagrams will not be accepted; draw every schematic with software (see Software at the end of this manual). Include screenshots, code, and simulation or measurement results where specified.
- AI Policy: The use of any AI tool, including a large language model (LLM), to write, generate, debug, or modify code or any portion of the lab report is prohibited unless the course instructor grants explicit permission in advance. A teaching assistant (TA) cannot grant permission to use AI tools.
By signing below, I acknowledge that I have read and understood the policies and instructions above and agree to follow them.
Student Name (print):
Student Signature: Date:
Introduction
The motors in Lab 5 needed separate power supplies and driver circuits. This week's MCP9808 temperature sensor draws so little current that we can connect its VDD pin directly to the 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 function that reads the temperature in finer steps.
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. - Create one sensor object before the loop, then read and print the temperature about every 0.2 s. Record the actual time of each reading: the sleep adds to the time spent reading and printing, so the readings will not be exactly 0.2 s apart. 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. Check that the output now moves in 0.125 °C steps (e.g., 24.125, 24.250). Test your function on register values whose answers you already know, both positive and negative. Also read and record the sensor's resolution setting: it must be 0.125 °C or finer. Keep in mind that a 0.125 °C step does not make the sensor accurate to 0.125 °C.
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:
- Lines joining the measured temperature points. If you smooth the data, say so.
- 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:
Extra Credit (up to +5 points)
These tasks are optional and meant for students who finish the required tasks early. The course instructor and TAs will not provide any help with them: no hints, no debugging, and no checking of your design, wiring or code. Do them entirely by yourself.
Finish every required task and get it signed off first. A TA will sign off a finished extra-credit task only when it works. A TA will still stop any setup that could damage the equipment or the Raspberry Pi. Points count only for a task that is signed off and written up in your report, with an error analysis where it applies, as for every task.
Task 5: Live Temperature Plot (+5 points)
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). - ☐ An error analysis for every task where it applies, including extra-credit tasks: the uncertainty of each measured or calculated value, its main sources of error, and a comparison with the expected, simulated, or datasheet value.
- ☐ 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 did the extra credit (Task 5), include your live-plot code and a short video or animated GIF of the plot in action.
Draw every circuit diagram with software; hand-drawn circuit diagrams will not be accepted. We recommend the free draw.io; the course’s Drawing Circuit Diagrams page shows how to set it up. If a component has no symbol of its own, use the closest one and label it clearly.
Where a task asks for a simulation: the web version of Multisim is no longer available. If you have a Windows computer, you can install the offline (desktop) version of Multisim; ask a TA for the serial number to install it. Otherwise, feel free to use any SPICE-based simulation tool, such as LTspice or ngspice.