Building a temperature and relative humidity data logger using PIC16F877A, DHT11 and Microsoft Excel is very easy where data are sent serially from the PIC16F877A microcontroller to the Excel sheet.
Hardware Required:
The Circuit:
Project circuit schematic is shown below.
(All grounded terminals are connected together)
As shown above, the circuit is very simple we need the DHT11 sensor only and there is no need for a real time clock chip (DS3231, DS1302, DS1307 …) because Microsoft Excel can get data and time information from Windows.
The DHT11 sensor has 4 pins (from left to right): VCC (5V), data, NC (not connected pin) and GND. Data pin is connected to pin RD4.
The MAX232 integrated circuit is used to interface the microcontroller with the PC.
CCS C code:
Functions used in the code:
void Start_Signal(): used to send start signal to the DHT11 sensor.
int1 Check_Response(): used to detect the response signal which comes from the DHT11 sensor, this function returns 1 (TRUE) if OK and 0 (FALSE) if error.
int1 Read_Data(int8* dht_data): this function reads 1 byte (8 bits) from the sensor, data are saved in the variable dht_data. Returns 0 (FALSE) if OK and 1 (TRUE) if error (time out error).
In order to send data serially from the Arduino board to Excel we need a small software named PLX-DAQ(Parallax Data Acquisition tool). This software is just an add-in for Microsoft Excel. PLX-DAQ download link can be found in the page below:
https://www.parallax.com/downloads/plx-daq
After downloading and installing you will see a new folder created in your PC desktop named: PLX-DAQwhich contains two shortcut files: PLX-DAQ Help File and PLX-DAQ Spreadsheet. Double click (or open using Excel) on the second file (PLX-DAQ Spreadsheet) gives a window as the one shown in the picture below. If there is a security warning (macros have been disabled) just click on Options –> check: Enable this content –> OK . Note that this plugin was tested with Microsoft Office 2007 and it should work with Office 2010, but unfortunately it doesn’t work with Office 2013.
To start receiving data just choose the COM port and baud rate then click on Connect.
C code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
// Excel data logger using PIC16F877A and DHT11 relative humidity and temperature sensor
// C Code for CCS C compiler.
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8MHz)
#use rs232(UART1, baud = 9600)
#use fast_io(D)
#define DHT11_PIN PIN_D4 // DHT11 Data pin is connected to RD4
int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
void Start_Signal() {
output_drive(DHT11_PIN); // Configure connection pin as output
output_low(DHT11_PIN); // Connection pin output low
delay_ms(25);
output_high(DHT11_PIN); // Connection pin output high
delay_us(25);
output_float(DHT11_PIN); // Configure connection pin as input
}
int1 Check_Response() {
set_timer1(0); // Set Timer1 value to 0
setup_timer_1(T1_INTERNAL | T1_DIV_BY_2); // Start Timer1 with internal clock source + 2 prescaler
while(!input(DHT11_PIN) && get_timer1() < 100); // Wait until DHT11_PIN becomes high (checking of 80µs low time response)
if(get_timer1() > 99) // If response time > 99µS ==> Response error
return FALSE; // Return FALSE (Device has a problem with response)
else {
set_timer1(0); // Set Timer1 value to 0
while(input(DHT11_PIN) && get_timer1() < 100); // Wait until DHT11_PIN becomes low (cheking of 80µs high time response)
if(get_timer1() > 99) // If response time > 99µS ==> Response error
return FALSE; // Return FALSE (Device has a problem with response)
else
return TRUE; // Return TRUE (response OK)
}
}
int1 Read_Data(int8* dht_data) {
int8 i;
*dht_data = 0;
for(i = 0; i < 8; i++){
set_timer1(0); // Set Timer1 value to 0
while(!input(DHT11_PIN)) // Wait until DHT11_PIN becomes high
if(get_timer1() > 100) { // If low time > 100 ==> Time out error (Normally it takes 50µs)
return TRUE;
}
set_timer1(0); // Set Timer1 value to 0
while(input(DHT11_PIN)) // Wait until DHT11_PIN becomes low
if(get_timer1() > 100) { // If high time > 100 ==> Time out error (Normally it takes 26-28µs for 0 and 70µs for 1)
return TRUE;
}
if(get_timer1() > 50) // If high time > 50 ==> Sensor sent 1
*dht_data |= (1 << (7 – i)); // Set bit (7 – i)
}
return FALSE;
}
void main() {
printf(« CLEARDATA\r\n »); // Clear all Excel sheet data
// Label columns: A for date, B for time, C for temperature and D for humidity
printf(« LABEL,Date,Time,Temperature,Humidity\r\n »);
while(TRUE) {
printf(« DATA,DATE,TIME, »); // Write date and time on row A and row B respectively
Start_Signal(); // Send start signal to the sensor
if(Check_Response()) { // Check if there is a response from sensor (If OK start reading humidity and temperature data)
// Read (and save) data from the DHT11 sensor and check time out errors
if(Read_Data(&RH_byte1) || Read_Data(&RH_byte2) || Read_Data(&T_byte1) || Read_Data(&T_byte2) || Read_Data(&Checksum))
printf(« Time Out!,Time Out!,\r\n »);
else { // If there is no time out error
if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)) {
// If there is no checksum error
printf(« %02u », T_byte1); // Send temperature value to Excel
printf(« , »); // Move to next column
printf(« %02u », RH_byte1); // Send humidity value to Excel
printf(« ,\r\n »); // Move to next column and start new row
}
// If there is a checksum error
else
printf(« Checksum Error!,Checksum Error!,\r\n »);
}
}
// If there is a response (from the sensor) problem
else
printf(« No Response!,No Response!,\r\n »);
setup_timer_1(T1_DISABLED); // Disable Timer1 module
delay_ms(1000); // Wait 1 second
}
}
// End of code.
|
The result:
If you have a problem with time column (column B) just select that column –> right mouse click –> Format Cells (a new window will open) –> select Time (from a list on the left) –> OK .