2 Temp&Humi Sensors on the same Arduino

Grove is a modulated, ready-to-use tool set.
Much like Lego, it takes a building block approach to assembling electronics.

Moderators: lily.li, violet, jeremy882, crail.lyu969

Post Reply
zws
Pre-kindergarten
Pre-kindergarten
Posts: 28
Joined: Mon May 02, 2011 3:15 am
Are you a staff member of seeedstudio?: no
Which products/projects are your favorite?: aham

2 Temp&Humi Sensors on the same Arduino

Post by zws » Sun Feb 05, 2012 1:20 am

Hey guys,

I am trying to use 2 Temperature & Humidity Sensor Modules (http://www.seeedstudio.com/wiki/Grove-_ ... ity_Sensor) with the same Arduino. As the code for using only one sensor is quite complicated for my skill level, I would really need some help.

Thank you so much,
zws

Code: Select all

#define DHT11_PIN 0      // ADC0



byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
 
 
    while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
    delayMicroseconds(30);
 
    if(PINC & _BV(DHT11_PIN)) 
      result |=(1<<(7-i));
    while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
 
 
  }
  return result;
}
 
 
void setup()
{
  DDRC |= _BV(DHT11_PIN);
  PORTC |= _BV(DHT11_PIN);
 
  Serial.begin(9600);
 
  Serial.println("Ready");
}
 
void loop()
{
  byte dht11_dat[5];
  byte dht11_in;
  byte i;
  // start condition
  // 1. pull-down i/o pin from 18ms
  PORTC &= ~_BV(DHT11_PIN);
  delay(18);
  PORTC |= _BV(DHT11_PIN);
  delayMicroseconds(40);
 
  DDRC &= ~_BV(DHT11_PIN);
  delayMicroseconds(40);
 
  dht11_in = PINC & _BV(DHT11_PIN);
 
  if(dht11_in){
    Serial.println("dht11 start condition 1 not met");
    return;
  }
  delayMicroseconds(80);
 
  dht11_in = PINC & _BV(DHT11_PIN);
 
  if(!dht11_in){
    Serial.println("dht11 start condition 2 not met");
    return;
  }
  delayMicroseconds(80);
  // now ready for data reception
  for (i=0; i<5; i++)
    dht11_dat[i] = read_dht11_dat();
 
  DDRC |= _BV(DHT11_PIN);
  PORTC |= _BV(DHT11_PIN);
 
  byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
  // check check_sum
  if(dht11_dat[4]!= dht11_check_sum)
  {
    Serial.println("DHT11 checksum error");
  }
 
  Serial.print("Current humdity = ");
  Serial.print(dht11_dat[0], DEC);
  Serial.print(".");
  Serial.print(dht11_dat[1], DEC);
  Serial.print("%  ");
  Serial.print("temperature = ");
  Serial.print(dht11_dat[2], DEC);
  Serial.print(".");
  Serial.print(dht11_dat[3], DEC);
  Serial.println("C  ");
 
  delay(2000);
}

schmron
Elementary-1
Elementary-1
Posts: 145
Joined: Mon Dec 05, 2011 6:12 am

Re: 2 Temp&Humi Sensors on the same Arduino

Post by schmron » Sun Feb 05, 2012 2:33 am

http://arduino.cc/playground/Main/DHT11Lib

Use this library.

In this line

Code: Select all

int chk = DHT11.read(DHT11PIN);
you read values from the sensor you define in DHT11PIN.
If your reading is finished you can start another with a new pin.
Best regards from Vienna/Austria
Rony

zws
Pre-kindergarten
Pre-kindergarten
Posts: 28
Joined: Mon May 02, 2011 3:15 am
Are you a staff member of seeedstudio?: no
Which products/projects are your favorite?: aham

Re: 2 Temp&Humi Sensors on the same Arduino

Post by zws » Sun Feb 05, 2012 4:52 am

Thank you for the tip, Rony!

Unfortunately it doesn't work. I tried it with only one sensor. I installed the library, I copied the sketch, I changed the #define DHT11PIN 2 to 0 (as I use that pin), it gives me no errors while verifying.

But once I run it on Arduino, it gives me "Read sensor: Time out error" which by the definition on the link it says "-2 : if a timeout occured, communication failed."

Did you use the library yourself with the Temp&Humi Sensor module from Seeedstudio?

By the way, I tried to use the library from adafruit for the DHT11 and I had problems with that as well (you can check: http://www.ladyada.net/learn/sensors/dht.html for the tutorial from adafruit)

schmron
Elementary-1
Elementary-1
Posts: 145
Joined: Mon Dec 05, 2011 6:12 am

Re: 2 Temp&Humi Sensors on the same Arduino

Post by schmron » Sun Feb 05, 2012 5:16 am

Pin 0 is RX from serial interface. Pin 1 is TX.
You cannot use this pins for your sensor if you use the serial interface.

Yes i use this lib (and also i tested ladyada lib) with this sensor and it works very well.
Best regards from Vienna/Austria
Rony

zws
Pre-kindergarten
Pre-kindergarten
Posts: 28
Joined: Mon May 02, 2011 3:15 am
Are you a staff member of seeedstudio?: no
Which products/projects are your favorite?: aham

Re: 2 Temp&Humi Sensors on the same Arduino

Post by zws » Mon Feb 06, 2012 8:02 am

Hey Rony,

Thank you so so much!

I was stuck on getting some response while using the analogue inputs :D

I just tested 2 and while they are next to each other, they output totally different results. I also let them settle for about 15 minutes before copying the results you see below.

Read sensor 1: OK
Humidity1 (%): 37.00
Temperature1 (oC): 22.00
Read sensor 2: OK
Humidity2 (%): 33.000
Temperature2 (oC): 21.000

Humidity is off by 4% and Temperature by 1oC. :(

It says there on the wiki page that:
Accuracy
Humidity ±5% RH
Temperature ±2 °C
(see http://www.seeedstudio.com/wiki/Grove-_ ... ity_Sensor)

..but reality hit me pretty hard :cry:

schmron
Elementary-1
Elementary-1
Posts: 145
Joined: Mon Dec 05, 2011 6:12 am

Re: 2 Temp&Humi Sensors on the same Arduino

Post by schmron » Mon Feb 06, 2012 7:11 pm

zws wrote:..but reality hit me pretty hard :cry:
If you want a sensor with better tolerance you must cash more then $5 or $15.
For this price the results are very good ;)

You can calibrate your sensor in your software...
Best regards from Vienna/Austria
Rony

Post Reply