Temperature and humidity data logger with PIC18F4550 and DHT22 sensor

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:

pic18f4550 data logger output

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 with microsd card, ds3231 RTC, dht22 and lcd

PIC18F4550 data logger circuit:
Project circuit diagram is shown below.

PIC18F4550 data logger circuit with, SD card, DHT22, DS3231 and LCD

(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:

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


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

4 thoughts on “Temperature and humidity data logger with PIC18F4550 and DHT22 sensor”

  1. 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
    smiller.jose@gmail.com

    thanks

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

  3. 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!!

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