Temp&Humi Sensor code does not output decimals

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

Temp&Humi Sensor code does not output decimals

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

Hey guys,

The code that I found on the seeed wiki does not output decimal numbers even though it should by the looks of how the code is constructed.

Any ideas what the problem is?

Thank you,
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: Temp&Humi Sensor code does not output decimals

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

What returns your DHT11?

If you mean your DHT11 returns only a integer... this is correct. DHT11 dont returns floating point numbers.
The "DEC" in Serial.print means the value to print is a number.
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: Temp&Humi Sensor code does not output decimals

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

Hey, thank you, Rony!

What you are saying is that the normal Temp&Humi Sensor only outputs integers. If I want to output floating point numbers I should get the Pro version (http://www.seeedstudio.com/depot/grove- ... th=144_147), right?

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

Re: Temp&Humi Sensor code does not output decimals

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

No.
DHT11 and DHT22 show both integer values.
With calculation value to °C or °F or whatelse you get a float value.
Sensors _always_ give you a integer.

The Example from Wiki shows you only the raw output from sensor.

If you want better software use the DHTlib from arduino.cc (or ladyada).
Best regards from Vienna/Austria
Rony

Post Reply