Trouble with 2 DHT11 sensors


I want to start by saying that I,m completely new on programming and I haven’t been using english sience school, so please bear with me. :wink:

I want to use 2 DHT11 sensors to control a fan by starting the fan when the temperature is higher on one of them, but I don’t know how to define witch sensor to use in the “if-statement”.

Here is the code so far:

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT1 7

#define DHT2 8

const int FAN = 1;

void setup() {
  lcd.begin(16, 2);

  pinMode(FAN, OUTPUT);

void loop()
  int chk = DHT.read11(DHT1);
  lcd.setCursor(0, 0);
  lcd.print("Temp : ");


  int chk2 = DHT.read11(DHT2);
  lcd.setCursor(0, 1);
  lcd.print("Temp2: ");


  if (DHT.temperature < DHT1.temperature)

    digitalWrite(FAN, HIGH);
  else {
    digitalWrite(FAN, LOW);


Styrning.Solpanel.ino (700 Bytes)

if ( chk < chk2)

Also lcd.print(chk)

“if (chk<chk2)” just makes the fan go all the time.

Where do you want "lcd.print(chk)?

Then the condition is true. Chk is less than chk2.

Print(DHT.temperature) Is non-descript. Replace with chk and chk2 which are the values from the sensors.

I mean that it doesn’t matter if I put a “>” or “<” in the code, or in witch order I put chk/chk2. The fan is on anyways.

The DHT11 reads and both temperature and humidity, does that have anything to do with it?

@mistergreen: What do the values chk and chk2 represent?
Are those values directly relevant to the control of the fan?

oh, which DHT lib are you using?
Getting the temp will be different depending on the lib.

I got the library from this page:


If i write (DHT.temperature < 22) the fan will go on when the temperature is higher than 22*C on “DHT2”, so that bit works. It fells like it should be pretty easy, but I can’t figure out how to make it read the temperature on the first sensor in the “if-statement”.

I’d recommend using a new library that’ll work with the new Arduino IDE

You’re using an old lib. The link will show you how to use it. You’ll need to make 2 instances of the DHT class for the 2 sensors

#include "DHT.h"

#define DHTPIN1 7     // what digital pin we're connected to
#define DHTPIN2 8     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// 2 instances for 2 sensors

void setup() {
  Serial.println("DHTxx test!");


void loop() {
  // Wait a few seconds between measurements.

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)

  // Read temperature as Fahrenheit (isFahrenheit = true)
  float temp1 = dht1.readTemperature();
  float temp2 = dht2.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(temp1) || isnan(temp2)) {
    Serial.println("Failed to read from DHT sensor!");

  if(temp1 < temp2) {
        digitalWrite(FAN, HIGH);
   else {
       digitalWrite(FAN, LOW);