Project 3.1.12: Hygiene Data Logger
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico detects hygiene events and stores a simple local log for later review.
Without records, it is difficult to compare when and how often hygiene-related events happened.
A Pico 2 W prototype with an IR sensor, event LED, and local file logging.
Event logging, edge detection, and simple local record keeping.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() IR Sensor |
![]() Breadboard |
![]() Jumper wires |
![]() LED |
![]() Resistor |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| IR sensor OUT | GPIO 14 | GPIO 14 / physical pin 19 | Event input |
| LED anode | 220 ohm resistor to GPIO 15 | GPIO 15 / physical pin 20 | Event log LED |
Wiring Diagram

IR sensor OUT -> GPIO 14
GPIO 15 -> 220 ohm resistor -> LED anode
Common GND -> IR sensor and LED
Step-by-Step Assembly
- Connect the IR sensor output to GPIO 14 and power the module according to its rating.
- Wire the LED to GPIO 15 through a 220 ohm resistor.
- Aim the sensor so one deliberate event produces one clear trigger.
Testing Individual Components
Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Verify the raw digital sensor state before enabling file logging.
- Test the output device: Test the LED output separately and then test that a sample line can be written to a file.
- Test the decision logic: Trigger several events and confirm the saved file has the correct number of records.
- Run the full system: Run the full logger and compare the printed events with the saved file content.
- Validate the prototype: Evaluate whether the lockout is long enough to reduce duplicate records.
- Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.
Full Project Code
After completing and checking the circuit connections, open Thonny IDE, copy and paste this code into a new file or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.
from machine import Pin
import time
SENSOR_PIN = 14
LED_PIN = 15
ACTIVE_STATE = 0
LOCKOUT_SECONDS = 1.0
LOG_FILE = 'hygiene_log.txt'
sensor = Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP)
led = Pin(LED_PIN, Pin.OUT)
last_trigger = 0
active_before = False
print('Hygiene data logger ready')
while True:
active_now = sensor.value() == ACTIVE_STATE
now = time.time()
if active_now and not active_before and now - last_trigger >= LOCKOUT_SECONDS:
last_trigger = now
record = 'event,{}\n'.format(now)
with open(LOG_FILE, 'a') as handle:
handle.write(record)
led.value(1)
print(record.strip())
else:
led.value(0)
active_before = active_now
time.sleep(0.05)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Event lockout | Prevents repeated events from being logged too quickly | A logger is only useful if one real event becomes one record | Increase the lockout if duplicate events still appear |
| Local file logging | Appends a simple timestamped line to a text file | Local storage makes the prototype useful even without networking | Open the file in Thonny after testing to verify the saved records |
| Event LED | Flashes when a record is written | This provides instant feedback that a log entry was saved | If the LED feels too brief, add a small pulse hold during testing |
Expected Result
The serial monitor reports the current reading or state clearly.
The hardware output responds when the decision logic changes state.
Subsystem behavior matches the thresholds, timing, or rules described in the document.
Validation Checks
- Normal condition test: confirm the system stays in its safe or idle state under baseline conditions
- Warning condition test: move the input close to the chosen threshold and confirm the transition is sensible
- Critical condition test: trigger the strongest alert or control state and confirm the output response is correct
- Calibration test: compare the sensor or timing against a known reference or repeated trial
- Limitation test: deliberately create an awkward or noisy condition and note how the prototype behaves
Deployment and Limitations
- This prototype is useful for classroom analytics and local operational studies
- Before deployment, the sensor location and counting logic need validation against real traffic patterns
- Counts are only useful when users know the conditions that produce under-counting or over-counting
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| The log file stays empty | The event sensor is not changing state or file writes are failing | Print the raw pin value first and then test a manual file write |
| Too many duplicate records appear | The lockout is too short or the sensor placement is noisy | Increase the lockout and improve the sensor aim |
| The LED never flashes | No events are being detected or the LED wiring is wrong | Test GPIO 15 separately and then retest the sensing path |
| The Pico storage fills up | The file is being left to grow indefinitely | Clear or rotate the log file between classroom test runs |





