1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Grove Temperature and Humidity Sensor

Discussion in 'Arduino' started by x_641zzz, Apr 8, 2013.

  1. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    Hi, I am connecting Grove Temp & Hum Sensor to XBee Carrier + Wifi Bee, I am using the library in the Wiki and use the example like this :
    Code (text):
    // Example testing sketch for various DHT humidity/temperature sensors
    // Written by ladyada, public domain

    #include "DHT.h"

    #define DHTPIN 2     // what pin we're connected to

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

    // Connect pin 1 (on the left) of the sensor to +5V
    // Connect pin 2 of the sensor to whatever your DHTPIN is
    // Connect pin 4 (on the right) of the sensor to GROUND
    // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

    DHT dht(DHTPIN, DHTTYPE);

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

      dht.begin();
    }

    void loop() {
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      float t = dht.readTemperature();

      // check if returns are valid, if they are NaN (not a number) then something went wrong!
      if (isnan(t) || isnan(h)) {
        Serial.println("Failed to read from DHT");
      } else {
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print(" %\t");
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.println(" *C");
      }
    }
    but the result is :
    Code (text):
    DHTxx test!
    Read failFailed to read from DHT
    Humidity: 0.00 %   Temperature: 0.00 *C
    Humidity: 0.00 %   Temperature: 0.00 *C
    Humidity: 0.00 %   Temperature: 0.00 *C
    ==============
    and then I tried to use another code :

    Code (text):
    /* YourDuino.com Example Software Sketch
       DHT11 Humidity and Temperature Sensor test
       Credits: Rob Tillaart
       http://arduino-direct.com/sunshop/index.php?l=product_detail&p=162
       terry@yourduino.com */
       
    /*-----( Import needed libraries )-----*/
    #include <dht11.h>

    /*-----( Declare objects )-----*/
    dht11 DHT11;

    /*-----( Declare Constants, Pin Numbers )-----*/
    #define DHT11PIN 2

    void setup()   /*----( SETUP: RUNS ONCE )----*/
    {
      Serial.begin(9600);
      Serial.println("DHT11 TEST PROGRAM ");
      Serial.print("LIBRARY VERSION: ");
      Serial.println(DHT11LIB_VERSION);
      Serial.println();
    }/*--(end setup )---*/

    void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
    {
      Serial.println("\n");

      int chk = DHT11.read(DHT11PIN);

      Serial.print("Read sensor: ");
      switch (chk)
      {
        case 0: Serial.println("OK"); break;
        case -1: Serial.println("Checksum error"); break;
        case -2: Serial.println("Time out error"); break;
        default: Serial.println("Unknown error"); break;
      }

      Serial.print("Humidity (%): ");
      Serial.println((float)DHT11.humidity, 2);

      Serial.print("Temperature (oC): ");
      Serial.println((float)DHT11.temperature, 2);

      Serial.print("Temperature (oF): ");
      Serial.println(Fahrenheit(DHT11.temperature), 2);

      Serial.print("Temperature (K): ");
      Serial.println(Kelvin(DHT11.temperature), 2);

      Serial.print("Dew Point (oC): ");
      Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

      Serial.print("Dew PointFast (oC): ");
      Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));

      delay(2000);
    }/* --(end main loop )-- */

    /*-----( Declare User-written Functions )-----*/
    //
    //Celsius to Fahrenheit conversion
    double Fahrenheit(double celsius)
    {
            return 1.8 * celsius + 32;
    }

    //Celsius to Kelvin conversion
    double Kelvin(double celsius)
    {
            return celsius + 273.15;
    }

    // dewPoint function NOAA
    // reference: http://wahiduddin.net/calc/density_algorithms.htm
    double dewPoint(double celsius, double humidity)
    {
            double A0= 373.15/(273.15 + celsius);
            double SUM = -7.90298 * (A0-1);
            SUM += 5.02808 * log10(A0);
            SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
            SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
            SUM += log10(1013.246);
            double VP = pow(10, SUM-3) * humidity;
            double T = log(VP/0.61078);   // temp var
            return (241.88 * T) / (17.558-T);
    }

    // delta max = 0.6544 wrt dewPoint()
    // 5x faster than dewPoint()
    // reference: http://en.wikipedia.org/wiki/Dew_point
    double dewPointFast(double celsius, double humidity)
    {
            double a = 17.271;
            double b = 237.7;
            double temp = (a * celsius) / (b + celsius) + log(humidity/100);
            double Td = (b * temp) / (a - temp);
            return Td;
    }

    /* ( THE END ) */
    and the result is :
    Code (text):
    DHT11 TEST PROGRAM
    LIBRARY VERSION: 0.3.2



    Read sensor: Time out error
    Humidity (%): 0.00
    Temperature (oC): 0.00
    Temperature (oF): 32.00
    Temperature (K): 273.15
    Dew Point (oC): nan
    Dew PointFast (oC): nan
    =====
    I tried another code again from here :
    Code (text):
    #define dht11_pin 14 //Analog port 0 on Arduino Uno
    //#define dht11_pin 54 //Analog port 0 on Arduino Mega2560
     
    byte read_dht11_dat()
    {
      byte i = 0;
      byte result=0;
      for(i=0; i< 8; i++)
      {
        while (!digitalRead(dht11_pin));
        delayMicroseconds(30);
        if (digitalRead(dht11_pin) != 0 )
          bitSet(result, 7-i);
        while (digitalRead(dht11_pin));
      }
      return result;
    }
     
     
    void setup()
    {
      pinMode(dht11_pin, OUTPUT);
      digitalWrite(dht11_pin, HIGH);
      Serial.begin(9600);
      Serial.println("Ready");
    }
         
    void loop()    
    {
      byte dht11_dat[5];  
      byte dht11_in;
      byte i;// start condition
           
      digitalWrite(dht11_pin, LOW);
      delay(18);
      digitalWrite(dht11_pin, HIGH);
      delayMicroseconds(1);
      pinMode(dht11_pin, INPUT);
      delayMicroseconds(40);    
       
      if (digitalRead(dht11_pin))
      {
        Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
        delay(1000);
        return;
      }
      delayMicroseconds(80);
      if (!digitalRead(dht11_pin))
      {
        Serial.println("dht11 start condition 2 not met");  //wair for second response signal:HIGH
        return;
      }
       
      delayMicroseconds(80);// now ready for data reception
      for (i=0; i<5; i++)
      {  dht11_dat[i] = read_dht11_dat();}  //recieved 40 bits data. Details are described in datasheet
       
      pinMode(dht11_pin, OUTPUT);
      digitalWrite(dht11_pin, HIGH);
      byte dht11_check_sum = dht11_dat[0]+dht11_dat[2];// 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("temperature = ");
      Serial.print(dht11_dat[2], DEC);
      Serial.println("C  ");
      delay(2000); //fresh time
    }
    the result :
    Code (text):
    Ready
    dht11 start condition 1 not met
    dht11 start condition 1 not met
    dht11 start condition 1 not met
    dht11 start condition 1 not met
    dht11 start condition 1 not met
    dht11 start condition 1 not met
    I was wondering that the cause is the I/O pin, in both code above they use pin 2 but when I googling around I found that the code is for DHT11 like this :
    [​IMG]
    pin 1 = VCC
    pin 2 = Signal/Data
    pin 3 = NC/Not Connected
    pin 4 = Ground
    not a Grove like I am using :
    [​IMG]
    that's why the codes above use pin 2, so I tried to change it to pin 5 as discribed in Xbee Carrier Wiki, although I am not sure that pin 5 means D5 and pin 6 means D6 Grove Connector:
    [​IMG]
    and when I carefully look at the cable, everything is at the right place :
    Cable Colour == Xbee Carrier == Grove Temp Hum
    Yellow == D5 == SIG
    White == D6 == NC
    Red == VCC == VCC
    Black == Ground == Ground
    but the result is the same, timeout or communication failed

    I also asked on "Question & Answer" section in Xbee Carrier product guide and Jacket Chueng answered :
    anyone can give me some more detail what should I do? I am a newbie on Arduino and microcontroller, I just want to make a simple project and develop it later after this has been success.

    #sorry for my bad english
     
  2. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Hi,

    Maybe you should try accomplishing the tasks part by part, rather than trying on all aspects at once.

    I recommend that you try reading the output using a scope, since it is a serial output. Then if it produces the changes accordingly, we can try to deal with the programming?

    Cheers.
     
  3. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    Sorry, I'm a totally newbie, I didn't get what you mean clearly. I got those codes above from seeeduino Wiki, dfrobot, and other source, they seems using same library from here, I thought the codes are ready to use because in the wiki there is no more detail explanation, it says that I should open the example sketch and need a little modification depends on what I need such as on what DHT Type and which pin the output is used.
    I am only able reading the first code from here because the code is simple enough to understand, it only defines the DHT type, the output pin, read the temperature and humidity, and then check if the value is valid and if it NaN then it will print the error message, since the result shows error message then the value is NaN.
    Can you explain me step by step how to solve this?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia

    I hope I could. You need to start with basic electronics. If I were you, I'd test the sensor package first. Make the connections for Vcc (just use a 5V output) and GND. Then, get an oscilloscope (from your college, there should be one good enough for your application).

    If it works fine, then a pulsed output from the pin 2 of the sensor package should be observed from the oscilloscope. Any changes to the temperature around the sensor will show some changes on the pulse profile as well.

    If that part is done, you can start a simple programming scheme. Check whether your microcontroller communicates properly with the sensor package.

    Start part by part.

    Cheers.
     
  6. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    I asked my friend to check it using oscilloscope, it only shows the voltage but when he checked the SIG and GND it shows nothing, even he tried to change the temperature around the sensor with blowing his warm breath to the sensor, the pulse didnt change.
     
  7. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    So there is a pulse signal output, is it? What do mean by nothing? Is there no output at all, or a DC output, or an unchanged pulse output?

    If there is a pulse, then you're on the right track. It's only getting the sensor to respond properly is the next thing. Use a hairdryer to blow hot air. It takes time to respond only to warm breaths since it is too transient for detection. Standard temperature sensors takes a few seconds to respond even when you grip it with your hand.

    Vizier87.
     
  8. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    Sorry for my bad english, it's difficult to find the right words to describe what I wanted to say..
    So, the sensor was not responding, and the shop suggested me to send it back to them so they can help to check if the sensor is fine.
     
  9. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    okay then. If that part is clear, we can work on your serial programming after that. Good luck! :)
     
  10. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    They said the sensor is fine, and someone said that the sketches I used above don't match for xbee carrier, but matches for simple board like Uno, Mega, etc. and he said that I should build simple web server which can read sensor data. I've used sketch from Wifi Bee Wiki :

    Code (text):
    /*
     * A simple sketch that uses WiServer to serve a web page
     */


    #include <WiServer.h>

    // Wireless configuration parameters ----------------------------------------
    unsigned char local_ip[]    = {192,168,234,55};   // IP address of WiShield
    unsigned char gateway_ip[]  = {192,168,234,1};   // router or gateway IP address
    unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
    char ssid[]                 = {"Connectify-me"};   // max 32 bytes
    unsigned char security_type = 3;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

    // WPA/WPA2 passphrase
    const prog_char security_passphrase[] PROGMEM = {"0987654321"}; // max 64 characters

    // WEP 128-bit keys
    prog_uchar wep_keys[] PROGMEM = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,   // Key 0
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 1
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 2
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00    // Key 3
    };

    // setup the wireless mode; infrastructure - connect to AP; adhoc - connect to another WiFi device
    #define WIRELESS_MODE_INFRA 1
    #define WIRELESS_MODE_ADHOC 2
    unsigned char wireless_mode = WIRELESS_MODE_INFRA;
    unsigned char sensorValue;
    unsigned char ssid_len;
    unsigned char security_passphrase_len;
    // End of wireless configuration parameters ----------------------------------------


    int counter;
    // This is our page serving function that generates web pages
    boolean sendMyPage(char* URL) {
     
        // Check if the requested URL matches "/"
        if (strcmp(URL, "/") == 0) {
            // Use WiServer's print and println functions to write out the page content
            WiServer.print("<html>");
            // Instruct the web broweser to refersh every 10 seconds
            WiServer.print("<head><meta http-equiv=\"refresh\" content=\"10\"> </head>");
            WiServer.println("<H1>WifiBee Connected...<H1>");
            WiServer.print("<H2>Sensor Value = ");
            WiServer.print(sensorValue);  // connected to an exernal sensor. The RAW value is send to the client.
            WiServer.print("</H2>");
            WiServer.print("</html>");
            // URL was recognized
            return true;
        }
        // URL not found
        return false;
    }


    void setup() {
      // Initialize WiServer and have it use the sendMyPage function to serve pages
      WiServer.init(sendMyPage);
     
      // Enable Serial output and ask WiServer to generate log messages (optional)
      Serial.begin(57600);
      WiServer.enableVerboseMode(true);
    }

    void loop(){
      sensorValue = analogRead(5);  // connected to an exernal sensor via Grove interface in Grove - XBee Carrier .
      // Run WiServer
      WiServer.server_task();
     
      delay(10);
    }

     
    And the result is :
    Code (text):
    WifiBee Connected...
    Sensor Value = xxx
    but it is only RAW value a.k.a analog value, how to convert it to temp & humid data?
     
    Last edited: May 8, 2013
  11. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Hi,

    Did the sensor value change when you tried to introduce any change (like blowing with hot air) or did it just stick to only xxx? You can write a simple code to multiply or divide the digital value, assuming it has a linear correlation with the temperature and humidity. This is an example:

    Let's say:
    0xFF=100°C
    and 0x00=0°C


    A linear function is y=mx+c, x is the digital sensor value, y is the value to be displayed.
    Therefore y=100x

    If the correlation is not linear, then you need to find out using a commercial hygrometer & thermometer and calibrate the sensor, then you can use a proper transfer function into your algorithm.

    Hope this helps? :)
     
  12. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    yes the sensor value changes even I didn't introduce any change, you can see the sensor value here (44 secs) :
    [video=youtube_share;RqbTDmyV_xY]http://youtu.be/RqbTDmyV_xY[/video]
    I set the Wifi Bee to refresh the browser every 1 sec, as you can see that the value changes automatically, according to the datasheet :
    [​IMG]
    this sensor is 8bit, so the range of conversion from analog to digital is 0-255, but what I dont understand is why the value changes extremely from 255 to 0 and vice versa?
    and the measurement range is 0*-50* so when digital value shows 0 it means 0* and when digital value shows 255 it means 50*?

    I need to understand this before calibrate the sensor
     
  13. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    I just want to extract temperature & humidity value from DHT11, but dont know what command to use.. based on the datasheet :

    how can i get those datas??
     
  14. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Hi,

    Sorry for the late response, I was quite busy these few days. The video was set to private, I can't view it.

    I'm not familiar with the library (yes, it is in C) but I noticed the communication part of the program only seems to be this:
    Code (text):
    void setup() {
      Serial.begin(9600);
      Serial.println("DHTxx test!");
     
      dht.begin();
    }
    This code setups your serial communication, so can you test if you can display differently if you overwrite:

    Serial.println("Test!");

    Or directly write the data into the display?

    I'll see if I can pitch more into the code.
     
  15. x_641zzz

    x_641zzz New Member

    Joined:
    Apr 8, 2013
    Messages:
    8
    Likes:
    0
    Location:
    Between Heaven n Hell
    sorry, my bad.. I've just changed the video setting to public..

    When I am writing this reply I look again to Wifi Bee wiki for pin definition :
    [​IMG]

    and try again the sketch :
    Code (text):
    // Example testing sketch for various DHT humidity/temperature sensors
    // Written by ladyada, public domain

    #include <DHT.h>

    #define DHTPIN 5     // what pin we're connected to

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

    // Connect pin 1 (on the left) of the sensor to +5V
    // Connect pin 2 of the sensor to whatever your DHTPIN is
    // Connect pin 4 (on the right) of the sensor to GROUND
    // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

    DHT dht(DHTPIN, DHTTYPE);

    void setup() {
      Serial.begin(9600);
      Serial.println("DHTxx test!");
     
      dht.begin();
    }

    void loop() {
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      unsigned char sensorValue = analogRead(5);

      // check if returns are valid, if they are NaN (not a number) then something went wrong!
      if (isnan(t) || isnan(h)) {
        Serial.println("Failed to read from DHT");
      } else {
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print(" %\t");
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.println(" *C");
        Serial.print("RAW value : ");
        Serial.println(sensorValue);
      }
    }
    but the result still same although it can read RAW value from sensor :
    [​IMG]

    then I move the sensor from D5,D6 to I2C Grove Connector and changed the DHTPIN from 5 to 19 and it works!! :eek:
    [​IMG]

    Not sure how it become works because I have already tried to move the sensor to I2C connector but that time still didnt work. And I dont know why pin 5 (PD5) cant read sensor value but pin 19 (SCL) can although pin 5 and 19 both are DIO.
    Cant believe I have been struggling for 2 months only because of this mistake.. -_-'

    since my problem is solved then I will continue my project, I were already able to store RAW value and store it into MySQL and show the statistic, and now I only have to change the RAW value with temperature & humidity value.. :eek:
    many thanks to Vizier87 who has been assisting me till now!! sorry if this newbie have been being troublesome for you..
     
    Last edited: May 20, 2013
  16. edeca

    edeca Active Member

    Joined:
    Apr 8, 2008
    Messages:
    589
    Likes:
    38
    Location:
    England
  17. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Hi,

    Glad it worked out for you, although I'm not sure how much I contributed. But sometimes you just need to ask in a forum only to discover the solution after posting it! It happens to me perhaps in 50% of the cases when I posed my questions here as well.

    My advice is you should start learning more lower-level programming so that you can you can gain much more flexibility in your projects.

    Good luck!
     

Share This Page