Combining Humidity sensor Temp and servo motor

Hi! I'm trying to get the value of temperature from the humidity sensor. and after it reaches a certain temperature, the servo motor rotates. please help. Thanks!

#include <dht.h>
#include <Servo.h>
Servo servo;
dht DHT;
int moto = 8;
int angle = 0;
int tempe = 33;
#define DHT11_PIN 2
#define DHTTYPE DHT11 
void setup(){
  Serial.begin(9600);
  servo.attach(moto);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);

if(chk >= tempe){
  for(angle = 10; angle < 180; angle++)  
  {                                  
    servo.write(angle);               
    delay(15);                   
  } 
}
}

Shouldn't that be:

if(DHT.temperature >= tempe) // if temperature greater than 33

Good job posting code properly.

It worked! Thank you

Do you know why it worked?

// if temperature greater than 33 or equal to 33

Yes

One code line is missing in the following segment of the sketch of your Post-1 to save the temperature in a variable. Can you write that missing line here and then modify your if() structure accordingly?

 int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);

if(chk >= tempe){

i didn't delete other code i've only changed the chk to DHT.temperature and it works. When temperature reaches at ideal temp, the servo motor rotates.

Here's the updated one.

if(DHT.temperature >= tempe)
  {
    for(angle = 10; angle < 180; angle++)
    {
      servo.write(angle);
      delay(10);
    }

This was suggested by @groundFungus in Post-2 and not your work. In Post-8, I have given you an assignment with a hope that you will do it to valiadate you assertion of Post-7 (Yes).

Oh. I thought that @groundFungus was going to explain why it worked. Still trying to decipher on how it worked. would be a great help for an explanation.

1. This is (Fig-1) your DHT11-type Humidity and Temperature Sensor.
dht11-2x
Figure-1:

2. The Sensor converts the humidity into 16-bit data (8-bit for integer part and 8-bit for fractional part). It converts the temperature into 16-bit data (8-bit for integer part and 8-bit for fractional part). There is another 8-bit data called CHKSUM (Checksum) which is for error detection in case there is any data corruption during transmission.

3. To read the 40-bit (5-byte) data from the Sensor, the UNO gives the following command:

DHT.read11(DHT11_PIN);   //Sensor's Signal/DATA pin is connected with DPin-2

The above command has two parts:
DHT (Left part to the . dot) is called object in view of C++ Programming
read11(DHT11_PIN) (Right part to . dot) is called method in view of C++ Programming.

4. The 5-byte (40-bit) data of Step-3 (coming from Sensor) are stored in a an array of the MCU named bits[ ] (not visible to user) which is conceptually shown in Fig-2.
arraydht11
Figure-2:

5. To compute float-type (number with both integer part and
fractional part) Humidity from <bits[0], bits[1]>, the following command is executed.

float myHumi = DHT.humidity;   //this line is missing in sketch of Post-1

As a result, the Humidity value is stored in the variable/identifier named myHumi.

6. Similarly the following command is executed to compute the float-type Temperature.

float myTemp = DHT.Temperature;   //this line is missing in sketch of Post-1

7. Now, telling few words on the need of chk variable in the following command.

int chk = DHT.read11(DHT11_PIN);   

The above command does the following jobs:
(1) It stores humidity, temperature, and CHKSUM data in the array of Fig-2.

(2) It assigns -1 to the variable chk if there is any data corruption during transmission.

(3) It assigns -2 to the variable chk if there is a timeout event between host MCU and sensor. It means the MCU has not received the 40-bit data from Sensor within 4 ms time after issuing the read11() command.

(4) It assigns 0 to the variable chk if neither of the above two errors occur which means that there is good data.

The value of chk variable can be used for diagnostic purposes of a non-functional DHT11 Sensor.

8. Finally, your sketch for UNO would appear as follows (only for Temperature):

#include <dht.h> //header file containing codes for various commands
dht DHT;  //dht = className; DHT = Object
int tempe = 33;   //reference temperature
#define DHT11_PIN 2

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

void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  if (chk != 0)
  {
    Serial.print("There is error!");
    while (1); //wait for ever
  }
  Serial.println(chk, DEC); //see 0 on the screen

  Serial.print("Temperature = ");
  float myTemp = DHT.temperature;
  Serial.println(myTemp, 2);  //2-digit after decimal point

  delay(1000);  //test/sampling interval
}
0
Temperature = 32.00    //why the fractional part is always 0 for my DHT11 sensor?

9. To get fractional part of temperature, I declared myTemp variable as double (as per declaration of my DHT Library) and then uploaded the sketch in DUE Board; surprisingly, the sketch did not work; rather, it announced - There is error!.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.