#include <Arduino.h>
#define DO_PIN A1
void setup()
{
Serial.begin(115200);
}
void loop()
{
int analogValue = analogRead(A0);
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
float temperature = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
float voltage = analogRead(DO_PIN) / 1023.0 * 5000; // read the voltage
float DOvalue = readDO(voltage, temperature);
Serial.print("Temperaturet:\t" + String(temperature) + "\t");
Serial.print("DO:\t" + String(DOvalue) + "\t");
Serial.print("Voltage_mv:\t" + String(voltage));
Serial.println();
delay(1000);
}
float readDO(float voltage_mv, float temperature_c) {
if (temperature_c < 0.0 | temperature_c > 40.0) return 0.0;
//Single-point calibration Mode=0
//Two-point calibration Mode=1
#define TWO_POINT_CALIBRATION 1
//Single point calibration needs to be filled CAL1_V and CAL1_T
#define CAL1_V (1600) //mv
#define CAL1_T (25) //℃
//Two-point calibration needs to be filled CAL2_V and CAL2_T
//CAL1 High temperature point, CAL2 Low temperature point
#define CAL2_V (1300) //mv
#define CAL2_T (15) //℃
const uint16_t DO_Table[41] = {
14460, 14220, 13820, 13440, 13090, 12740, 12420, 12110, 11810, 11530,
11260, 11010, 10770, 10530, 10300, 10080, 9860, 9660, 9460, 9270,
9080, 8900, 8730, 8570, 8410, 8250, 8110, 7960, 7820, 7690,
7560, 7430, 7300, 7180, 7070, 6950, 6840, 6730, 6630, 6530, 6410
};
#if TWO_POINT_CALIBRATION == 0
// uint16_t V_saturation = (uint32_t)CAL1_V + (uint32_t)35 * temperature_c - (uint32_t)CAL1_T * 35;
//return (voltage_mv * DO_Table[temperature_c] / V_saturation);
float V_saturation = CAL1_V + 35 * (uint8_t)temperature_c - CAL1_T * 35;
return (voltage_mv * DO_Table[(uint8_t)temperature_c] / V_saturation);
#else
//uint16_t V_saturation = (int16_t)((int8_t)temperature_c - CAL2_T) * ((uint16_t)CAL1_V - CAL2_V) / ((uint8_t)CAL1_T - CAL2_T) + CAL2_V;
//return (voltage_mv * DO_Table[temperature_c] / V_saturation);
float V_saturation = ((uint8_t)temperature_c - CAL2_T) * (CAL1_V - CAL2_V) / (CAL1_T - CAL2_T) + CAL2_V;
return (voltage_mv * DO_Table[(uint8_t)temperature_c] / V_saturation);
#endif
}