← Advanced Instrumentation

PHYS 351 · Advanced Instrumentation

Lab 6 · Digital temperature sensing

PHYS 351 LAB 6
Digital Temperature Sensing with Raspberry Pi

© Ran Yang, Ph.D.
SUBMISSION   Submit the completed lab report as a PDF on Gradescope.

Due: 2:00 p.m. next Monday.

Policies and Instructions
  1. Report Format: Submit a formal, typed lab report in PDF format. Handwritten lab reports will not be accepted. Hand-drawn circuit schematics are allowed only when explicitly permitted in the task instructions. Include screenshots, code, and simulation or measurement results where specified.
  2. 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

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.

  1. Open the terminal and create a new directory for this lab (e.g., phys351_lab6). Navigate into this directory.
  2. Enable the I2C interface using the Raspberry Pi Configuration tool.
    sudo raspi-config
    Navigate to Interface OptionsI2C and select <Yes>. Reboot when prompted.
TASK APPROVAL Task 1 (Workspace and I2C Setup)

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.

  1. With the Raspberry Pi powered off, connect the MCP9808 sensor using four jumper wires. Consult the pinout diagrams in the lecture notes and datasheet.
  2. 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.
  3. Copy the provided file mcp9808-mod.py from a USB drive into your lab directory.
  4. Create a new Python script named lab6_main.py. In this script, import time and the MCP9808 class from the provided module.
  5. 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.
  6. Run your script. What do you notice about the temperature values being displayed? Describe the limitation of the provided module in your lab notes.
TASK APPROVAL Task 2 (Sensor Connection and Initial Reading)

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.

  1. 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.
  2. In your lab6_main.py script, write a new function, for example read_temp_high_res(sensor). This function will take the sensor object as an argument.
  3. 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.
  4. 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).
TASK APPROVAL Task 3 (High-Resolution Reading)

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Finally, add code to save the collected time and temperature data to a .csv file named temp_data.csv. The file should have two columns with headers: “Time (s)” and “Temperature (C)”.
TASK APPROVAL Task 4 (Data Analysis and Visualization)

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.

  1. Use matplotlib.animation or a similar technique to create the live plot.
  2. The plot should display the most recent N seconds of data and scroll as new data arrives.
  3. Ensure the plot includes a title, labeled axes, and a legend.
  4. The animation should run smoothly without excessive lag.
TASK APPROVAL Task 5 (Optional: Live Temperature Plot)

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 .csv file (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.

← All course materials