Implement alternate voltage divider equation
This commit is contained in:
@@ -525,7 +525,7 @@ word I2C_ADS1115_ReadRaw(int channel)
|
|||||||
// 110 | ±0.256 V
|
// 110 | ±0.256 V
|
||||||
// 111 | ±0.256 V
|
// 111 | ±0.256 V
|
||||||
|
|
||||||
gain = 0x0000; // 6.144 V | 00000100 00000000
|
gain = 0x0000; // 6.144 V | 00000000 00000000
|
||||||
|
|
||||||
// Input multiplexer configuration: These bits
|
// Input multiplexer configuration: These bits
|
||||||
// configure the input multiplexer.
|
// configure the input multiplexer.
|
||||||
|
|||||||
@@ -2,41 +2,62 @@
|
|||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
// https://www.sciencedirect.com/science/article/abs/pii/S0263224111002594
|
||||||
// https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
|
// https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
|
||||||
// https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18280890511
|
// https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18280890511
|
||||||
// https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18441370202
|
// https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18441370202
|
||||||
// https://bmcnoldy.earth.miami.edu/Humidity.html
|
// https://bmcnoldy.earth.miami.edu/Humidity.html
|
||||||
|
|
||||||
// TODO: Is resistance function semantically correct?
|
|
||||||
// TODO: Check results for humidity conversion functions.
|
|
||||||
|
|
||||||
// Thermistor general constants
|
// Thermistor general constants
|
||||||
#define R1 10000.0f // Resistor ohm value
|
#define VIN 5.0f // Source voltage (VCC)
|
||||||
#define VRES 0xFFFF // 16-bit ADC resolution
|
#define GAIN 6.144f // PGA gain setting 0x00
|
||||||
|
#define AMAX 0x7FFF // 16-bit ADC resolution
|
||||||
|
#define R1 10000 // 10k Ohm resistor
|
||||||
|
|
||||||
// Thermistor coeficients
|
// Thermistor coeficients
|
||||||
#define SC1 1.009249522e-3f
|
#define SC1 1.009249522e-3f
|
||||||
#define SC2 2.378405444e-4f
|
#define SC2 2.378405444e-4f
|
||||||
#define SC3 2.019202697e-7f
|
#define SC3 2.019202697e-7f
|
||||||
|
|
||||||
// Humidity conversion constants
|
// Conversion constants
|
||||||
#define TH1 100.0f
|
#define TH1 100.0f
|
||||||
#define TH2 243.04f
|
#define TH2 243.04f
|
||||||
#define TH3 17.625f
|
#define TH3 17.625f
|
||||||
|
|
||||||
float Resistance(int adc_raw)
|
float Resistance(int adc_raw)
|
||||||
{
|
{
|
||||||
float r2;
|
float av, r2;
|
||||||
|
|
||||||
// TODO: Improve readability.
|
// [GND] - [10K R] - [ADC IN] - [THERMISTOR] - [VCC]
|
||||||
// TODO: Make sure output is correct.
|
|
||||||
|
|
||||||
// [GND] - [10K RESISTOR] - [ADC INPUT] - [THERMISTOR] - [VCC]
|
// The ADC measurement is taken between a 10k Ohm
|
||||||
|
// resistor and the thermistor. This is also called
|
||||||
|
// a voltage divider circuit which has the following
|
||||||
|
// equation:
|
||||||
|
|
||||||
// return ((VRES - adc_raw) * R1) / adc_raw;
|
// Vout = Vin * (R2 / R1 + R2)
|
||||||
r2 = (float) adc_raw / 264.8f * 0.05f;
|
|
||||||
|
|
||||||
return R1 / (5.0f / r2 - 1.0f);
|
// We can solve for resistance R2 by rearranging and
|
||||||
|
// simplifying the equation:
|
||||||
|
|
||||||
|
// R2 = R1 / (Vin / Vout - 1)
|
||||||
|
|
||||||
|
// If the ADC reference voltage and voltage divider
|
||||||
|
// source voltage (Vin) are the same, then the
|
||||||
|
// following is true:
|
||||||
|
|
||||||
|
// Vin / Vout = AMAX / AV
|
||||||
|
|
||||||
|
// This means the ratio of voltage divider input
|
||||||
|
// voltage to output voltage is the same as the ratio
|
||||||
|
// of the ADC full range value (AMAX) to the value
|
||||||
|
// returned by the ADC (AV). For a 16 bit ADC like
|
||||||
|
// the ADS1115 the AMAX is 0x7FFF.
|
||||||
|
|
||||||
|
av = adc_raw * (GAIN / VIN);
|
||||||
|
r2 = R1 / (AMAX / av - 1.0f);
|
||||||
|
|
||||||
|
return r2;
|
||||||
}
|
}
|
||||||
|
|
||||||
float SteinhartHart(float res)
|
float SteinhartHart(float res)
|
||||||
@@ -45,6 +66,9 @@ float SteinhartHart(float res)
|
|||||||
|
|
||||||
// Convert resistance to temperature
|
// Convert resistance to temperature
|
||||||
|
|
||||||
|
// When the temperature increases, NTC thermistor
|
||||||
|
// resistance will decrease:
|
||||||
|
|
||||||
// 25C = 10000 Ohms
|
// 25C = 10000 Ohms
|
||||||
// 100C = 6744 Ohms
|
// 100C = 6744 Ohms
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user