DataLogger with PIC18F4550, SD card and DHT11 sensor

This post shows how to make a temperature and humidity datalogger using PIC18F4550 microcontroller, SD card and DHT11 sensor where the temperature and humidity measurements are saved in a text file (located on the SD card). The compiler used in this project is CCS PIC C.

In this project the PIC18F4550 reads relative humidity and temperature data from the DHT11 sensor and after creating a text file on the SD card named “1Log.txt”, it starts writing that data to the text file.
A 16×2 LCD screen is connected to the microcontroller in order to display the relative humidity and temperature measurements.

To be able to build this project we need MMC/SD card driver and FAT library for CCS C compiler, their download links are in the topic below (official post of the two libraries), the names 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

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).

The temperature and humidity measurements are displayed on the 16×2 LCD screen and stored on the microSD card. The microSD card is placed in the module (adapter).

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.

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 SD card data logger C code:
The C code below is for CCS C compiler, it was tested with version 5.051.

The compilation of the project C code may give some warnings, just ignore them!

With the 8MHz and PLL2 the microcontroller becomes working at 48MHZ (12 MIPS) which is the highest speed of the PIC18F4550 and its hardware SPI module.

In this example I used software SPI just to show how the FAT library works with soft SPI mode. Hardware SPI is much faster the the soft one.

I used Timer1 module to measure pulse widths, with a prescaler of 8, Timer1 increments by 1.5 every 1us (15 every 10us).

Used functions: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.fat_init(): initializes the FAT system and the media card, returns 0 if 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 error.

I tested this project with Samsung 8GB (SDHC) and 2GB (SDSC) microSD cards, both are formatted with FAT32 file system, I got the file shown below:

The following video shows Proteus simulation of the project, I got the same result as the hardware circuit. Note that the simulation circuit is not the same as the hardware circuit, the hardware circuit diagram is shown above.

7 comments

Good work you have done. your tutorial is very detailed and easy to follow. Like Ismael, I tested your code with the same type of hardware you have used, it works well on Proteus but on hardware, it detects the FAT but when creating files or folders the pic is frozen, and you can never create files or folders on the SD card. any solution?

The FAT library has been updated to ‘support’ SD cards with MBR. Re-download it again and add it to your project!
You’re facing this error may be because your SD card has MBR, the updated library may work for you.

Thanks very much for your response, I downloaded the new FAT library that support SD card with MBR, and every thing worked well now. I am able to create directory and files. Please continue with the good work. see logged file below.

If you wan to use the MCLR pin you’ve to add it’s circuit and the easiest one is by connecting the MCLR pin to VCC (+5V) through 10k ohm resistor.
Currently there is only one link for the FAT library which supports SD cards with or without MBR.