Advertisement
Guest User

Untitled

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