Temperature Logger to SD Card with Menu Control – MikroC

Temperature Logger to SD Card with Menu Control – MikroC

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

An SD/SDHC card 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 1: Saving 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 is 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 
(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 these articles first: 

Interfacing SD Card with PIC Microcontroller

Interfacing LCD Display with PIC microcontroller

Analog to Digital Converter in PIC Microcontroller and 

PIC Microcontroller Communication with RS232 

Prototyping on PCB is always robust with few errors due to poor connections. For rapid development, you can buy an SD card board that you can plug directly into you development board or PCB headers. Below you can add to cart the devices you can use in this project:

The other and probably most important is also the cost to manufacture your PCBs. The quality of a PCB can affect the overall quality of the whole electronic device.

There are many PCB companies in the world that can make you good quality PCBs, but we need one that can produce cheap boards of high quality on small orders because the first batch of any project is likely be of small quantity. One of those companies we can recommend is PCBWay.

PCBWay is a china based PCB manufacturer specializing in PCB prototyping, small volume production and PCB assembly service all under one roof with more than a decade of experience. You can get 10 high quality PCBs (size: 100mm x 100mm or smaller) for just $5 USD. New members receive $5 bonus which can be used to get your first order completely free.

You can pick between these solder mask colours at no extra cost: Green, Red, Yellow, Blue, white and Black. Please read this article to learn more about PCBWay:

Low Cost PCB Prototype and Assembly with PCBWay.

And if you are a student or an educator, you could get PCB for free, read this article to find out how:

Free PCBs and Discounts for Students and Educators with PCBWay’s Sponsorship Program.

Alternatively you can contact them from their website, click on the logo below:

PCBWay Website

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

Share this post

You've just added this product to the cart:

Select your currency
ZARSouth African rand
EUR Euro