Skip to content

Commit

Permalink
chore: change resistor value
Browse files Browse the repository at this point in the history
  • Loading branch information
sondresjolyst committed Dec 31, 2024
1 parent 7593ac0 commit 33f4031
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/VoltmeterController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 33f4031

Please sign in to comment.