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
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
- void getCurrentTemp(int * sign, int * whole, int * fract);
- char temp_string[10];
- void setup()
- {
- Serial.begin(9600);
-
- digitalWrite(REF_PIN, LOW);
- pinMode(REF_PIN, INPUT);
- pinMode(A0, INPUT);
- }
- void loop()
- {
- getCurrentTemp(temp_string);
- Serial.println(temp_string);
- delay(1000);
- }
- void OneWireReset(int Pin)
- {
- digitalWrite(Pin, LOW);
- pinMode(Pin, OUTPUT);
- delayMicroseconds(500);
- pinMode(Pin, INPUT);
- delayMicroseconds(500);
- }
- void OneWireOutByte(int Pin, byte d)
- {
- byte n;
- for (n = 8; n != 0; n--)
- {
- if ((d & 0x01) == 1)
- {
- digitalWrite(Pin, LOW);
- pinMode(Pin, OUTPUT);
- delayMicroseconds(5);
- pinMode(Pin, INPUT);
- delayMicroseconds(60);
- }
- else
- {
- digitalWrite(Pin, LOW);
- pinMode(Pin, OUTPUT);
- delayMicroseconds(60);
- pinMode(Pin, INPUT);
- }
- d = d >> 1;
- }
- }
- byte OneWireInByte(int Pin)
- {
- byte d, n, b;
- for (n = 0; n < 8; n++)
- {
- digitalWrite(Pin, LOW);
- pinMode(Pin, OUTPUT);
- delayMicroseconds(5);
- pinMode(Pin, INPUT);
- delayMicroseconds(5);
- b = digitalRead(Pin);
- delayMicroseconds(50);
- d = (d >> 1) | (b << 7);
- }
- return (d);
- }
- void getCurrentTemp(char * temp)
- {
- int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;
- OneWireReset(REF_PIN);
- OneWireOutByte(REF_PIN, 0xcc);
- OneWireOutByte(REF_PIN, 0x44);
- OneWireReset(REF_PIN);
- OneWireOutByte(REF_PIN, 0xcc);
- OneWireOutByte(REF_PIN, 0xbe);
- LowByte = OneWireInByte(REF_PIN);
- HighByte = OneWireInByte(REF_PIN);
- TReading = (HighByte << 8) + LowByte;
- sign = TReading & 0x8000;
- if (sign)
- {
- TReading = (TReading ^ 0xffff) + 1;
- }
- Tc_100 = (6 * TReading) + TReading / 4;
- whole = Tc_100 / 100;
- fract = Tc_100 % 100;
- if (sign)
- {
- temp[0] = '-';
- }
- else
- {
- temp[0] = '+';
- }
- if (whole / 100 == 0)
- {
- temp[1] = ' ';
- }
- else
- {
- temp[1] = whole / 100 + '0';
- }
- temp[2] = (whole - (whole / 100) * 100) / 10 + '0';
- temp[3] = whole - (whole / 10) * 10 + '0';
- temp[4] = '.';
- temp[5] = fract / 10 + '0';
- temp[6] = fract - (fract / 10) * 10 + '0';
- temp[7] = '\0';
- }
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.
Figure 2: Output