DS18B20 + PWM Pins

Just got me two DS18B20's from Sparkfun and used the sample code on the OneWire wiki to test it. Why does this only work on PWM pins and not on the rest? I dont see any PWM code being used...

Edit: Well turns out it does work, but I kinda have to jump start it. If I upload the code, and monitor the serial, nothing shows up, if I pull the signal pin I start getting flooded with invalid CRC messages, then once I plug it back in, thats when I start getting real temp data back. Why do I have to do this, whats going on?

After further troubleshooting, I am finding that the program is stuck in the search loop. I put in some print statements to debug:

  if ( !ds.search(addr)) {
      //Serial.print("No more addresses.\n");
        delay(500);
      ds.reset_search();
        delay(500);
        Serial.print("Reset complete");
      return;
  }

I am getting lots of Reset complete messages, and nothing else in the serial viewer.

And while I got your attention, can anyone tell me what the numbers in the square brackets mean when declaring variables?

byte data[12];
byte addr[8];

That declares an array of bytes. The first line specifies that data is an array made of 12 bytes. You can access each individual byte by using the variable name followed by the index in brackets. I.e. data[0], data[1], etc. If you go higher than the declared index, your program blows up. (I assume that arduino uses 0 based indexes, but some languages start on 1)

I have no clue on the first question, I'm an arduino noob.

===========
byte data[12];
byte addr[8];

Thanks chris, I figured out array some time ago but I was busy working on other problems to update my post.

I decided to chuck the search function and I am now inputting my serial numbers manually, works every time. After some testing I found that the sample code was only CRC checking the serial number and not the result! So I copy pasted the CRC checker for the serial and made it work with the temp result. The only thing is, the CRC check is missing some bad results. First here is my hacked up code as it stands now:

#include <OneWire.h>

/* DS18S20 Temperature chip i/o
 
 */

OneWire  ds(2);  // on pin 2

void setup(void) {
  // initialize inputs/outputs
  // start serial port
  delay(2000);
  Serial.begin(9600);
  digitalWrite(13,HIGH);
  Serial.print("Going to loop()");
}



void loop(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8]= {0x28,0x21,0x71,0x8A,0x01,0x00,0x00,0x56};
  byte sensor2[8]= {0x28,0x0E,0x53,0x8A,0x01,0x00,0x00,0xBF};
  int Temp;
  /*
  if ( !ds.search(addr)) {
      //Serial.print("No more addresses.\n");
      ds.reset_search();
        //Serial.print("Reset complete");
      return;
  }
  
  digitalWrite(13,LOW);
  Serial.print("R=");
  for( i = 0; i < 8; i++) {
    Serial.print(addr[i], DEC);
    Serial.print(" ");
  }*/

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }
  /*
  if ( addr[0] != 0x28) {
      Serial.print("Device is not a DS18S20 family device.\n");
      return;
  }*/

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
  
  delay(750);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("P=");  
  Serial.print(present,HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print("  ");
  }
  
  if ( OneWire::crc8( data, 8) != data[8]) {
      Serial.print("CRC Bad!");
  }
  else {
        Serial.print("CRC A-OK!");
  }
  
  Temp=(data[1]<<8)+data[0];//take the two bytes from the response relating to temperature

  Temp=Temp>>4;//divide by 16 to get pure celcius readout

  //next line is Fahrenheit conversion
  Temp=Temp*1.8+32; // comment this line out to get celcius
  
  Serial.print("T=");//output the temperature to serial port
  Serial.print(Temp);
    Serial.print("  ");


  Serial.print(" CRC=");
  Serial.print( OneWire::crc8( data, 8), HEX);
  Serial.println();
}

And here is some output, It is definatly not 185 degreese in here...

Going to loop()P=1 91  1  4B  46  7F  FF  F  10  25  CRC A-OK!T=77   CRC=25
P=1 92  1  4B  46  7F  FF  E  10  24  CRC A-OK!T=77   CRC=24
P=1 93  1  4B  46  7F  FF  D  10  32  CRC A-OK!T=77   CRC=32
P=1 95  1  4B  46  7F  FF  B  10  B  CRC A-OK!T=77   CRC=B
P=1 98  1  4B  46  7F  FF  8  10  22  CRC A-OK!T=77   CRC=22
P=1 FF  FF  FF  FF  FF  FF  FF  FF  FF  CRC Bad!T=30   CRC=C9
P=1 FF  FF  FF  FF  FF  FF  FF  FF  FF  CRC Bad!T=30   CRC=C9
P=1 FF  FF  FF  FF  FF  FF  FF  FF  FF  CRC Bad!T=30   CRC=C9
P=1 FF  FF  FF  FF  FF  FF  FF  FF  FF  CRC Bad!T=30   CRC=C9
P=1 FF  FF  FF  FF  FF  FF  FF  FF  FF  CRC Bad!T=30   CRC=C9
P=1 FF  FF  FF  FF  FF  FF  FF  FF  FF  CRC Bad!T=30   CRC=C9
P=1 50  5  4B  46  7F  FF  C  10  1C  CRC A-OK!T=185   CRC=1C
P=1 A2  1  4B  46  7F  FF  E  10  D8  CRC A-OK!T=78   CRC=D8
P=1 A3  1  4B  46  7F  FF  D  10  CE  CRC A-OK!T=78   CRC=CE
P=1 A4  1  4B  46  7F  FF  C  10  DA  CRC A-OK!T=78   CRC=DA
P=1 A4  1  4B  46  7F  FF  C  10  DA  CRC A-OK!T=78   CRC=DA
P=1 A5  1  4B  46  7F  FF  B  10  F7  CRC A-OK!T=78   CRC=F7
P=1 A4  1  4B  46  7F  FF  C  10  DA  CRC A-OK!T=78   CRC=DA
P=1 A4  1  4B  46  7F  FF  C  10  DA  CRC A-OK!T=78   CRC=DA
P=1 A3  1  4B  46  7F  FF  D  10  CE  CRC A-OK!T=78   CRC=CE

I guess I will have to live with this false crc, its better than no crc I suppose. I made these errors by jamming my sensor in and out of the breadboard, not really a real life scenario....

It is definatly not 185 degreese in here...
I guess I will have to live with this false crc, its better than no crc I suppose.

Hi Nonyaz,

The 185 degrees Fahrenheit reading is the same as 85 degrees Celscius. The datasheet says: "The power-on reset value of the temperature register is +85°C."
That's why the CRC is valid, the temperature sensor is responding with correct data, it just lost power (because you pulled it out) and returns the default value.

Ah ha! Good observation Waffle, I can just write a if statement to retest if I ever get a 185 reading again, that way I'll be sure to get correct readings no matter whats happening to the sensor.

I assume that arduino uses 0 based indexes

FWIW you are correct. :slight_smile:

--Phil.

I decided to chuck the search function and I am now inputting my serial numbers manually, works every time.

I'm trying to get a temperature reading and it keeps outputting "No more addresses". How do you find the serial number to input it manually?

Humm I never got that error, make sure you got it wired right, pull up resistor on the center pin and the two outer ones connected to ground.

Is this correct?