Temperature Logger to SD Card with Menu Control – XC8

temperature logger to sd card with menu setup

Temperature Logger to SD Card with Menu Control – XC8

An SD/SDHC 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 20 seconds and stored in a file on an SD card. 
When the program starts, the user is given two options: 

  • Option 1: Save the temperature readings to SD/SDHC card (a new text file will be created: “DATA.TXT” or if this file already exists, it will be appended)
  • Option 2: Display the temperature values to PC

The user will have to choose one option 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.

If you prefer to watch a video tutorial, please watch the video below:

Project Setup

In this project the PIC16F18346 is used on the Curiosity development board. The LM35 temperature sensor is connected to analog ANA0 of the PIC.

microSD Click is connected to SPI pins of the PIC: SDI1 to RB4, SDO1 to RC7, SCK1 to RB6 and CS to RC6. The serial to USB module’s TX is connected to RB7 and RX to RB5.

To learn more, please refer to these tutorials first:

Reading and Writing to SD Card with PIC Microcontroller using MPLAB Code Configurator

Analog to Digital Converter in PIC microcontroller

PIC Communication with RS232 Bus

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

They are now running a big sale for this end of the year 2019, you could earn free gifts, coupons and sales up to 70% off for PCB prototype and assembly.

They are offering Christmas coupons of up to $200 that you can claim from 1st December to 31st December 2019, huge discount up to 70% off for PCB prototype and assembly, this include Advanced PCB and FPC/Rigid-Flex and cash rewards and electronic gifts by just sharing your projects on PCBWay’s community.

To learn more please click on the image below to direct you to their website.

Microchip File System Library 

The File System library provides access to the FatFs file system library and SD Card library. In this tutorial we are using File System library v1.00
FatFS is basically Chan’s FatFs a generic FAT file system available through an open source license. It provides functions to writing to physical media devices that use the FAT file system, a common file system used on removable media devices like multi-media cards or USB memory sticks.

For more information regarding FatFs including module settings documentation, FatFs application notes, and file system API documentation please visit the FatFs Home page: http://elm-chan.org/fsw/ff/00index_e.html

System Module:

We selected the Internal Oscillator and set it to 16MHz and enabled the Low-Voltage programming mode, this is important as the curiosity development board works only in Low-Voltage programming mode.


In the Device Resources, ad the FatFs library, it will also add automatically the SD Card (SPI) under the Foundation Services library

Click on Insert Driver to create a physical driver and set its label name. You can click on the Help button on the FatFs to get more information on how to configure this section.

The rest of the setting are more or less left to their default values. You can play around with these settings to disable the features you don’t need to optimize the code.



The STDIO to USART is not checked here to use the standard fprint() to save some memory of this chip we are using. We will use our own function to print a string to serial output:

Main application

On start-up, the project will display menu on the PC screen.

If choice 1 is selected, the temperature will be read every 20 seconds and saved to sd card.

Below is a sample data saved to SD card:

If choice 2 is selected, the temperature will be read every 20 seconds and displayed to PC.

You can download the full project files (MPLAB X project) 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: MPLAB Project Temperature_Logger_SD_Card

Share this post

has been added to your cart:
Select your currency