Difference between pages "One-Wire Temperature Sensor - DS18B20" and "Temperature & Humidity Sensor Pro"

From Nearwiki
(Difference between pages)
Jump to: navigation, search
(Created page with " === Reference Information === <br /> * '''Info Web:''' http://seeedstudio.com/depot/one-wire-temperature-sensor-p-1235.html?cPath=25_27 * '''Techinical Spec:''' Temperature...")
 
(Created page with " * '''Product Web:''' http://seeedstudio.com/depot/grove-temperaturehumidity-sensor-pro-p-838.html?cPath=25_27 * '''Wiki:''' http://seeedstudio.com/wiki/Grove_-_Temperature_...")
 
Line 1: Line 1:
  
=== Reference Information ===
 
<br />
 
* '''Info Web:''' http://seeedstudio.com/depot/one-wire-temperature-sensor-p-1235.html?cPath=25_27
 
  
* '''Techinical Spec:''' Temperature range: -55°C to +125°C - Accuracy: ±0.5°C( -10°C to +85°C)
+
* '''Product Web:''' http://seeedstudio.com/depot/grove-temperaturehumidity-sensor-pro-p-838.html?cPath=25_27
 +
 
 +
* '''Wiki:''' http://seeedstudio.com/wiki/Grove_-_Temperature_and_Humidity_Sensor_Pro
 +
 
 +
* '''Code Source:''' http://seeedstudio.com/wiki/File:Humidity_Temperature_Sensor.zip
 +
 
 +
* '''Technical Spec:''' Humidytly: 5% RH - 99% RH - Temperature -40°C - 80°C - Acuracy: 2% RH / 0.5°C
  
 
* '''Connector Type:''' Base Shield V1.3 - Grove compatible - http://seeedstudio.com/depot/base-shield-v13-p-1378.html
 
* '''Connector Type:''' Base Shield V1.3 - Grove compatible - http://seeedstudio.com/depot/base-shield-v13-p-1378.html
 
* '''Code Source:''' http://playground.arduino.cc/Learning/OneWire
 
  
  
  
 
----
 
----
 +
===Temperature & Humidity Sensor Pro===
 +
<br />
  
===Temperature Sensor - DS18B20 - (Waterproof)===
+
[[File:Grove_Tem_Humid.jpg]]
<br />
 
[[File:Temp probe 18B20.jpg]]
 
  
  
Line 25: Line 26:
 
<br />
 
<br />
  
'''In order to run this code you should copy the following code into the area delimited by the MY_BIOS CUSTOM CODE banners in the Hello_World_xxx.ino example.'''
+
'''In order to run this code you should copy the following code into the area delimited by the MY_NBIOS CUSTOM CODE banners in the Hello_World_xxx.ino example.'''
 +
 
  
 
<pre>
 
<pre>
<nowiki>#include <OneWire.h></nowiki>
+
void Nearbus::MyNbios_0( byte portId, UINT setValue, ULONG* pRetValue, byte vmcuMethod, PRT_CNTRL_STRCT* pPortControlStruct )
 
+
{  
OneWire  ds(3);  // Arduino PIN_3
+
UINT data[6];
+
UINT _pin;
void Nearbus::MyNbios_0( byte portId, ULONG setValue, ULONG* pRetValue, byte vmcuMethod, PRT_CNTRL_STRCT* pPortControlStruct )
+
UINT _count = 6;
{
+
ULONG _lastreadtime = 0;
byte i;
+
boolean firstreading = true;
byte present = 0;
+
float h;
byte type_s;
+
float t;
byte data[12];
+
byte ret;
byte addr[8];
+
UINT laststate = HIGH;
float celsius, fahrenheit;
+
UINT counter = 0;
   
+
UINT j = 0;
    for (i=0 ; i < 20 ; i++) {
+
UINT  i;
        if ( ds.search(addr)) {
+
ULONG currenttime;
            break;
+
          
         }
+
         //************************************
         ds.reset_search();
+
         // Reconfiguring Ports as I/O
         delay(250);
+
        //************************************
    }
+
         if( pPortControlStruct->portMode != MYDRIVER_MODE )  
         if (i == 20 ) {
+
        {
             *pRetValue = 0; 
+
             PortModeConfig( portId, MYDRIVER_MODE );      
            return;
 
 
         }
 
         }
 +
       
 +
        //************************************
 +
        // Custom Function
 +
        //************************************
 +
          _pin = pPortControlStruct->pinId;
 +
       
 +
          // pull the pin high and wait 250 milliseconds
 +
          digitalWrite(_pin, HIGH);
 +
          delay(250);
  
    if(OneWire::crc8(addr, 7) != addr[7]) {
+
          currenttime = millis();
        *pRetValue = 0;
+
          if (currenttime < _lastreadtime) {
        return;
+
              // ie there was a rollover
    }
+
              _lastreadtime = 0;
 +
          }
 +
          if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
 +
              ret = 0; // return last correct measurement
 +
              //delay(2000 - (currenttime - _lastreadtime));
 +
          }
 +
          else {
 +
              firstreading = false;
 +
              _lastreadtime = millis();
 +
           
 +
              data[0] = data[1] = data[2] = data[3] = data[4] = 0;
 +
             
 +
              // now pull it low for ~20 milliseconds
 +
              pinMode(_pin, OUTPUT);
 +
              digitalWrite(_pin, LOW);
 +
              delay(20);
 +
              cli();
 +
              digitalWrite(_pin, HIGH);
 +
              delayMicroseconds(40);
 +
              pinMode(_pin, INPUT);
 +
           
 +
              // read in timings
 +
              for ( i=0; i< 85; i++) {  // #define MAXTIMINGS = 85 // how many timing transitions we need to keep track of. 2 * number bits + extra
 +
                  counter = 0;
 +
                  while (digitalRead(_pin) == laststate) {
 +
                      counter++;
 +
                      delayMicroseconds(1);
 +
                      if (counter == 255) {
 +
                          break;
 +
                      }
 +
                  }
 +
                  laststate = digitalRead(_pin);
 +
         
 +
                  if (counter == 255) break;
 +
         
 +
                  // ignore first 3 transitions
 +
                  if ((i >= 4) && (i%2 == 0)) {
 +
                      // shove each bit into the storage bytes
 +
                      data[j/8] <<= 1;
 +
                      if (counter > _count)
 +
                          data[j/8] |= 1;
 +
                      j++;
 +
                  }
 +
              }
 +
              sei();
 +
           
 +
              // check we read 40 bits and that the checksum matches
 +
              if ((j >= 40) &&
 +
                  (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
 +
                  ret = 0;
 +
              }
 +
              else{
 +
                  ret = 1;
 +
              }
 +
          }
 +
          if (ret == 0) {
 +
              h = data[0];
 +
              h *= 256;
 +
              h += data[1];
 +
              h *= 10;
  
    // the first ROM byte indicates which chip
+
              t = data[2] & 0x7F;
    switch (addr[0]) {
+
              t *= 256;
        case 0x10:
+
              t += data[3];
            type_s = 1;
+
              t *= 10;
            break;
+
              if (data[2] & 0x80)
+
              t *= -1;
        case 0x28:
+
          }
            type_s = 0;
+
          else {
            break;
+
              t = 0;
+
              h = 0;  
        case 0x22:
+
          }
            type_s = 0;
+
          // delay(250);  
            break;
+
               
+
        //************************************
        default:
+
        // Updating register
            *pRetValue = 0;
+
        //************************************       
            return;
+
        * pRetValue = (ULONG) t;              
    }  
+
}
    ds.reset();
 
    ds.select(addr);
 
    ds.write(0x44, 1);                                    // start conversion, with parasite power on at the end
 
    delay(1000);                                          // maybe 750ms is enough, maybe not
 
    present = ds.reset();
 
    ds.select(addr);   
 
    ds.write(0xBE);                                      // Read Scratchpad
 
  
    for ( i = 0; i < 9; i++) {                            // we need 9 bytes
 
        data[i] = ds.read();
 
    }
 
 
    int16_t raw = (data[1] << 8) | data[0];
 
    if (type_s) {
 
        raw = raw << 3;                                  // 9 bit resolution default
 
        if (data[7] == 0x10) {                            // "count remain" gives full 12 bit resolution
 
            raw = (raw & 0xFFF0) + 12 - data[6];
 
        }
 
    }
 
else {
 
        byte cfg = (data[4] & 0x60);
 
        // at lower res, the low bits are undefined, so let's zero them
 
        if (cfg == 0x00) raw = raw & ~7;                  // 9 bit resolution, 93.75 ms
 
        else if (cfg == 0x20) raw = raw & ~3;            // 10 bit res, 187.5 ms
 
        else if (cfg == 0x40) raw = raw & ~1;            // 11 bit res, 375 ms
 
// default is 12 bit resolution, 750 ms conversion time
 
    }
 
    celsius = (float)raw / 16.0;
 
    fahrenheit = celsius * 1.8 + 32.0;     
 
 
 
    if ( !setValue ) {                                    // NearAPI: [value=1] => Fahrenheit - [value=0] => Celsius
 
        *pRetValue = (ULONG) (celsius * 100);
 
    }
 
    else {
 
        *pRetValue = (ULONG) (fahrenheit * 100);
 
    }   
 
}
 
 
</pre>
 
</pre>

Latest revision as of 21:17, 29 March 2018


  • Technical Spec: Humidytly: 5% RH - 99% RH - Temperature -40°C - 80°C - Acuracy: 2% RH / 0.5°C



Temperature & Humidity Sensor Pro


Grove Tem Humid.jpg



MyNbios Code - Example


In order to run this code you should copy the following code into the area delimited by the MY_NBIOS CUSTOM CODE banners in the Hello_World_xxx.ino example.


void Nearbus::MyNbios_0( byte portId, UINT setValue, ULONG* pRetValue, byte vmcuMethod, PRT_CNTRL_STRCT* pPortControlStruct  )
{ 
UINT data[6];
UINT _pin;
UINT _count = 6;
ULONG _lastreadtime = 0;
boolean firstreading = true;
float h;
float t;  
byte ret;
UINT laststate = HIGH;
UINT counter = 0;
UINT j = 0;
UINT  i;
ULONG currenttime;
        
        //************************************
        // Reconfiguring Ports as I/O
        //************************************
        if( pPortControlStruct->portMode != MYDRIVER_MODE ) 
        {
            PortModeConfig( portId, MYDRIVER_MODE );        
        }
        
        //************************************
        // Custom Function
        //************************************
          _pin = pPortControlStruct->pinId;
         
          // pull the pin high and wait 250 milliseconds
          digitalWrite(_pin, HIGH);
          delay(250);

          currenttime = millis();
          if (currenttime < _lastreadtime) {
              // ie there was a rollover
              _lastreadtime = 0;
          }
          if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
              ret = 0; // return last correct measurement
              //delay(2000 - (currenttime - _lastreadtime));
          }
          else {
              firstreading = false;
              _lastreadtime = millis();
            
              data[0] = data[1] = data[2] = data[3] = data[4] = 0;
              
              // now pull it low for ~20 milliseconds
              pinMode(_pin, OUTPUT);
              digitalWrite(_pin, LOW);
              delay(20);
              cli();
              digitalWrite(_pin, HIGH);
              delayMicroseconds(40);
              pinMode(_pin, INPUT);
            
              // read in timings
              for ( i=0; i< 85; i++) {   // #define MAXTIMINGS = 85 // how many timing transitions we need to keep track of. 2 * number bits + extra
                  counter = 0;
                  while (digitalRead(_pin) == laststate) {
                      counter++;
                      delayMicroseconds(1);
                      if (counter == 255) {
                          break;
                      }
                  }
                  laststate = digitalRead(_pin);
          
                  if (counter == 255) break;
          
                  // ignore first 3 transitions
                  if ((i >= 4) && (i%2 == 0)) {
                      // shove each bit into the storage bytes
                      data[j/8] <<= 1;
                      if (counter > _count)
                          data[j/8] |= 1;
                      j++;
                  }
              }
              sei();
            
              // check we read 40 bits and that the checksum matches
              if ((j >= 40) && 
                  (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
                  ret = 0;
              }
              else{
                  ret = 1;
              }
          }
          if (ret == 0) {
              h = data[0];
              h *= 256;
              h += data[1];
              h *= 10;

              t = data[2] & 0x7F;
              t *= 256;
              t += data[3];
              t *= 10;
              if (data[2] & 0x80)
              t *= -1;
          }
          else {
              t = 0;
              h = 0;    
          }  
          // delay(250); 
                
        //************************************
        // Updating register 
        //************************************         
        * pRetValue = (ULONG) t;                
}