Advertisement
ossipee

Working_One_Wire

Jan 25th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <OneWire.h>
  2.  
  3. int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
  4.  
  5. //Temperature chip i/o
  6. OneWire ds(DS18S20_Pin); // on digital pin 2
  7.  
  8. void setup(void) {
  9. Serial.begin(9600);
  10. }
  11.  
  12. void loop(void) {
  13. float temperature = getTemp();
  14. Serial.println(temperature);
  15.  
  16. delay(100); //just here to slow down the output so it is easier to read
  17.  
  18. }
  19.  
  20.  
  21. float getTemp(){
  22. //returns the temperature from one DS18S20 in DEG Celsius
  23.  
  24. byte data[12];
  25. byte addr[8];
  26.  
  27. if ( !ds.search(addr)) {
  28. //no more sensors on chain, reset search
  29. ds.reset_search();
  30. return -1000;
  31. }
  32.  
  33. if ( OneWire::crc8( addr, 7) != addr[7]) {
  34. Serial.println("CRC is not valid!");
  35. return -1000;
  36. }
  37.  
  38. if ( addr[0] != 0x10 && addr[0] != 0x28) {
  39. Serial.print("Device is not recognized");
  40. return -1000;
  41. }
  42.  
  43. ds.reset();
  44. ds.select(addr);
  45. ds.write(0x44,1); // start conversion, with parasite power on at the end
  46.  
  47. byte present = ds.reset();
  48. ds.select(addr);
  49. ds.write(0xBE); // Read Scratchpad
  50.  
  51.  
  52. for (int i = 0; i < 9; i++) { // we need 9 bytes
  53. data[i] = ds.read();
  54. }
  55.  
  56. ds.reset_search();
  57.  
  58. byte MSB = data[1];
  59. byte LSB = data[0];
  60.  
  61. float tempRead = ((MSB << 8) | LSB); //using two's compliment
  62. float TemperatureSum = tempRead / 16;
  63.  
  64. return TemperatureSum;
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement