Advertisement
Azuria

Arduino sketch for DS18B20 from

May 2nd, 2011
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #### Arduino sketch for DS18B20 from http://www.youtube.com/watch?v=jCXPdnfYhl0&feature=email&email=comment_reply_received
  2.  
  3. #include <OneWire.h>
  4.  
  5. //init the one wire interface on pin 10
  6. OneWire ow(4);
  7.  
  8. void setup(void) {
  9.   Serial.begin(9600);
  10.  
  11. }
  12.  
  13. void lookUpSensors(){
  14.   byte address[8];
  15.   int i=0;
  16.   byte ok = 0, tmp = 0;
  17.   //start the search
  18.   Serial.println("--Search started--");
  19.   while (ow.search(address)){
  20.     tmp = 0;
  21.     //0x10 = DS18S20
  22.     if (address[0] == 0x10){
  23.       Serial.print("Device is a DS18S20 : ");
  24.       tmp = 1;
  25.     } else {
  26.       //0x28 = DS18B20
  27.       if (address[0] == 0x28){
  28.         Serial.print("Device is a DS18B20 : 0x");
  29.         tmp = 1;
  30.       }
  31.     }
  32.     //display the address, if tmp is ok
  33.     if (tmp == 1){
  34.       if (OneWire::crc8(address, 7) != address[7]){
  35.         Serial.println("but it doesn't have a valid CRC!");
  36.       } else {
  37.         //all is ok, display it
  38.         for (i=0;i<8;i++){
  39.           if (address[i] < 9){
  40.             Serial.print("0");
  41.           }
  42.           Serial.print(address[i],HEX);
  43.           if (i<7){
  44.             Serial.print(", 0x");
  45.           }
  46.         }
  47.         Serial.println("");
  48.         ok = 1;
  49.       }
  50.     }//end if tmp
  51.   }//end while
  52.   if (ok == 0){
  53.     Serial.println("No devices were found");
  54.   }
  55.   Serial.println("--Search ended--");
  56. }
  57.  
  58. void loop(void) {
  59.   lookUpSensors();
  60.   delay(5000);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement