using power function with base as DHT22 variable.

Hello, I am a first time arduino user and programmer in general. I have a DHT22 sensor that I am receiving values from and I want to plug in the dht.readHumidity() function and dht.readTemperature() into the power function pow()

if I use the following

float hum=dht.readHumidity()

pow(hum,3) i get 0. even if I use pow(dht.readHumidity(), 3) I still get 0.

but if I use

float hum = 3.0

power (hum, 3) I get 27.

I'm not sure why i cant perform the power function using the variable to get humidity readings from dht22 sensor, bellow is the code im using. The serial monitor shows nan when i print volume. But if I manually plug in numbers for temp and hum I get a value.

//Libraries

#include <DHT.h>
#include <Wire.h>
//Constants
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino


//Variables for DHT22
  //Stores humidity value
  float hum;
  //Stores temperature value
  float temp;


//Variables for Formula
  float volume = (pow(temp,2.5)-hum*pow(temp,-2.5))*(1+0.4*5)*pow(10,-6);
  
void setup()
{
    Serial.begin(9600);  //Initialize serial port - 9600 bps
    dht.begin();

}

void loop()
{
//DHT22 Code 
    delay(1000); // Print value every 1 sec.
//Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature()*1.8+32;
//Print temp and humidity values to serial monitor
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.println(temp);
//END DHT22 Code

 //Calculations for Voume
  Serial.print("Volume: ");
  Serial.println(volume, 4);

  
}

The serial monitor shows nan when i print volume. But if I manually plug in numbers for temp and hum I get a value.

Thank you.

You didn't mention which DHT library you're using.

Did you try printing the result of

float hum=dht.getHumidity;

I'm thinking you may want:

float hum=dht.getHumidity();

Delta_G:
I'm not sure either. You've done something wrong. But you didn't show the code or context so I can't tell what. Put together a MCVE that will compile and run and exhibit the problem that we can look at.

Thank you I will put that together and update this thread this evening.

gfvalvo:
You didn't mention which DHT library you're using.
Did you try printing the result of

float hum=dht.getHumidity;

I'm thinking you may want:

float hum=dht.getHumidity();

I am using dht.getHumidity(). I'm not sure what library I'm using but I have #include <DHT.h> in my code. I went to the arduino libraries section and typed dht22 and downloaded all of them.

Thank you for the replies.

icuriel:
I am using dht.getHumidity(). I'm not sure what library I'm using but I have #include <DHT.h> in my code. I went to the arduino libraries section and typed dht22 and downloaded all of them.

Sorry, guess I misread your post. Thought the () was missing. Anyway, what did you get when you printed the humidity value before using it in the pow() function?

gfvalvo:
Sorry, guess I misread your post. Thought the () was missing. Anyway, what did you get when you printed the value before using it in the pow() function?

I went back and updated the post :slight_smile: thank you for pointing it out.

When I printed the value I got the humidity level in the serial monitor, which was 41.2, but for some reason it just wouldnt work in the function.

Delta_G:
Then why didn't you type that? Can you see why we don't like getting paraphrased code? We'd rather see the real code you really ran so we don't get sent on wild goose chases over typo errors.

Delta_G:
You didn't misread anything. It wasn't there before. The OP went back and edited the first post after it had replies and didn't make any note of the fact. I guess he thinks he is too cool for forum etiquette.

Delta_G:
The pow function works. You must have done something wrong in the code you still haven't shown.

I understand, thank you. Sorry for my lack of forum etiquette, I'm am still new to all of this. I will update with my code as soon as I can.

Delta_G:
Super secret pro tip. If you want to be successful at this kind of stuff, be the kind of person who reads the instructions first. That's the real difference between the ones who can and the ones who can't in this game. It doesn't take a lot of brains to program, just common sense and the tendency to read instructions all the way through and read for understanding FIRST before you start writing. 99% of the questions I answer here, all I am doing is writing what it says in the documentation if people would go RTFM.

How to use this forum - please read

When you post your code, please don't forget the code tags. Read the link above if you don't know what I mean by that.

Understood. I did read the how to use the forum. But I didn't know it was prohibited to post without code at all. I made note and apologized for not having the code readily available and was just asking a general question if the dht.getHumidity() would work or not in the power function.

Thanks for the help thus far. I'll be back this evening with the code. :slight_smile:

I am getting correct value; why not you?

? h = dht.readHumidity();   // h = 99.10
? = pow(h, 2.1);                      //
Serial.println (x, 2);                        //shows: 15550.86

GolamMostafa:
I am getting correct value; why not you?

? h = dht.readHumidity();   // h = 99.10

? = pow(h, 2.1);                      //
Serial.println (x, 2);                        //shows: 15550.86

Because you're using compile-time constants?
Who knows? You didn't post your code either.

Here is the complete codes tested on UNO. (I left those ?'s for the OP to fill up!)

#include "DHT.h"
#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);

void setup() 
{
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  dht.begin();
}

void loop() 
{
  // Wait a few seconds between measurements.
  delay(2000);
  float h = dht.readHumidity();
  Serial.println(h, 2);   //prints humidity value as float 98.50
  float x = pow(h, 2.1);  //shows : hE2.1
  Serial.println(x,2);          //15353.79
}

Ok I have updated the original post with the code. Hopefully I got this right.

*edit

ok I made a bonehead mistake :frowning: I realized I had the power functions before the hum variable. Forgive me!

icuriel:
The serial monitor shows nan when i print volume.

 //Calculations for Voume
  Serial.print("Volume: ");
  Serial.println(volume, 4);

What calculations? I don't see any calculations.

//Variables for Formula
  float volume = (pow(temp,2.5)-hum*pow(temp,-2.5))*(1+0.4*5)*pow(10,-6);

Do you think this updates volume automatically when temp and hum are updated? Well, it doesn't. Put it where you say you do the calculations.

pow(10,-6)

is simply 1.0e-6.

Have you heard about the existence of the following rule of thumb in Arduino Programming?

1. The Arduino IDE comes up only with two functions, and these are:

void setup()
{

}

void loop()
{

}

2. Tasks (jobs) that will be executed only once or for a definite number of times are put in the setup() function.

3. Tasks that are repeatedly executed are placed within the loop() function.

Please, look at the tasks that you have included in your loop() function. It is observed that you are continuously acquiring and displaying humidity and temperature signals at 1-sec interval. You are not displaying the Volume Quantity. Are you? The following is the screenshot of the OutBox of the Serial Monitor after the execution of your posted codes.

Please, modify your codes, post the updated codes so that the OutBox of the Serial comes up as follows showing the Volume Quantity (after the execution of the updated codes).

Thank you very much guys for all of the help. Ive fixed the issue and all is working correctly :). I still have a lot to learn but I shouldnt be making such easy mistakes ><

@OP

It would be much valuable for your part to respect the following quote rather than delivering the cheap Thank.

Please, modify your codes, post the updated codes so that the OutBox of the Serial comes up as follows showing the Volume Quantity (after the execution of the updated codes).