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

Leave a Reply

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