Temperature data logging with PIC18F4550 and DS18B20 sensor

In the previous post I’ve connected the DS18B20 temperature sensor with PIC18F4550 microcontroller in order to measure the ambient temperature, a 16×2 LCD was used to display the measured temperature. Now I’m going to add an SD card for storing the temperature values every 1 second. In this project DS3231 real time clock chip is used to keep time running even if the main power source is removed from the circuit, time and date are set using two pushbuttons connected to the PIC18F4550 microcontroller.

In the previous data logger projects I used SD cards (microSD) with FAT32 file system and in this project I’m going to test the FAT16 file system. Note that to be able to format a SD card with FAT16 file system, its size must be less than or equal to 2GB.

Related Projects:
Temperature and humidity data logger with PIC18F4550 and DHT22 sensor
PIC18F4550 with DS18B20 sensor and LCD project
DataLogger with PIC18F4550, SD card and DHT11 sensor
Read and write files from and to SD card with PIC18F4550 – CCS C
Real time clock & calendar with PIC18F4550 and DS3231 – CCS C

Hardware Required:

  • PIC18F4550 microcontroller   —> datasheet
  • FAT16 formatted microSD card (size less than or equal to 2GB)
  • DS3231 board with 3V coin cell battery   —>  DS3231 datasheet
  • DS18B20 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

Temperature data logger with PIC18F4550 and DS18B20 circuit

Temperature data logging with PIC18F4550 and DS18B20 circuit:
Project circuit schematic diagram is shown below.

DS18B20 data logger with PIC18F4550, SD card and DS3231

(All grounded terminals are connected together)

The DS18B20 sensor has 3 pins (from left to right): GND, data and VCC (+5V). The data pin is connected to PIC18F4550 pin RA0.

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 real time clock chip used in this project is the DS3231, with the help of the 3V coin cell battery it allows the time to keep running even if the main power source is disconnected form the circuit.

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

Temperature data logging with PIC18F4550 and DS18B20 C code:
The C code below is for CCS C compiler (PIC C), it was tested with 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 (for example C:\Program Files\CCS\Drivers):
SD Card driver and FAT Library for CCS C compiler

Or you can download them from the the 2 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.

This code reads the temperature from the DS18B20 sensor and write its value as well as time and date to a file named “DS18B20Log.txt” (located in the SD card) every 1 second.

DS18B20 functions:
void ds18b20_start(): used to send start signal (reset) to the DS18B20 sensor.
void ds18b20_write_bit(int1 value): writes (sends) 1 bit to the DS18B20 sensor, the bit is ‘value’ which may be 1 or 0.
void ds18b20_write_byte(int8 value): writes 1 byte (8 bits) to the DS18B20 sensor, this function is based on the previous function. This function writes LSB (Least Significant Bit) first.
int1 ds18b20_read_bit(void): reads 1 bit from the DS18B20 sensor, returns the read value (1 or 0).
int8 ds18b20_read_byte(void): reads 1 byte (8 bits) from the DS18B20 sensor, this function is based on the previous function. This function reads LSB first, returns the read byte.
void ds18b20_start_conversion(): sends start conversion command to the sensor.
void ds18b20_read(int16 *raw_temp_value): reads the temperature raw data which is 16-bit long (two 8-bit registers), the data is stored in the variable raw_temp_value.

DS3231 functions:
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 the simulation of the project with 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.

In my hardware circuit I used Samsung 2GB microSD card and the result is shown below where the “DS18B20Log.txt” file is clearly shown:

DS18B20 datalogger result

Downloads:
Proteus simulation file download:
Download
SD card image file (FAT16_SD) download:
Download

4 thoughts on “Temperature data logging with PIC18F4550 and DS18B20 sensor”

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