Close

Prototype Heater Control Sketch

A project log for OpenFluidWarmer

a safe, low-cost IV fluid warmer solution; for when commercially available IV fluid warmers are too expensive or cannot be sourced

john-opsahlJohn Opsahl 09/26/2020 at 01:590 Comments

The OpenFluidWarmer prototype Arduino sketch is detailed below. See the latest version of "OpenFluidWarmer_PrototypeSketch_MM-DD-YYYY.ino" in the files section of this project for the most up to date version of this code.

This sketch reads temperature from four thermistors (two per heater). Based on whether these temperature readings are above or below the hard coded temperature set points, the sketch will either send a command to the relays to close (and power the heaters) or do nothing.

// OpenFluidWarmer Prototype Sketch
// Inputs: 2 thermistors per heater
// Outputs: 1 relay per heater, indicator lights, buzzer

#include <math.h>

const int heater_pin[3] = {2, 3};
const int led_pin[4] = {4, 5, 6}; // red, yellow, green
const int buzzer_pin = 7;
const int thermistor_pin[5] = {A0, A1, A2, A3};

float thermistor[5]; // degC

bool heater_state[3] = {false, false};
bool warmup_complete = false;

const unsigned long led_blink_delay = 1000;
unsigned long led_blink_time = led_blink_delay;

const float heater_off_temp = 52.5; // degC
const float heater_on_temp = 50.0; // degC

const float V_0 = 5.0; // V
const float R_2 = 100000; // Ohm

// thermistor fit coefficients
const float c1 = 0.8272069482e-3;
const float c2 = 2.087897328e-4;
const float c3 = 0.8062131944e-7;

// averaging size
int avg_size = 10; 

void setup() {
  Serial.begin(9600);
  pinMode(heater_pin[0], OUTPUT);
  pinMode(heater_pin[1], OUTPUT);
  pinMode(led_pin[0], OUTPUT);
  pinMode(led_pin[1], OUTPUT);
  pinMode(led_pin[2], OUTPUT);
  pinMode(buzzer_pin, OUTPUT);
  
  digitalWrite(heater_pin[0], LOW);
  digitalWrite(heater_pin[1], LOW);
  digitalWrite(led_pin[0], LOW);
  digitalWrite(led_pin[1], LOW);
  digitalWrite(led_pin[2], LOW);
  digitalWrite(buzzer_pin, LOW);
  
  Serial.println("Setup Complete");
}

void loop() {

  for (int i=0; i<4; i++){
    
    // loop over several values to lower noise
    float loop_sum = 0.0;
  
    for (int j=0; j<avg_size; j++){
      int sensorValue = analogRead(thermistor_pin[i]);
      float voltage = (sensorValue/1023.0)*V_0;
      
      float R_1 = R_2*((V_0/voltage) - 1.0);
      
      float logR1 = log(R_1);
      loop_sum += (1.0 / (c1 + c2*logR1 + c3*logR1*logR1*logR1));
    }
    
    // average values from loop
    thermistor[i] = loop_sum/float(avg_size)-273.15;
  }
  
  // average the thermistor readings
  float thermistor_avg[3] = {0, 0};
  for (int i=0; i<2; i++){
    for (int j=0; j<2; j++){
      thermistor_avg[i] += thermistor[j+2*i];
    }
    thermistor_avg[i] /= 2.0;
  }

  float thermistor_sys_avg = (thermistor_avg[0]+thermistor_avg[1])/2.0;
  
  // readout for Celsius and Fahrenheit
  Serial.print("Thermisor: ");
  Serial.print(thermistor[0]);
  Serial.print(" ");
  Serial.print(thermistor[1]);
  Serial.print(" ");
  Serial.print(thermistor[2]);
  Serial.print(" ");
  Serial.print(thermistor[3]);
  Serial.print(" Side Average: ");
  Serial.print(thermistor_avg[0]);
  Serial.print(" ");
  Serial.print(thermistor_avg[1]);
  Serial.print(" System Average: ");
  Serial.print(thermistor_sys_avg);
  Serial.println();
  delay(500);

  // bang-bang heater control
  for (int i=0; i<2; i++){
    if (thermistor_avg[i] < heater_on_temp && heater_state[i] == false) {
      digitalWrite(heater_pin[i], HIGH);
      heater_state[i] = true;
      Serial.println("heater on");
    }
    else if (thermistor_avg[i] > heater_off_temp && heater_state[i] == true) {
      digitalWrite(heater_pin[i], LOW);
      heater_state[i] = false;
      Serial.println("heater off");
    }
  }

  // blink yellow LED during warmup period, turn on green LED when warmup period is over
  if (warmup_complete == false){
    
    if (millis() > led_blink_time){
      digitalWrite(led_pin[1], LOW); // turn off yellow LED
      led_blink_time = millis() + led_blink_delay;
    }
    else {
      digitalWrite(led_pin[1], HIGH); // turn on yellow LED
      digitalWrite(buzzer_pin, HIGH); // turn on buzzer
      delay(10);
      digitalWrite(buzzer_pin, LOW); // turn off buzzer
    }
    
    if (thermistor_avg[0] >= heater_off_temp && thermistor_avg[1] >= heater_off_temp){
      warmup_complete = true;
      digitalWrite(led_pin[1], LOW); // turn off yellow LED
      digitalWrite(led_pin[0], HIGH); //turn on green LED
      for (int j=0; j<6; j++){ //ping buzzer five times
        digitalWrite(buzzer_pin, HIGH); // turn on buzzer
        delay(10);
        digitalWrite(buzzer_pin, LOW); // turn off buzzer
        delay(100);
      }
    }    
  }
 
}

Discussions