[SOLVED] Can I run 2 identical DHT sensors at the same time?

Hi! I’ve been using RaspberryPi + GrovePi to gather temperature and humidity data using the DHT sensor. I am making an outdoor case that can protect the device from the weather while at the same time can still retrieve the real data from the atmosphere, not only from inside of the box.

So, for some reason, I got an idea to run 2 sensors at the same time (1 sensor out of the box, 1 sensor in the box) so that I can see if they got the same data and make a calibration. There are 2 cases that I tried:

  1. Run the two sensors consecutively
  2. Run the two sensors simultaneously

The script for the first case is:

import grovepi
import math
import time
 
sensor1 = 3
sensor2 = 4
blue = 0
white = 1
 


with open('TH.csv', 'a') as th:
     th.write("utc;temperature(1);humidity(1);temperature(2);humidity(2)" + '\n')
     while True:
         try:
             [temp1, humidity1] = grovepi.dht(sensor1, white)
             [temp2, humidity2] = grovepi.dht(sensor2, white)
             if not math.isnan(temp1) or math.isnan(humidity1) or math.isnan(temp2) or math.isnan(humidity2):
                 utc=time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime())
                 print(utc + ";" + "temp1 = %.02f C humidity1 =%.02f%%"%(temp1, humidity1) + '\n' + utc + ";" + "temp2 = %.02f C humidity2 =%.02f%%"%(temp2, humidity2))
                 th.write(utc + ';' + str(temp1) + ';' + str(humidity1) + ";" + str(temp2) + ';' + str(humidity2) + '\n')
             time.sleep(1)
  
         except IOError:
             print("Error")

and the scripts for the second case are:

import grovepi
import math
import time
 
sensor1 = 3
 
blue = 0
white = 1
 
with open('TH1.csv', 'a') as th1:
    while True:
        try:
            [temp1, humidity1] = grovepi.dht(sensor1, white)
            if not math.isnan(temp1) and not math.isnan(humidity1):
                time1=time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime())
                print(time1 + "temp1 = %.02f C humidity1 =%.02f%%"%(temp1, humidity1))
                th1.write(time1 + ';' + str(temp1) + ';' + str(humidity1) + '\n')
 
            time.sleep(1)
 
        except IOError:
            print("Error")

and

import grovepi
import math
import time
 
sensor2 = 4
 
blue = 0
white = 1
 
with open('TH2.csv', 'a') as th2:
    while True:
        try:           
            [temp2, humidity2] = grovepi.dht(sensor2, white)
            if not math.isnan(temp2) and not math.isnan(humidity2):
                time2=time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime())
                print(time2 + "temp2 = %.02f C humidity2 =%.02f%%"%(temp2, humidity2))
                th2.write(time2 + ';' + str(temp2) + ';' + str(humidity2) + '\n')
 
            time.sleep(1)
 
        except IOError:
            print("Error")

The first case print the result as 1 CSV file and the later 2 separate CSV files.

However, running both cases, the readings from both sensors always show the same value, even when one of the sensor is isolated and I blew the other one.

My question is, is it even possible to run 2 identical DHT sensors at the same time? If yes, how to get the readings from these 2 separate sensor?

Many thanks in advance!

Hi @calladium,

Unfortunately, at the moment, there’s no way of connecting 2 Grove DHT Sensors to the same GrovePi and make it work.

This simply isn’t possible because the GrovePi has a single serial port that goes to the Atmel chip.


One possible workaround for this challenge would be to create a logical selector, that based on an input, selects one of the 2 Grove DHT Sensors.

So the logical selector would have 3 inputs and 1 output:

  • 1x serial line for DHT sensor #1.

  • 1x serial line for DHT sensor #2.

  • An input called X.
    When X is LOW it selects DHT sensor #1.
    When X is HIGH it selects DHT sensor #2.

  • A serial output that goes to SER (serial port) on the GrovePi. The serial port will forward one of the 2 DHT Sensors depending on X input.

Then I’d hook the input pin X of the selector to a grove connector.

With this technique, you are able to select from within the Python script which of the 2 Grove DHT Sensors work at a moment.

If you are up to, this can turn into a really cool project.


Hope this offers you some more clarification on this matter.

Thank you!

Hi @RobertLucian,
Thank you so much for your thoughtful reply!
Regarding your idea, it would be an interesting experiment to try one day, but unfortunately at the moment it could not be used for calibration. I guess it’s safe to say that for now I can neglect the calibration task.
The DHT sensor will have 2 output which are humidity and temperature, so if we want to split the sensor usage as you said, we can only split it by the value of either one of the output (e.g: a certain value of temperature), right?

Hi @calladium,

The DHT sensor will have 2 output which are humidity and temperature, so if we want to split the sensor usage as you said, we can only split it by the value of either one of the output (e.g: a certain value of temperature), right?

Can you rephrase the quoted paragraph again? I’m not sure what you’re referring at.

Thank you!