Using two DHT11 at same time

Hello, does anyone knows how to use two DHT11 sensors at same time?

There is a tutorial about using DHT11s here.

They say:

You can add as many DHT sensors as you line on individual pins, just add new lines such as

DHT dht2 = DHT(pin, type);

below the declaration for the initial dht object, and you can reference the new dht2 whenever you like.

Just connect the data pin of each sensor to a different pin

Thank you! so how should i do it in this code?
Im relative new at arduino...
For example i write: #define DHT11_PIN 8 how should i continue?

/*
   Program to demonstrate Data Logging/Visualisation using Arduino

   ###Connection with SD card module###
   Vcc->5V
   Gnd->Gnd
   MISO->pin 12
   MOSI->pin 11
   SCK-> pin 13
   CS-> pin 4

   ###Connection with DS3231###
   Vcc->5V
   Gns->Gnd
   SCL->pin A5
   SDA-> pin A4

   ###Connection with DT11###
   Vcc->5V
   Gnd->Gnd
   Out-> pin 7


*/

#include <RTClib.h> //Library for RTC module (Download from Link in article)
#include <Wire.h>
#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include <dht.h> //Library for dht11 Temperature and Humidity sensor (Download from Link in article)

#define DHT11_PIN 7 //Sensor output pin is connected to pin 7
dht DHT; //Sensor object named as DHT
RTC_DS3231 rtc;

const int chipSelect = 4; //SD card CS pin connected to pin 4 of Arduino

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

void loop() {
  Write_SDcard();
  delay(5000); //Wait for 5 seconds before writing the next data
}

void Write_SDcard() {
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  int chk = DHT.read11(DHT11_PIN);
  float t = DHT.temperature;
  float h = DHT.humidity;
  Serial.println(t, 1);
  Serial.println(h, 1);
  DateTime now = rtc.now();

  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    char bufer[50];
    sprintf(bufer, "%02u.%02u.%02u,%02u:%02u:%02u,%0u.%0u,%0u.%0u\r\n",
            now.year(),
            now.month(),
            now.day(),
            now.hour(),
            now.minute(),
            now.second(),
            uint16_t(floor(t)), uint16_t(t * 10) % 10,
            uint16_t(floor(h)), uint16_t(h * 10) % 10
           );
    dataFile.print(bufer);
    dataFile.close(); //Close the file
  }
  else
    Serial.println("OOPS!! SD card writing failed");
}

void Initialize_SDcard() {
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println("Date,Time,Temperature,Humidity"); //Write the first row of the excel file
    dataFile.close();
    }

}

Write another line, just like that but with a different pin number and a different name

#define DHT11_PIN1 7
#define DHT11_PIN2 8

and then, create two DHT objects

dht DHT1;
dht DHT2;

and then, everywhere in your code, you use the variable DHT1 if you want to talk to sensor 1 and DHT2 if you want to talk to sensor 2

Thank you!
Do you know what i did wrong? The values in the serial monitor of the second sensor are always 0,00 and in the file on the sd card they aren`t mentioned...

/*
   Program to demonstrate Data Logging/Visualisation using Arduino

   ###Connection with SD card module###
   Vcc->5V
   Gnd->Gnd
   MISO->pin 12
   MOSI->pin 11
   SCK-> pin 13
   CS-> pin 4

   ###Connection with DS3231###
   Vcc->5V
   Gns->Gnd
   SCL->pin A5
   SDA-> pin A4

   ###Connection with DT11###
   Vcc->5V
   Gnd->Gnd
   Out-> pin 7


*/

#include <RTClib.h> //Library for RTC module (Download from Link in article)
#include <Wire.h>
#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include <dht.h> //Library for dht11 Temperature and Humidity sensor (Download from Link in article)

#define DHT11_PIN1 7 //Sensor output pin is connected to pin 7
#define DHT11_PIN2 8
dht DHT1; //Sensor object named as DHT
dht DHT2;
RTC_DS3231 rtc;

const int chipSelect = 4; //SD card CS pin connected to pin 4 of Arduino

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

void loop() {
  Write_SDcard();
  delay(5000); //Wait for 5 seconds before writing the next data
}

void Write_SDcard() {
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  int chk = DHT1.read11(DHT11_PIN1);DHT2.read11(DHT11_PIN2);
  float t = DHT1.temperature;
  float h = DHT1.humidity;
  float t1 = DHT2.temperature;
  float h1 = DHT2.humidity; 
  Serial.println(t, 1);
  Serial.println(h, 1);
  Serial.println(t1, 1);
  Serial.println(h1, 1);
  DateTime now = rtc.now();

  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    char bufer[50];
    sprintf(bufer, "%02u.%02u.%02u,%02u:%02u:%02u,%0u.%0u,%0u.%0u\r\n",
            now.year(),
            now.month(),
            now.day(),
            now.hour(),
            now.minute(),
            now.second(),
            uint16_t(floor(t)), uint16_t(t * 10) % 10,
            uint16_t(floor(h)), uint16_t(h * 10) % 10,
            uint16_t(floor(t1)), uint16_t(t * 10) % 10,
            uint16_t(floor(h1)), uint16_t(h * 10) % 10

           );
    dataFile.print(bufer);
    dataFile.close(); //Close the file
  }
  else
    Serial.println("OOPS!! SD card writing failed");
}

void Initialize_SDcard() {
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println("Date,Time,Temperature,Humidity"); //Write the first row of the excel file
    dataFile.close();
    }
  }


data logger 5

I think the SD is not getting the data from the DHT2 because the format string should be

`sprintf(bufer, "%02u.%02u.%02u,%02u:%02u:%02u,%0u.%0u,%0u.%0u,%0u.%0u,%0u.%0u\r\n",`
//                 YY   MM   DD   hh   mm   ss  t1  t1  h1  h1  t2  t2  h2  h2

And, just to check what may be wrong with DHT2 I would do several tests, such as
switching pins in the following declarations

In case you now get good readings from DHT2 and not from DHT1, I would think of a hardware issue. Check that both sensors are of the same type, especially if you are using the kind that comes in a little board. The board pinouts can be different.

1 Like

This format string defines 10 fields. You have provided 4 additional ones (the second sensor) but have not included them in the specification.

It appears you need to take some time and learn a bit about C/C++ just like we all did.

Also,

needs to use the variables t1 and h1 in both equations [the ones after the comma]

As for why you are reading 0 - is it wired up correctly? How about a picture of your setup?

1 Like

Thank you very much!! Now it works like i wanted!!

Now everything is perfect, my setup is a bit chaotic, but i will revise it...:slight_smile:
Yeah, i will learn a more about C/C++

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