This topic shows how to build a data logger using PIC18F4550 microcontroller. This data logger project stores the values of temperature and humidity every second in a text file where this file is located on SD card. As an addition to the temperature and humidity, this data logger puts date and time of the measured temperature and humidity in real time clock. The real time clock chip used in this project is the DS3231 and the sensor used to sense the temperature and humidity is the DHT22 sensor.
The result of the project should be as the one shown in the image below where the text file ‘Log.txt’ is shown:
Related Projects:
DataLogger with PIC18F4550, SD card and DHT11 sensor
Read and write files from and to SD card with PIC18F4550 – CCS C
Interfacing PIC18F4550 with DHT22 sensor – CCS C compiler
Real time clock & calendar with PIC18F4550 and DS3231 – CCS C
Hardware Required:
- PIC18F4550 microcontroller —> datasheet
- FAT32 formatted microSD card
- DS3231 board with 3V coin cell battery — DS3231 datasheet
- DHT22 relative humidity and temperature sensor — datasheet
- microSD card module
- USB-to-serial UART module (optional)
- 20×4 LCD screen
- 2 x pushbutton
- 8MHz crystal oscillator
- 2 x 22pF ceramic capacitors
- 4.7k ohm resistor
- 2 x 10k ohm resistor
- 10k ohm variable resistor or potentiometer
- Breadboard
- 5V source
- Jumper wires
PIC18F4550 data logger circuit:
Project circuit diagram is shown below.
(All grounded terminals are connected together)
With the microSD card module the connections are more simpler, the SD card module is supplied with 5V. This module has 6 pins which are (from left to right): GND, VCC, MISO, MOSI, SCK and CS (chip select).
In the circuit there are two push buttons: B1 and B2 connected to pins RA1 and RA2 respectively, these buttons are used to set the time and the date of the real time clock.
The DHT22 sensor has 4 pins: VCC, Data, NC (not connected) and GND. The data pin is connected to RA0 pin of the PIC18F4550.
The USB to serial UART module such as FT232RL is used to connect the microcontroller with the laptop (PC), in this example it shows the initialization status of the SD card and FAT system.
In this project the microcontroller runs with an external crystal oscillator of 8MHz and MCLR pin is configured as a digital input pin (in the software).
As a hint, instead of the microSD card module we can use AMS1117-3V3 to supply the SD card and three voltage dividers for SS, SCK and MOSI lines; each voltage divider consists of two resistors: 2.2k ohm and 3.3k ohm. Related project link above shows the circuit diagram.
PIC18F4550 data logger C code:
The C code below is for CCS C compiler, it was tested with compiler version 5.051. The compilation of this C code may give some warnings, they can be ignored!
Before compiling the code we’ve to add MMC/SD card driver and FAT library for CCS C compiler, their download links are in the page below. The name of the two files (with the extension) respectively are: mmcsd_m.c and fat_m.c. After downloading just add the two files to the project folder or to CCS C drivers folder:
SD Card driver and FAT Library for CCS C compiler
Or you can download them from the the links below:
mmcsd_m.c download
fat_m.c download
With the 8MHz and PLL2 the microcontroller becomes working at 48MHZ (12 MIPS) which is the highest speed of the PIC18F4550.
In this project I used software SPI for the communication between the microcontroller and the SD card, and I used hardware I2C for the communication between the microcontroller and the DS3231.
I used Timer1 module to measure pulse widths, with a prescaler of 8, Timer1 increments by 1.5 every 1us (15 every 10us).
Functions related with the DHT22 sensor:
void Start_Signal(void): sends the start signal to the DHT11 sensor.
int1 Check_Response(void): reads the response signal that comes from the DHT11 sensor, returns 1 if OK and zero if error.
int1 Read_Data(int8 *dht_data): reads humidity and temperature data from the DHT11 sensor and save data to pointer ‘dht_data‘, returns 0 if OK and 1 if error.
Functions related with the DS3231 RTC:
void DS3231_display(): when called, this function displays the time and date on the 20×4 LCD screen.
void blink_parameter(): this function is responsible of making a parameter (hours, minutes, date, month or year) blinks while editing.
int8 set(x, y, parameter): with this function the setting of time and date is more easier where x and y are the position of the parameter on the LCD.
FAT library functions:
fat_init(): initializes the FAT system and the media card, returns 0 if every thing went OK and non-zero if there was an error.
mk_file: creates a new file on the SD card, returns 0 if OK, non-zero if error.
fatopen: opens a file, returns 0 if OK, non-zero if error.
fatputs: puts a string into the opened file, returns 0 if OK, non-zero if error.
fatclose: closes the opened file, after writing to the file we must call this function, returns 0 if OK, non-zero if there was an error.
Full CCS 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | // Temperature and humidity data logger using PIC18F4550, SD card, DS3231 RTCC and DHT22 sensor // C code for CCS C compiler // http://simple-circuit.com/ // LCD module connections #define LCD_RS_PIN PIN_D0 #define LCD_RW_PIN PIN_D1 #define LCD_ENABLE_PIN PIN_D2 #define LCD_DATA4 PIN_D3 #define LCD_DATA5 PIN_D4 #define LCD_DATA6 PIN_D5 #define LCD_DATA7 PIN_D6 // End LCD module connections // SD card module connections #define MMCSD_PIN_SELECT PIN_B2 #define MMCSD_PIN_SCK PIN_B3 #define MMCSD_PIN_MOSI PIN_B4 #define MMCSD_PIN_MISO PIN_B5 // End SD card module connections // DHT22 sensor connection #define DHT22_PIN PIN_A0 // DHT22 Data pin is connected to RA0 // End DHT22 sensor connection #define button1 PIN_A1 // Button B1 is connected to RA1 pin #define button2 PIN_A2 // Button B2 is connected to RA2 pin #include <18F4550.h> #device PASS_STRINGS = IN_RAM #fuses NOMCLR HSPLL PLL2 CPUDIV1 #use delay(clock = 48MHz) #use fast_io(B) #use fast_io(D) #use I2C(MASTER, I2C1, FAST = 100000) // Initialize I2C #use rs232 (baud=9600, xmit=PIN_C0, rcv=PIN_C1) // Initialize UART protocol (needed for FAT library) #include <lcd.c> // Include LCD driver source file #include <mmcsd_m.c> // Include MMC/SD card driver source file #include <fat_m.c> // Include FAT library source file // Temperature and humidity variables char Temperature[] = " 00.0 C"; char Humidity[] = " 00.0 %"; int8 T_Byte1, T_Byte2, RH_Byte1, RH_Byte2, CheckSum, previous_second ; int16 Temp, RH; // Time and calendar variables char Time[] = " : : "; char Calendar[] = " / /20 "; int8 i, second, minute, hour, date, month, year; // FAT file system variable int8 fat_status; FILE LogFile; //////////// DHT22 functions //////////// void Start_Signal(void){ output_drive(DHT22_PIN); // Configure connection pin as output output_low(DHT22_PIN); // Connection pin output low delay_ms(25); // Wait 25 ms output_high(DHT22_PIN); // Connection pin output high delay_us(30); // Wait 30 us output_float(DHT22_PIN); // Configure connection pin as input } int1 Check_Response(void){ set_timer1(0); // Set Timer1 value to 0 while(!input(DHT22_PIN) && get_timer1() < 150); // Wait until DHT22_PIN becomes high (cheking of 80µs low time response) if(get_timer1() > 149) // If response time > 99µS ==> Response error return 0; // Return 0 (Device has a problem with response) else { set_timer1(0); // Set Timer1 value to 0 while(input(DHT22_PIN) && get_timer1() < 150); // Wait until DHT22_PIN becomes low (cheking of 80µs high time response) if(get_timer1() > 149) // If response time > 99µS ==> Response error return 0; // Return 0 (Device has a problem with response) else return 1; // Return 1 (response OK) } } int1 Read_Data(int8 *dht_data) { int8 j; *dht_data = 0; for(j = 0; j < 8; j++){ set_timer1(0); // Reset Timer1 while(!input(DHT22_PIN)) // Wait until DHT22_PIN becomes high if(get_timer1() > 150) { // If low time > 100µs ==> Time out error (Normally it takes 50µs) return 1; } set_timer1(0); // Reset Timer1 while(input(DHT22_PIN)) // Wait until DHT22_PIN becomes low if(get_timer1() > 150) { // If high time > 100µs ==> Time out error (Normally it takes 26-28µs for 0 and 70µs for 1) return 1; // Return 1 (timeout error) } if(get_timer1() > 70) // If high time > ~46µs ==> Sensor sent 1 bit_set(*dht_data, (7 - j)); // Set bit (7 - j) } return 0; // Return 0 (data read OK) } //////////// DS3231 functions //////////// void DS3231_display(){ // Convert BCD to decimal second = (second >> 4) * 10 + (second & 0x0F); minute = (minute >> 4) * 10 + (minute & 0x0F); hour = (hour >> 4) * 10 + (hour & 0x0F); date = (date >> 4) * 10 + (date & 0x0F); month = (month >> 4) * 10 + (month & 0x0F); year = (year >> 4) * 10 + (year & 0x0F); // End conversion Time[7] = second % 10 + 48; Time[6] = second / 10 + 48; Time[4] = minute % 10 + 48; Time[3] = minute / 10 + 48; Time[1] = hour % 10 + 48; Time[0] = hour / 10 + 48; Calendar[9] = year % 10 + 48; Calendar[8] = year / 10 + 48; Calendar[4] = month % 10 + 48; Calendar[3] = month / 10 + 48; Calendar[1] = date % 10 + 48; Calendar[0] = date / 10 + 48; lcd_gotoxy(6, 1); printf(lcd_putc, Time); // Display time lcd_gotoxy(6, 2); printf(lcd_putc, Calendar); // Display calendar } void blink_parameter(){ int8 j = 0; while(j < 10 && input(button1) && input(button2)){ j++; delay_ms(25); } } int8 set(x, y, parameter){ while(!input(button1)); // Wait until button (pin #8) released while(TRUE){ while(!input(button2)){ // If button (pin #9) is pressed parameter++; if(i == 0 && parameter > 23) // If hours > 23 ==> hours = 0 parameter = 0; if(i == 1 && parameter > 59) // If minutes > 59 ==> minutes = 0 parameter = 0; if(i == 2 && parameter > 31) // If date > 31 ==> date = 1 parameter = 1; if(i == 3 && parameter > 12) // If month > 12 ==> month = 1 parameter = 1; if(i == 4 && parameter > 99) // If year > 99 ==> year = 0 parameter = 0; lcd_gotoxy(x, y); printf(lcd_putc,"%02u", parameter); // Display parameter delay_ms(200); // Wait 200ms } lcd_gotoxy(x, y); lcd_putc(" "); blink_parameter(); lcd_gotoxy(x, y); // Display two spaces printf(lcd_putc,"%02u", parameter); // Display parameter blink_parameter(); if(!input(button1)){ // If button (pin #8) is pressed i++; // Increament 'i' for the next parameter return parameter; // Return parameter value and exit } } } void main(void) { lcd_init(); // Initialize LCD module lcd_putc('\f'); // clear LCD command lcd_gotoxy(1, 1); lcd_putc("TIME:"); lcd_gotoxy(1, 2); lcd_putc("DATE:"); delay_ms(1000); // wait 2 seconds setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); // Start Timer1 with internal clock source + 8 prescaler // Initializing the FAT library as well as the SD card ---> returns 0 if OK printf("Initializing FAT library ... "); fat_status = fat_init(); if( fat_status == 0 ) { // initialization OK printf("OK"); mk_file("/Log.txt"); // Create a new file with name 'Log.txt' fatopen("/Log.txt", "w", &LogFile); // Open 'Log.txt' with write permission 'w' fatputs("Temperature and humidity data logger with real time clock using PIC18F4550 microcontroller, DS3231 and DHT22 sensor\r\n\r\n" , &LogFile); fatputs("\r\n DATE | TIME | TEMPERATURE | HUMIDITY\r\n", &LogFile); fatclose(&LogFile); } else // FAT initialization error printf("Error!"); while(TRUE) { if(!input(button1)){ // If button1 is pressed i = 0; hour = set(6, 1, hour); minute = set(9, 1, minute); date = set(6, 2, date); month = set(9, 2, month); year = set(14, 2, year); // Convert decimal to BCD minute = ((minute / 10) << 4) + (minute % 10); hour = ((hour / 10) << 4) + (hour % 10); date = ((date / 10) << 4) + (date % 10); month = ((month / 10) << 4) + (month % 10); year = ((year / 10) << 4) + (year % 10); // End conversion // Write data to DS3231 RTC i2c_start(); // Start I2C i2c_write(0xD0); // DS3231 address i2c_write(0); i2c_write(0); // Reset sesonds and start oscillator i2c_write(minute); // Write minute value to DS3231 i2c_write(hour); // Write hour value to DS3231 i2c_write(1); // Write day value (not used) i2c_write(date); // Write date value to DS3231 i2c_write(month); // Write month value to DS3231 i2c_write(year); // Write year value to DS3231 i2c_stop(); // Stop I2C delay_ms(200); // Wait 200ms } // end if(!input(button1)) // Read data from the DS3231 i2c_start(); // Start I2C i2c_write(0xD0); // DS3231 address i2c_write(0); // Send register address i2c_start(); // Restart I2C i2c_write(0xD1); // Initialize data read second = i2c_read(1); // Read seconds from register 0 minute = i2c_read(1); // Read minuts from register 1 hour = i2c_read(1); // Read hour from register 2 i2c_read(1); // Read day from register 3 (not used) date = i2c_read(1); // Read date from register 4 month = i2c_read(1); // Read month from register 5 year = i2c_read(0); // Read year from register 6 i2c_stop(); // Stop I2C DS3231_display(); // Diaplay time & calendar on the LCD screen if( second != previous_second ) { previous_second = second; // Read humidity and temperature from DHT22 sensor Start_Signal(); // Send start signal to the sensor if(Check_Response()) { // Check if there is a response from sensor (If OK start reding humidity and temperature data) // Response OK ==> 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)) { lcd_gotoxy(21, 1); // Go to column 1 row 3 lcd_putc(" Time Out! "); lcd_gotoxy(21, 2); // Go to column 1 row 4 lcd_putc(" "); // Clear 4th row } // end if(Read_Data(&RH_Byte1) || Read_Data(&RH_Byte2) || Read_Data(&T_Byte1) || Read_Data(&T_Byte2) || Read_Data(&CheckSum)) 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 RH = RH_Byte1; RH = (RH << 8) | RH_Byte2; Temp = T_Byte1; Temp = (Temp << 8) | T_Byte2; if( RH >= 1000) Humidity[0] = '1'; else Humidity[0] = ' '; if(Temp > 0x8000) { Temperature[0] = '-'; Temp = Temp & 0x7FFF; } // end if(Temp > 0x8000) else Temperature[0] = ' '; Temperature[1] = (Temp / 100) % 10 + 48; Temperature[2] = (Temp / 10) % 10 + 48; Temperature[4] = Temp % 10 + 48; Temperature[5] = 223; // Put degree symbol (°) for the LCD Humidity[1] = (RH / 100) % 10 + 48; Humidity[2] = (RH / 10) % 10 + 48; Humidity[4] = RH % 10 + 48; lcd_gotoxy(21, 1); lcd_putc("Temperature ="); lcd_gotoxy(21, 2); lcd_putc("Humidity ="); lcd_gotoxy(34, 1); // Go to column 1 row 1 printf(lcd_putc, Temperature); // Display message1 lcd_gotoxy(34, 2); // Go to column 1 row 2 printf(lcd_putc, Humidity); // Display message2 if( fat_status == 0 ) { Temperature[5] = '°'; // Put degree symbol (°) for 'Log.txt' file fatopen("/Log.txt", "a", &LogFile); // Open 'Log.txt' with append permission 'a' fatputs(Calendar, &LogFile); fatputs(" | ", &LogFile); fatputs(Time, &LogFile); fatputs(" | ", &LogFile); fatputs(Temperature, &LogFile); fatputs(" | ", &LogFile); fatputs(Humidity, &LogFile); fatputs("\r\n", &LogFile); fatclose(&LogFile); } // end if( fat_status == 0 ) } // end if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)) else { lcd_gotoxy(21, 1); // Go to column 1 row 3 lcd_putc(" Checksum Error! "); lcd_gotoxy(21, 1); // Go to column 1 row 4 lcd_putc(" "); // Clear 4th row } // end else } // end else } // end if(Check_Response()) else { // If there is no response from the sensor lcd_gotoxy(21, 1); // Go to column 3 row 3 lcd_putc(" No response "); lcd_gotoxy(21, 2); // Go to column 1 row 4 lcd_putc(" from the sensor "); } // end else } // end if( second != previous_second ) delay_ms(50); } // end while(TRUE) } // end void main() // End of code |
The video below shows a simulation of the PIC18F4550 microcontroller time, date, temperature and humidity datalogger using Proteus ISIS, I got the same result as the real hardware circuit. Note that the simulation circuit is not the same as the hardware circuit, the hardware circuit diagram is shown above.
Downloads:
Proteus simulation file download:
Download
SD card image file download:
Download
Hello friend, thank you very much for sharing your knowledge …. I want to implement your project to capture temperature and humidity data in a chicken egg incubation project, this data logger is adjusted to save the temperature and humidity of the hen in the entire incubation process ….. and so I can have the parameters to adjust my incubator … I have three questions:
1: what is the page to download the new FAT library that supports SD cards with MBR
2: how much is the maximum capacity in Gb of memory that this library supports? (2,4,8,16,32,64) Gb
3: if for example, if I have a 4Gb sd card with capacity. How much data can I store of temperature and humidity knowing that my measurements are with 1 decimal. example 37.5 ° C every one second
thanks friend .. I hope you can help me
I am using google translator because I only speak spanish.
thanks
if it is of any use to you
this is my e-mail
[email protected]
thanks
Thank you friend, I compile everything in Ccs C 5,091, greetings from my beloved Venezuela. Sorry for my English, I just translated it from google.
i was too much interested for this detail explanation proteaus and micro c codes but i was be weak microc can share source code hex file!!
so great and thanks for share all detail, i have useful for your modified code.