Figure 1: Temperature Logger to SD Card with menu control circuit diagram

An SD Card can be used to log data continuously over time for various purposes, this project shows the design of a temperature data logger with menu control system. 
The ambient temperature is read every minute and stored in a file on an SD card. 
When the program starts, the user is given three options: 

  • Option 1Saving the temperature readings to a new file on an SD card (a new text file will be created: “TEMP.TXT”)
  • Option 2: Appending the temperature readings to an existing file on an SD card (Open the existing text file “TEMP.TXT” and continue saving from the last data)
  • Option 3: Sending all the saved file contents to a PC.

The user will have to choose one option and then press enter to start logging data. This menu only accessible once when the program starts, if the user wants to choose another option, he/she has to restart the program 
by pressing the reset button.

Circuit diagram

The circuit diagram above on figure 1 shows an SD Card interfaced to PORTC of PIC18F45K22, an LCD 
display to PORTB and a MAX232 level converter connected to the PIC UART module (RC6 and RC7) to communicate with a PC. 
The CS pin of the card is connected to RC2 of the PIC, Din of card to Do1 (RC5) of PIC, Do of card to 
Di1 (RC4) of card and CLK of card to CLK1 of PIC (RC3).
The power connections of the card are not shown here, but should be connected to the 3.3V variable 
regulator 
(U4). VDD and VSS of the pic microcontroller are not shown in the circuit diagram. VDD should be connected
to +5V and VSS to GND. An 8MHz crystal oscillator is connected OSC1 and OSC2 of the PIC.        

Variable voltage regulator

Figure 2: Variable voltage regulator

Please refer to the articles: Interfacing SD Card with PIC MicrocontrollerInterfacing LCD Display with PIC microcontrollerAnalog to Digital Converter in PIC Microcontroller and PIC Microcontroller Communication with RS232 for more information on hardware connection and software routines.

MikroC Code

In this project, a file called TEMPTXT is created on the SD card to store the temperature readings (the library 
function call will insert the “.” to make the filename “TEMP.TXT”), if it does not already exist. 

The following functions are created at the beginning of the program, before the main program:

Newline sends a carriage return and a line feed to the UART so the cursor moves to the next line.

Space insert a space between characters. 

Text_To_UART receives a text string as its argument and sends it to the UART to display on the PC screen. 

Read_Temperature starts the A/D conversion and receives the converted data into a variable called VoltIn. The voltage corresponding to this value is then calculated in millivolts and divided by 10 to find the actual measured temperature in °C. The decimal part of the found temperature is then converted into string form using function LongToStr. The leading spaces are removed from this string, and the resulting string is stored in character array temperature. 

Then the fractional parts of the measured temperature, a carriage return, and a line feed are added to this 
character array, which is later written to the SD card.

Inside the main program, the following operations are performed:

     Initialize LCD Display, 
     Initialize the UART to 9600 baud, 
     Initialize the SPI bus, 
     Initialize the FAT file system, 
     Display menu on the PC screen,
     Get a choice from the user (1, 2, or 3):

  • If the choice is 1, create a new temperature file, get new temperature readings every minute, and store them in the file 
  • If the choice  is 2, assign to the existing temperature file, get new temperature readings every minute and append them to the existing temperature file
  • If the choice is 3, assign the temperature file, read the temperature records, and display them on the PC screen. 
  • If the choice is not 1, 2, or 3, display an error message on the screen

The project can be tested by connecting the output of the microcontroller to the serial port of a PC (e.g., 
COM1) and then running the HyperTerminal terminal emulation software or any other serial terminal software
If you are using Microsoft windows XP, Go to Start, All Programs, Accessories, Communications and Hyper 
Terminal to open a windows free Hyper Terminal. 
Set the communications parameters to 9600 baud, 8 data bits, 1 stop bit, and no parity bit. Figure 3 below 
shows a snapshot of the PC screen when Option 2 is selected to save the temperature records in a new file.                

Snapshot of PC Screen

Figure 3: Snapshot of PC Screen

Full project Source Code

You can download the full project files (MikroC source code and Proteus Schematic design) below here. 
All the files are zipped, you will need to unzip them (
Download a free version of the Winzip utility to unzip files).

Download Temperature Logger MikroC Code

Download Temperature Logger Proteus