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

Related Projects:
Interfacing PIC18F4550 with DHT11 humidity and temperature sensor
Read and write files from and to SD card with PIC18F4550 – CCS C

Hardware Required:

  • PIC18F4550 microcontroller
  • FAT32 formatted SD card (MMC, SD, SDHC, micro SD ….)
  • DHT11 relative humidity and temperature sensor
  • microSD card module (adapter)
  • USB-to-serial UART module (optional)
  • 16×2 LCD screen
  • 8MHz crystal oscillator
  • 2 x 22pF ceramic capacitors
  • 4.7k ohm resistor
  • 10k ohm variable resistor or potentiometer
  • Breadboard
  • 5V source
  • Jumper wires

PIC18F4550 datalogger with SD card and DHT11 sensor hardware circuit

PIC18F4550 SD card data logger circuit:
PIC18F4550 datalogger circuit diagram is shown below.

PIC18F4550 datalogger circuit with SD card and DHT11 sensor

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.

As written above, to be able to compile the C code below, we need MMC/SD driver and FAT library (mmcsd_m.c and fat_m.c), their download links are in this page:
SD Card driver and FAT Library for CCS C compiler

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.

Full CCS C code:

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.

Proteus simulation file download:
Download
SD card image file download:
Download

7 thoughts on “DataLogger with PIC18F4550, SD card and DHT11 sensor”

  1. #fuses NOMCLR HSPLL PLL2 CPUDIV1—————working fine
    //#fuses MCLR, HSPLL, PLL2, CPUDIV1 ——- MCLR NOT WORKING PLEASE TELL ME
    #use delay(clock=48000000,crystal=8000000) —— working fine

    I WANT MCLR PIN TO WORK AS MCLR PIN
    IS FILE WITH MBR SUPPORT HAVE DIFFERENT DOWNLOAD LINK OR WE HAVE DOWNLOADED CORRECT FILE HOW TO CHECK IT

    SIMULATION WORKING FINE BUT I WANT MCLR PIN TO WORK WITH THE SAME CODE

    THANK YOU FOR YOUR SUPPORT

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

  2. 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?

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

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

        Temperature and Humidity Logger created by Legened
        Temp= 27.0ßC RH = 37.0 %
        Temp= 27.0ßC RH = 41.0 %
        Temp= 26.0ßC RH = 41.0 %
        Temp= 27.0ßC RH = 40.0 %
        Temp= 26.0ßC RH = 41.0 %
        Temp= 26.0ßC RH = 40.0 %
        Temp= 26.0ßC RH = 40.0 %
        Temp= 27.0ßC RH = 39.0 %
        Temp= 26.0ßC RH = 40.0 %
        Temp= 26.0ßC RH = 40.0 %
        Temp= 26.0ßC RH = 40.0 %
        Temp= 26.0ßC RH = 40.0 %
        Temp= 26.0ßC RH = 40.0 %

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top