← About PHYS 351

PHYS 351 · Advanced Instrumentation

Lab 7 · Distance sensor & accelerometer

Submission Submit the completed lab report as a PDF on Gradescope.

Due: 2:00 p.m. on the next Wednesday or Thursday.

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 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.
  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 Lab 6, you worked with a digital temperature sensor that communicates with a Raspberry Pi through the I2C protocol. In this lab, you will explore an ultrasonic range finder and an accelerometer. Capture screenshots, photographs, and videos as necessary, and add descriptive comments to your Python scripts.

Tasks

  1. HC-SR04 Range Finder Setup and Implementation:
    1. Show an instructor the HC-SR04 Python script you wrote and the schematic to connect this range finder to the Raspberry Pi.
    2. With the power off, build the circuit, including the Echo divider. Do not connect the divider's output to the GPIO pin yet.
    3. Show the circuit to an instructor before you turn on the power. Check the divided Echo pulse on the oscilloscope, then power down and connect it to the GPIO pin.
    4. Debug and run your script to test if the range finder can measure distance.
    5. Output measurements in m, cm, mm, and inches. Handle the significant figures appropriately.
Task approvalTask 1 (HC-SR04 Setup and Implementation)

Course Instructor or TA Signature:

Date and Time:

  1. Range Finder Calibration:
    1. Determine the zero offset, minimum range, maximum range, and accuracy of the range finder.
    2. Compare your measurements to the specifications in the datasheet.
Task approvalTask 2 (Range-Finder Calibration)

Course Instructor or TA Signature:

Date and Time:

  1. LIS3DH Accelerometer Setup:
    1. Copy the provided file LIS3DHMOD.py from a USB drive into your lab directory.
    2. Connect the LIS3DH properly to the Raspberry Pi.
    3. Detect the I2C address using the terminal.
    4. Write and run your script to validate the output of the x-axis.
    5. Show your script to an instructor.
Task approvalTask 3 (LIS3DH Setup)

Course Instructor or TA Signature:

Date and Time:

  1. Acceleration Data Collection and Analysis:
    1. Record at least 100 acceleration data points along each of the three axes.
    2. Determine the standard deviation with the accelerometer stationary.
    3. Theorize how a tilted plane affects the accelerometer readings.
    4. Determine the smallest angle change detectable by the accelerometer.
Task approvalTask 4 (Acceleration Data Analysis)

Course Instructor or TA Signature:

Date and Time:

  1. Data Averaging and Visualization:
    1. Write code that averages the readings in separate groups of n (readings 1 to n, then n + 1 to 2n, and so on). Record a timestamp with every reading, and do not read faster than the sensor's data rate, or you will get the same sample more than once.
    2. Determine the standard deviation of these averaged measurements.
    3. Plot acceleration against the measured time, with enough samples to show the motion clearly. Plot the raw and the averaged data separately, so the averaging does not hide anything.
    4. Record data after manipulating the accelerometer.
    5. Save all data in a .csv file and save the plots as image files.
    6. Discuss results, observations, and the plots in detail in your report.
Task approvalTask 5 (Data Averaging and Visualization)

Course Instructor or TA Signature:

Date and Time:

Extra Credit (up to +14 points)

Extra credit · up to +14 points · no help given

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. For Lab 7 Task 7, a TA may sign off completed parts for partial credit; clearly identify those parts in your report. For other extra-credit tasks, a TA will sign off a finished 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 work that is signed off and written up in your report, with an error analysis where it applies, as for every task.

You can earn up to +4 points for Task 6, plus up to +10 points for the shared Task 7 if you claim it on Lab 7.

Shared Task 7: up to +10 points, awarded once

You may complete Lab 7 Task 7 during Lab 7, during Lab 8, or across both labs. This is one shared extra-credit assignment worth up to 10 points total, not 10 points for each lab. If you finish it in Lab 7, include it in your Lab 7 report and the points will be added to Lab 7. If you finish it later, include it in your Lab 8 report and the points will be added to Lab 8. Credit is awarded only once, on one lab report; work already credited in Lab 7 cannot earn credit again in Lab 8.

Partial credit is available. In the report where you claim credit, clearly list which of the six parts you completed, which are incomplete or unattempted, and the supporting code, measurements, plots, and analysis for each completed part. Have the completed parts signed off and identify whether you are claiming the credit on Lab 7 or Lab 8. If you plan to continue in Lab 8, wait and claim your Task 7 credit in the Lab 8 report, so all your work on it is graded together, once.

  1. Accelerometer Pendulum Motion and Data Visualization (+4 points):
    1. Setup and Swing Test: Secure the accelerometer and connect it to the Raspberry Pi using the correct pins. Configure your script to capture and log acceleration continuously along all three axes (x, y, and z) while the accelerometer swings as a pendulum. Mount the accelerometer so that it can swing freely, and record at least 100 data points over at least 10 s, covering several swings.
    2. Data Visualization: Plot the acceleration data over time for all three axes using Matplotlib, with each axis represented by a different color (e.g., X-axis in red, Y-axis in green, Z-axis in blue). Label the axes clearly and add a legend to distinguish between them.
    3. Data Analysis: Analyze whether the measured oscillation frequencies correspond to the expected pendulum motion. In your report, discuss whether the frequencies observed along each axis match the physical motion of the pendulum, and explain any discrepancies.
Task approvalTask 6 (Extra Credit: Pendulum Motion)

Course Instructor or TA Signature:

Date and Time:

  1. Temperature-Compensated Range Finder with Threads and Processes (up to +10 points, shared with Lab 8):
    1. Temperature Compensation: Connect the MCP9808 temperature sensor to your circuit alongside the LIS3DH, giving the two chips different I2C addresses. Modify your code so the range finder outputs distance based on the MCP9808 temperature reading.
    2. One Loop (Baseline): Write a program that reads the accelerometer, the temperature, and the distance in a single loop, and records a time stamp with every reading. With the range finder aimed at a fixed target, collect at least 100 distance readings. Determine the accelerometer sampling interval (mean, standard deviation, and maximum) and the standard deviation of the distance.
    3. Threads: Rewrite the program with the threading module so that the accelerometer, the range finder, and the temperature sensor each run in their own thread, with the accelerometer aiming for 100 Hz. Repeat the baseline measurements from One Loop. Then change the switch interval with sys.setswitchinterval() and measure again.
    4. Processes: Rewrite the program with the multiprocessing module so that the range finder runs in its own process and sends its readings back through a queue. Repeat the baseline measurements from One Loop.
    5. Calculation Test: Time a CPU-bound calculation run as 1, 2, and 4 threads and as 1, 2, and 4 processes. Repeat each timing at least three times, and calculate the speedup for each case.
    6. Comparison: Tabulate all your results with their uncertainties, and explain the differences using the global interpreter lock (GIL) and the difference between threads and processes. Show an instructor your running programs.
Task approvalTask 7 (Extra Credit: Temperature Compensation, Threads, and Processes)

Course Instructor or TA Signature:

Date and Time:

Submission Reminder

  • Detailed schematics of your sensor circuits (HC-SR04 and LIS3DH).
  • Your complete Python scripts for both the range finder and accelerometer, with clear comments.
  • Calibration data and analysis for the HC-SR04 range finder.
  • Acceleration data analysis, including standard deviations, angle change detection, and the averaged measurements.
  • Plots of acceleration against time (Task 5), with explanations of your observations.
  • 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 of any challenges encountered and how you overcame them.
  • If you did the extra credit: for Task 6, plots of the pendulum data for all three axes (X, Y, Z), each in a different color, and a discussion of whether the observed frequencies match the pendulum's motion, explaining any differences. For Task 7, if you are claiming the credit on Lab 7, list the parts you completed and include their schematic, Python scripts (single loop, threads, processes, and the calculation test), timing results with uncertainties, and explanations. For partial credit, also list the parts you did not finish or did not attempt. If you are continuing in Lab 8, say that you will claim Task 7 on Lab 8 instead.
  • (Optional) Link to your YouTube video demonstrating the sensor operations.
Software for drawing and simulation

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.

← All course materials