Humidity calculations

hello, ive hit an issue where my humidity calculations as they are coming up incorrect. im using whats called the wet bulb-dry bulb technique. Hopefully someone here is familiar and can lend a hand as im very new to this and currently tinkercad. the issue with the last part labelled HumidityCalculations
and the equation im following is here

relative humidity equation

             // LCD library
#include <LiquidCrystal.h>

// Initialize the LCD library
LiquidCrystal LCD(12,11,5,4,3,2);

// Defines analog pin A0 as Temperature Sensor input
int SensorTempPin=0;

// Defines analog pin A1 as Temperature Sensor input
int SensorTempPin1=1;


void setup() {
	

	// Defines the number of columns and rows on the LCD
	LCD.begin(16,2);
	// Print the message on the LCD
	LCD.print("Temp: Hum:");
	// Change cursor to first column and second row of LCD
	LCD.setCursor(0,1);
	// Print the message on the LCD
	LCD.print("      &         ");
}

void loop() {
	// Read the voltage on the Temperature Sensor
	int SensorTempVoltage=analogRead(SensorTempPin);

  	// Converts the voltage read
	float Voltage=SensorTempVoltage*5;
	Voltage/=1024;

  	// Converts read voltage to Degrees Celsius
	float TemperatureC=(Voltage-0.5)*100;

  	

  	// Change cursor to first column and second row of LCD
  	LCD.setCursor(0,1);

  	// Prints the temperature in degrees Celsius
	LCD.print(TemperatureC);
  
  
  
  
  	// Read the voltage on the Temperature Sensor
	int SensorTempVoltage1=analogRead(SensorTempPin1);

  	// Converts the voltage read
	float Voltage1=SensorTempVoltage1*5;
	Voltage1/=1024;

  	// Converts read voltage to Degrees Celsius
	float TemperatureC1=(Voltage1-0.5)*100;

  	

  	// Change cursor to first column and second row of LCD
  	LCD.setCursor(8,1);

  	// Prints the temperature in degrees Celsius
	LCD.print(TemperatureC1);


  	
    //HumidityCalculations

	float DryBulb=(17.502*SensorTempVoltage/240.97+SensorTempVoltage);
    float Wetbulb=(17.502*SensorTempVoltage1/240.97+SensorTempVoltage1);  
  	
  	float a=6.112*(2.71828182845904*DryBulb);
    float b=6.112*(2.71828182845904*Wetbulb);
                     
  	float RelativeHumidity = ((b-0.6687451584*(1+0.00115*SensorTempVoltage1)*(SensorTempVoltage-SensorTempVoltage1))/a)*100;                 
	                          
                    
         LCD.setCursor(10,0);
  LCD.print(RelativeHumidity);
}

make sure calculations are correct (parenthesis and pow())

results and code

30.00 20.00 7.28 7.06 2.96
30.00 22.00 7.28 7.10 22.21
30.00 24.00 7.28 7.15 41.54
30.00 26.00 7.28 7.19 60.95
30.00 28.00 7.28 7.24 80.43
30.00 30.00 7.28 7.28 100.00

40.00 30.00 7.50 7.28 4.87
40.00 32.00 7.50 7.33 23.74
40.00 34.00 7.50 7.37 42.69
40.00 36.00 7.50 7.41 61.72
40.00 38.00 7.50 7.46 80.82
40.00 40.00 7.50 7.50 100.00

50.00 40.00 7.71 7.50 6.55
50.00 42.00 7.71 7.54 25.09
50.00 44.00 7.71 7.58 43.71
50.00 46.00 7.71 7.63 62.39
50.00 48.00 7.71 7.67 81.16
50.00 50.00 7.71 7.71 100.00

60.00 50.00 7.91 7.71 8.04
60.00 52.00 7.91 7.75 26.29
60.00 54.00 7.91 7.79 44.60
60.00 56.00 7.91 7.83 63.00
60.00 58.00 7.91 7.87 81.46
60.00 60.00 7.91 7.91 100.00

70.00 60.00 8.10 7.91 9.36
70.00 62.00 8.10 7.95 27.35
70.00 64.00 8.10 7.98 45.40
70.00 66.00 8.10 8.02 63.53
70.00 68.00 8.10 8.06 81.73
70.00 70.00 8.10 8.10 100.00

80.00 70.00 8.28 8.10 10.53
80.00 72.00 8.28 8.14 28.29
80.00 74.00 8.28 8.17 46.11
80.00 76.00 8.28 8.21 64.00
80.00 78.00 8.28 8.25 81.97
80.00 80.00 8.28 8.28 100.00


#define N   0.6687451584

float eDry;
float eWet;
float relH;

void
calc (
    float tDry,
    float tWet )
{
    eDry = 6.112 * pow(M_E, (17.502 + tDry) / (240.97 + tDry));
    eWet = 6.112 * pow(M_E, (17.502 + tWet) / (240.97 + tWet));
    relH = 100 * (eWet - N* (1+ 0.00115* tWet) * ( tDry - tWet)) / eDry;
}

void setup() {
    Serial.begin (9600);

    for (float tDry = 30; tDry <= 80; tDry += 10)  {
        for (float tWet = tDry-10; tWet <= tDry; tWet += 2)  {
            Serial.print (tDry);
            Serial.print (" ");
            Serial.print (tWet);
            Serial.print (" ");

            calc (tDry, tWet);
            
            Serial.print (eDry);
            Serial.print (" ");
            Serial.print (eWet);
            Serial.print (" ");
            Serial.print (relH);
            Serial.println ();
        }
        Serial.println ();
    }
}

void loop()
{
}

Try using the following formula:

//log(e) = 0.4343
//ln(6.112) = 1.8115
log(ew) = x =  0.4343*(1.8115+(17.502*Tw)/(240.87+Tw))
float ew = pow(10, x);

No, do not try this formula:

Neither line is correct. log() is the natural log function, and you cannot assign it a value.

The function exp(x) returns e, the base of natural logarithms, raised to the power of x.

This is how I have derived the formula:

y = (17.502*Tw)/(240.87+Tw)
ew = 6.112*e^y
e^y = ew/6.112
y = ln(ew/6.112)
y = ln(ew) - ln(6.112)
y = ln(ew) - 1.8115
ln(ew) = 1.8115 + y
log(ew)/log(e) = 1.8115 + y
log(ew) = 0.4343*(1.8115 + y) = x
ew = pow(10, x)

This is how I would write the equations for ed and ew:

float ed = 6.112*exp(17.502*Td/(240.97 + Td));
float ew = 6.112*exp(17.502*Tw/(240.97 + Tw));

Anything wrong with my derivation of Post-5?

Yes, several things.

With Tw = 78 and 67, your equation shows: 441.48 and 275.30
With Tw = 78 and 67, my equation shows: 442.66 and 276.01

Where are the bad ingredients in my formula derived for a learner?

float x =  0.4343 * (1.8115 + (17.502 * Tw) / (240.87 + Tw));
float ew = pow(10, x);
Serial.println(ew, 2);

The fact that the entire "derivation" is totally unnecessary, that you don't explain where some of the "magic numbers" come from, and that you end up using pow(10, x) when working with a formula that does not involve powers of 10.

Is your effort intended to be instructive for someone else?

If your constants are carried to enough significant digits, the results are identical (MathCAD carries a and b to a large number of significant digits...only 2 or 3 shown here):
image

But, as @jremington said, the "derivation" is totally unnecessary.

Sorry im major a novice and not entirely sure what changes are required and where... like i said im getting a humidity reading but its incorrect

             
	                          
                    ```
// LCD library
#include <LiquidCrystal.h>

// Initialize the LCD library
LiquidCrystal LCD(12,11,5,4,3,2);

// Defines analog pin A0 as Temperature Sensor input
int SensorTempPin=0;

// Defines analog pin A1 as Temperature Sensor input
int SensorTempPin1=1;

int  N=0.6687451584;

void setup() {
	

	// Defines the number of columns and rows on the LCD
	LCD.begin(16,2);
	// Print the message on the LCD
	LCD.print("Temp: Hum:");
	// Change cursor to first column and second row of LCD
	LCD.setCursor(0,1);
	// Print the message on the LCD
	LCD.print("      &         ");
}

void loop() {
	// Read the voltage on the Temperature Sensor
	int SensorTempVoltage=analogRead(SensorTempPin);

  	// Converts the voltage read
	float Voltage=SensorTempVoltage*5;
	Voltage/=1024;

  	// Converts read voltage to Degrees Celsius
	float TemperatureC=(Voltage-0.5)*100;

  	

  	// Change cursor to first column and second row of LCD
  	LCD.setCursor(0,1);

  	// Prints the temperature in degrees Celsius
	LCD.print(TemperatureC);
  
  
  
  
  	// Read the voltage on the Temperature Sensor
	int SensorTempVoltage1=analogRead(SensorTempPin1);

  	// Converts the voltage read
	float Voltage1=SensorTempVoltage1*5;
	Voltage1/=1024;

  	// Converts read voltage to Degrees Celsius
	float TemperatureC1=(Voltage1-0.5)*100;

  	

  	// Change cursor to first column and second row of LCD
  	LCD.setCursor(8,1);

  	// Prints the temperature in degrees Celsius
	LCD.print(TemperatureC1);
  	
    //HumidityCalculations

float a = 6.112*exp(17.502*SensorTempVoltage/(240.97 + SensorTempVoltage));
float b = 6.112*exp(17.502*SensorTempVoltage1/(240.97 + SensorTempVoltage1));
                     
  	float RelativeHumidity = ((b-0.6687451584*(1+0.00115*SensorTempVoltage1)*(SensorTempVoltage-SensorTempVoltage1))/a)*100;                 
	                          
                    
         LCD.setCursor(10,0);
  LCD.print(RelativeHumidity);
}

Please, see Post-5 for the sources of the so called magic numbers. They are clearly developed.

The derivation was necessary for me as I was not aware of the existence of exp() function.

The pity thing was that you told the OP not to use the formula when you were totally unable to pin-point the pitfalls of the formula.

In my process, pow(10, x) gives the anti-log.

But not natural anti-log, which is confusing as the equations are all given using powers of e.

Physics uses e^x and ln(x) precisely because that's how nature (and mathematics come to that) works.

In C/C++ exp(x) calculates e^x and log(x) calculates ln(x). log10() and pow() might be appropriate for decibel conversions but are complicating things here.

I am not required natural anti-log; but, the decimal anti-log. Moreover, a man with ignorance moves in a way that his inquisitive mind thinks logical though primitive.

The original expression of the OP is:

ew = 6.112*e^ ((17.502*Tw)/(240.87+Tw))
Let us take: y = (17.502*Tw)/(240.87+Tw)

==> ew = 6.112*e^y
==> e^y = ew/6.112
==> y = ln(ew/6.112)
==> y = ln(ew) - ln(6.112)
==> y = ln(ew) - 1.8115
==> ln(ew) = 1.8115 + y
==> log(ew)/log(e) = 1.8115 + y
==> log(ew)/0.4343 = 1.8115 + y
==> log(ew) = 0.4343*(1.8115 +y)
==> log(ew) = 04343*(1.8115+(17.502*Tw)/(240.87+Tw))

Implementation:
float x =  0.4343*(1.8115+(17.502*Tw)/(240.87+Tw)); //for Tw = 78, x = 2.6460
where: x = log(ew)

==> ew = 10^x

Implementation:
float ew = pow(10, x);  //10^2.6460 = 442.59

Final Codes:
float x =  0.4343*(1.8115+(17.502*Tw)/(240.87+Tw)); 
float ew = pow(10, x);  //10^2.6460 = 442.59
Serial.println(ew, 2);

The point is you are making a simple expression more complicated for no reason.

This is physics: everyone uses natural logs and e because its natural and simpler.

My approach appeared complicated because you know the exp() function. It was rather enjoyable to me to see the log transformation from one base to another that I saw in school 52 years ago.

What is M_E? something exponential

it is the value of "e" that is raised to the power of the value in the parenthesis in the Relative Humidity Equations you posted. values such as e or pi are defined in math.h