From 33f4031c10bdf6d68717c16212710f432e445fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sondre=20Sj=C3=B8lyst?= Date: Tue, 31 Dec 2024 12:27:42 +0100 Subject: [PATCH] chore: change resistor value --- src/VoltmeterController.h | 62 +++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/VoltmeterController.h b/src/VoltmeterController.h index b7be277..529bd90 100644 --- a/src/VoltmeterController.h +++ b/src/VoltmeterController.h @@ -16,32 +16,32 @@ extern PRINTHelper printHelper; const int ANALOG_IN_PIN = A0; const int ANALOG_RESOLUTION = 1024; // 10-bit resolution const float ANALOG_VOLTAGE = 3.32; // Reference voltage for ESP8266 ADC -const float R1 = 10000.0; // 10kΩ +const float R1 = 47000.0; // 47kΩ const float R2 = 4700.0; // 4.7kΩ const float CORRECTION_FACTOR = 1.218; // multimeter voltage / voltageMeasured // Corrected constants for exponential correction // Constants a and b are calculated based on curve fitting using known points: -// Point 1: (Measured Voltage: 5.25, Actual Voltage: 5.09) -// Point 2: (Measured Voltage: 10.38, Actual Voltage: 12.65) +// Point 1: (Measured Voltage: 5.31395, Actual Voltage: 5.03) +// Point 2: (Measured Voltage: 13.05305, Actual Voltage: 12.59) // Steps to calculate a and b: // 1. Convert the known points to natural logarithms. // 2. Solve the system of linear equations to find b. // 3. Use b to solve for a. // -// Using points (5.25, 5.09) and (10.38, 12.65): -// ln(5.09) = ln(a) + b * ln(5.25) -// ln(12.65) = ln(a) + b * ln(10.38) +// Using points (5.31395, 5.03) and (13.05305, 12.59): +// ln(5.03) = ln(a) + b * ln(5.31395) +// ln(12.59) = ln(a) + b * ln(13.05305) // // Solve for b: -// b = (ln(12.65) - ln(5.09)) / (ln(10.38) - ln(5.25)) -// b ≈ 1.33555 +// b = (ln(12.59) - ln(5.03)) / (ln(13.05305) - ln(5.31395)) +// b ≈ 1.02092 // // Solve for a: -// a = exp(ln(5.09) - b * ln(5.25)) -// a ≈ 0.555788 -const float a = 0.555788; -const float b = 1.33555; +// a = exp(ln(5.03) - b * ln(5.31395)) +// a ≈ 0.91406 +const float a = 0.91406; +const float b = 1.02092; const int READ_VOLTAGE_DELAY = 60000; const int READING_VOLTAGE_BUFFER = 5; @@ -58,44 +58,48 @@ float readVoltage() { Serial.print("Sensor Value: "); Serial.println(sensorValue); + printHelper.print("sensorValue: "); + printHelper.print(String(sensorValue)); + printHelper.println(" V"); + // Calculate the measured voltage at the divider output float voltageMeasured = (ANALOG_VOLTAGE / ANALOG_RESOLUTION) * sensorValue; - Serial.print("Measured Voltage: "); - Serial.print(voltageMeasured); + Serial.print("voltageMeasured: "); + Serial.print(voltageMeasured, 5); Serial.println(" V"); - printHelper.print("Measured Voltage: "); - printHelper.print(String(voltageMeasured)); + printHelper.print("voltageMeasured: "); + printHelper.print(String(voltageMeasured, 5)); printHelper.println(" V"); float vinTest = voltageMeasured * ((R1 + R2) / R2); - Serial.print("Input Voltage: "); - Serial.print(vinTest); + Serial.print("vinTest: "); + Serial.print(vinTest, 5); Serial.println(" V"); - printHelper.print("Input Voltage: "); - printHelper.print(String(vinTest)); + printHelper.print("vinTest: "); + printHelper.print(String(vinTest, 5)); printHelper.println(" V"); // calculated correction - float vinTestCorrected = + float vinMeasuredCorrected = voltageMeasured * ((R1 + R2) / R2 * CORRECTION_FACTOR); - Serial.print("Input Voltage Corrected: "); - Serial.print(vinTestCorrected); + Serial.print("vinMeasuredCorrected: "); + Serial.print(vinMeasuredCorrected, 5); Serial.println(" V"); - printHelper.print("Input Voltage Corrected: "); - printHelper.print(String(vinTestCorrected)); + printHelper.print("vinMeasuredCorrected: "); + printHelper.print(String(vinMeasuredCorrected, 5)); printHelper.println(" V"); // exponential correction float vinTestCorrectedExponential = a * pow(vinTest, b); - Serial.print("Input Voltage exponential Corrected: "); - Serial.print(vinTestCorrectedExponential); + Serial.print("vinTestCorrectedExponential: "); + Serial.print(vinTestCorrectedExponential, 5); Serial.println(" V"); - printHelper.print("Input Voltage exponential Corrected: "); - printHelper.print(String(vinTestCorrectedExponential)); + printHelper.print("vinTestCorrectedExponential: "); + printHelper.print(String(vinTestCorrectedExponential, 5)); printHelper.println(" V"); return vinTestCorrectedExponential;