Introduction

  • In this article, I will explain about checking the temperature and humidity using Arduino Mega 2560.
  • It can accurately display the temperature in the serial monitor.
Parts Of Lists
  • Arduino Mega 2560
  • Temperature sensor
  • Hookup Wires
  • Bread Board
Temperature Sensor
  • Temperature sensor is used to find the temperature of the place.
  • It also posses the self low heating and does not cross the 0.1-degree Celsius temperature rise till the air.
Figure 1: Temperature Sensor
Connection
Step 1: Connection From Temperature Sensor To ArduinoMega2560.
  • Connect the 5v of the supply to the 5v of the Arduino mega board.
  • Connect the Analog pin of the sensor to the A0 of the Analog of the board.
  • Connect the ground pin to the board of the Gnd
Programming
  1. void getCurrentTemp(int * sign, int * whole, int * fract);
  2. char temp_string[10];
  3. void setup()
  4. {
  5. Serial.begin(9600);
  6. // initialize DS18B20 datapin
  7. digitalWrite(REF_PIN, LOW);
  8. pinMode(REF_PIN, INPUT); // sets the digital pin as input (logic 1)
  9. pinMode(A0, INPUT);
  10. }
  11. void loop()
  12. {
  13. getCurrentTemp(temp_string);
  14. Serial.println(temp_string);
  15. delay(1000);
  16. }
  17. void OneWireReset(int Pin) // reset. Should improve to act as a presence pulse
  18. {
  19. digitalWrite(Pin, LOW);
  20. pinMode(Pin, OUTPUT); // bring low for 500 us
  21. delayMicroseconds(500);
  22. pinMode(Pin, INPUT);
  23. delayMicroseconds(500);
  24. }
  25. void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
  26. {
  27. byte n;
  28. for (n = 8; n != 0; n--)
  29. {
  30. if ((d & 0x01) == 1) // test least sig bit
  31. {
  32. digitalWrite(Pin, LOW);
  33. pinMode(Pin, OUTPUT);
  34. delayMicroseconds(5);
  35. pinMode(Pin, INPUT);
  36. delayMicroseconds(60);
  37. }
  38. else
  39. {
  40. digitalWrite(Pin, LOW);
  41. pinMode(Pin, OUTPUT);
  42. delayMicroseconds(60);
  43. pinMode(Pin, INPUT);
  44. }
  45. d = d >> 1; // now the next bit is in the least sig bit position.
  46. }
  47. }
  48. byte OneWireInByte(int Pin) // read byte, least sig byte first
  49. {
  50. byte d, n, b;
  51. for (n = 0; n < 8; n++)
  52. {
  53. digitalWrite(Pin, LOW);
  54. pinMode(Pin, OUTPUT);
  55. delayMicroseconds(5);
  56. pinMode(Pin, INPUT);
  57. delayMicroseconds(5);
  58. b = digitalRead(Pin);
  59. delayMicroseconds(50);
  60. d = (d >> 1) | (b << 7); // shift d to right and insert b in most sig bit position
  61. }
  62. return (d);
  63. }
  64. void getCurrentTemp(char * temp)
  65. {
  66. int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;
  67. OneWireReset(REF_PIN);
  68. OneWireOutByte(REF_PIN, 0xcc);
  69. OneWireOutByte(REF_PIN, 0x44); // perform temperature conversion, strong pullup for one sec
  70. OneWireReset(REF_PIN);
  71. OneWireOutByte(REF_PIN, 0xcc);
  72. OneWireOutByte(REF_PIN, 0xbe);
  73. LowByte = OneWireInByte(REF_PIN);
  74. HighByte = OneWireInByte(REF_PIN);
  75. TReading = (HighByte << 8) + LowByte;
  76. sign = TReading & 0x8000; // test most sig bit
  77. if (sign) // negative
  78. {
  79. TReading = (TReading ^ 0xffff) + 1; // 2's comp
  80. }
  81. Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
  82. whole = Tc_100 / 100; // separate off the whole and fractional portions
  83. fract = Tc_100 % 100;
  84. if (sign)
  85. {
  86. temp[0] = '-';
  87. }
  88. else
  89. {
  90. temp[0] = '+';
  91. }
  92. if (whole / 100 == 0)
  93. {
  94. temp[1] = ' ';
  95. }
  96. else
  97. {
  98. temp[1] = whole / 100 + '0';
  99. }
  100. temp[2] = (whole - (whole / 100) * 100) / 10 + '0';
  101. temp[3] = whole - (whole / 10) * 10 + '0';
  102. temp[4] = '.';
  103. temp[5] = fract / 10 + '0';
  104. temp[6] = fract - (fract / 10) * 10 + '0';
  105. temp[7] = '\0';
  106. }
Explanation
  • In this article I explained about the Checking temperature and humidity using Arduinomega2560.
  • It can accurately give the temperature of the place in the serial monitor.
Output
Figure 2: Output