DS18B20 with Arduino Micro

Discussion in 'Embedded Systems and Microcontrollers' started by james211, Sep 16, 2014.

  1. james211

    Thread Starter Member

    May 29, 2012
    210
    2
    Looking for a bit of help...I swear I've had this working before, but right now all I can get is the 185 reading. I have two DS18B20 sensors, one standard and one waterproof. With both I get the 185 reading. Any thoughts?

    Included code and pics of breadboard.

    Here is my code:
    Code (Text):
    1. <#include <OneWire.h>
    2.  
    3. int DS18S20_Pin = 4; //DS18S20 Signal pin on digital 2
    4. int relay = 7;
    5.  
    6. //Temperature chip i/o
    7. OneWire ds(DS18S20_Pin);  // on digital pin 2
    8.  
    9. void setup(void) {
    10.   Serial.begin(9600);
    11. }
    12.  
    13. void loop(void) {
    14.   float temperature = getTemp();
    15.   float tempF = (temperature * 9.0)/ 5.0 + 32.0;
    16.   Serial.println(tempF);
    17.   if (tempF >60) {
    18.   digitalWrite(7,HIGH);
    19.   } else {
    20.   digitalWrite(7,LOW);
    21.   }
    22.  
    23.   delay(100); //just here to slow down the output so it is easier to read
    24.  
    25. }
    26.  
    27.  
    28. float getTemp(){
    29.   //returns the temperature from one DS18S20 in DEG Celsius
    30.  
    31.   byte data[12];
    32.   byte addr[8];
    33.  
    34.   if ( !ds.search(addr)) {
    35.   //no more sensors on chain, reset search
    36.   ds.reset_search();
    37.   return -1000;
    38.   }
    39.  
    40.   if ( OneWire::crc8( addr, 7) != addr[7]) {
    41.   Serial.println("CRC is not valid!");
    42.   return -1000;
    43.   }
    44.  
    45.   if ( addr[0] != 0x10 && addr[0] != 0x28) {
    46.   Serial.print("Device is not recognized");
    47.   return -1000;
    48.   }
    49.  
    50.   ds.reset();
    51.   ds.select(addr);
    52.   ds.write(0x44, 1); // start conversion, with parasite power on at the end
    53.  
    54.   byte present = ds.reset();
    55.   ds.select(addr);  
    56.   ds.write(0xBE); // Read Scratchpad
    57.  
    58.  
    59.   for (int i = 0; i < 9; i++) { // we need 9 bytes
    60.   data[i] = ds.read();
    61.   }
    62.  
    63.   ds.reset_search();
    64.  
    65.   byte MSB = data[1];
    66.   byte LSB = data[0];
    67.  
    68.   float tempRead = ((MSB << 8) | LSB); //using two's compliment
    69.   float TemperatureSum = tempRead / 16;
    70.  
    71.   return TemperatureSum;
    72.  
    73. }
    74.  
     
  2. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,770
    970
  3. james211

    Thread Starter Member

    May 29, 2012
    210
    2
    I ended up getting it to work, not really sure where I went wrong to be honest. The hookup seems pretty standard, but for some reason not all the codes work from the examples.

    Quick question, since I'll be using this with a relay to turn a fan on and off, is there a way to put a delay in so that once the temp hits the off point, the relay will stay off for a minimum amount of time? I know the delay would have to be in seconds, I just don't know how or where in the code to put it.
     
  4. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    First you say you want delay.
    Then you say you want relay off for MINIMUM amount of time.

    You are not making sense.
     
  5. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    I read that as him wanting hysteresis, he wants to delay the fan coming back on for an amount of time. The simplest thing to do may be to just increase the delay(100) at the end of your loop function to delay (10000) or whatever - the value is in mS so 10000 would give you a 10 second delay between temperature measurements.

    However a better approach would be to not use a delay but use a temperature range to create a dead-band. So if temp is >60.5 turn on and if temp is <59.5 turn off.
     
Loading...