Is there a way for me use the IR sensor without the heater? Or is that the only way to calibrate it? I have my pioreactor in an incubator at 37C, but the temperature of the vessel can still change due to other elements. I tried the code below, but I’m having difficulty calibrating it. I’m not sure if I’m taking the simplest approach here.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
import time
from datetime import datetime
import sys
from pioreactor.whoami import get_unit_name
from pioreactor.logging import create_logger
from pioreactor.utils.timing import current_utc_datetime
from pioreactor.utils.adcs import ADC
from pioreactor.hardware import ADC_CHANNEL_FUNCS, is_ADC_present, is_DAC_present, is_heating_pcb_present
def voltage_to_temp(voltage: float) -> float:
"""
Convert voltage to temperature using calibrated values.
Calibrated assuming 0.702V = 37°C
"""
# Using a simple linear conversion: temp = scale * voltage + offset
# We know that 0.702V = 37°C
scale = 37 / 0.702 # this gives us approximately 52.7 °C/V
return voltage * scale
def main():
"""Main function to run the temperature logger."""
unit = get_unit_name()
logger = create_logger(name="temp_logger", unit=unit)
try:
# Check hardware status
hw_status = check_hardware()
for device, status in hw_status.items():
logger.info(f"{device}: {'Yes' if status else 'No'}")
# Initialize ADC
adc = ADC()
logger.info("ADC initialized successfully")
with open('temperature_readings.txt', 'a') as f:
f.write(f"\nStarting temperature logging at {datetime.now()}\n")
f.write("Timestamp, Raw, Voltage, Temperature (°C)\n")
print(f"Temperature logger started. Logging to temperature_readings.txt")
print("Press Ctrl+C to stop...")
while True:
try:
# Read from aux channel
raw_value = adc.read_from_channel(ADC_CHANNEL_FUNCS["aux"])
voltage = adc.from_raw_to_voltage(raw_value)
temperature = voltage_to_temp(voltage)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Log to file
with open('temperature_readings.txt', 'a') as f:
f.write(f"{timestamp}, {raw_value}, {voltage:.4f}, {temperature:.2f}\n")
# Log to Pioreactor system
logger.info(f"Temp: {temperature:.2f}°C (Raw: {raw_value}, Voltage: {voltage:.4f}V)")
time.sleep(10)
except Exception as e:
logger.error(f"Error reading ADC: {str(e)}")
time.sleep(1)
except KeyboardInterrupt:
print("\nShutting down temperature logger...")
except Exception as e:
logger.error(f"Failed to initialize ADC: {str(e)}")
print(f"Error: {str(e)}")
sys.exit(1)
finally:
if 'adc' in locals():
adc.close()
def check_hardware():
"""Check what hardware is present and functional."""
results = {
"ADC present": is_ADC_present(),
"DAC present": is_DAC_present(),
"Heating PCB present": is_heating_pcb_present()
}
return results
if __name__ == "__main__":
main()