Help with code for DHT11 temp sensor

I am a relative noob and in need of some guidance. I am working from the book "Arduino Workshop" by John Boxall. I am working on project 12 which is to make a temp sensor display to the serial monitor using functions.

So far I am able to get the program to run but the temps don't seem right. Instead of Celsius and Fahrenheit I get some weird numbers like 449.51 deg C and 841.12 deg F. I suspect the conversion part of the code (posted below) is off. I am using a DHT11 3 pin temp/humidity sensor. Some guidance on where to look in my code or what should I looking at to do a proper converson.

thanks
John G.

Code:

//Project 12 - Display temp in serial monitor
#include <dht.h>
dht DHT;

float celsius = 0;
float fahrenheit = 0;

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

void findTemps()
{
  float voltage = 0;
  float sensor = 0;
  // read the temp sensor and convert the result to degrees C & F
  sensor = analogRead(0);
  voltage = (sensor * 5000) / 1024; //covert the raw data to millivolts
  voltage = voltage - 500; //remove the voltage offset
  celsius = voltage  /10; //convert millivots to celsius
  fahrenheit = (1.8 * celsius )+ 32; //convert celsius to fahrenheit
}

void displayTemps()
{
  Serial.print("Temp is ");
  Serial.print(celsius, 2);
  Serial.print(" deg. C/");
  Serial.print(fahrenheit, 2);
  Serial.println(" deg. F/");
  //use prinln here so the next reading starts on a new line
}

void loop() 
{
  findTemps();
  displayTemps();
  delay(1000);
}

There are examples that come with the DHT library. They look NOTHING like your code.

@OP

Are you using this sensor?
dht11Pic.png

This is a digital type sensor, and it produces digital data for Temperature and Humidity; but, your codes are processing analog signals.

Try to run the following sketch (tested on UNO):

#include <dht.h>
dht DHT;

#define DHT11_PIN 2   

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

void loop()
{
  delay(2000); //Delay 2 sec.
  //Read data and store it to variables hum and temp
  DHT.read11(DHT11_PIN);
  int hum = DHT.humidity;
  int temp = DHT.temperature;

  Serial.print("Humidity is = ");
  Serial.println(hum);

  Serial.print("Temperature is = ");
  Serial.println(temp);
  Serial.println("===========================");
}

dht11Pic.png

1 Like

Thank you GolamMostafa. Still learning the in's and out's. Now to figure out how to do the conversions to Fahrenheit.

John

Now to figure out how to do the conversions to Fahrenheit.

Hint: It's REALLY simple math.

I'll bet the class even has a method to do that.

jgarrett129:
Still learning the in's and out's. Now to figure out how to do the conversions to Fahrenheit.

1. I hope you know this formula:
C/5 = (F-32)/9
==> F = ((9/5)*C) + 32 //F = Fahrenheit; C = Celsius

2. Now, write code to evaluate F of Step-2 and include in the main codes of Post#2. Upload and check the result.

3. If working, it will be fine; else, post your codes here; we will help to correct your codes.

One would like to think that the code in a book would match the sensor they cover?

OP's sketch looks like it's for an LM35.

not_a_noob:
One would like to think that the code in a book would match the sensor they cover?

OP's sketch looks like it's for an LM35.

Or that OP would realize that not all temperature sensors are the same.

Looks to be a TMP36 according to this site, assuming it's the same as his book:
https://tronixstuff.com/2010/04/15/getting-started-with-arduino-chapter-two/

Code looks very similar.

/*
example 2.1 - digital thermometer
Created 14/04/2010 ---  By John Boxall --- http://tronixstuff.com ---  CC by-sa v3.0
Uses an Analog Devices TMP36 temperature sensor to measure temperature and output values to the serial connection
Pin 1 of TMP36 to Arduino 5V power socket
Pin 2 of TMP36 to Arduino analog 0 socket
Pin 3 of TMP36 to Arduino GND socket
*/
void setup()
{
Serial.begin(9600);   // activate the serial output connection
}
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float fahrenheit = 0;
void loop()
{              // let's get measurin'
sensor = analogRead(0);
voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-400;        // remove voltage offset
celsius = voltage/10;         // convert millivolts to Celsius
fahrenheit = ((celsius * 1.8)+32); // convert celsius to fahrenheit