====================================================================================================

Tuesday, May 10, 2011

microSD ATmega32 Data-Logger



Hi friends,

aim of this project is to present a way to store a large quantity of data into microSD card in files with FAT32 format. Here, ATmega32 is used for data collection and microSD interface. The data is received from in-build 8-channel ADC of ATmega32. One channel is used for reading temperature from LM35 sensor and remaining channels are used for simply reading voltages and storing them.

This project can be used to interface 8 different sensors with ADC of ATmega32, similar to the LM35 used here. The data is stored in CSV (comma separated values) format, which can be read using a PC/Laptop with Microsoft Excel or other compatible software. A snapshot of the excel file is given later in this post.

This project is an example of how to use the microSD FAT32 library presented in my earlier post. In that post, the files were created using hyper-terminal and entering data with the PC keyboard, since that demonstrates the file creation and it's easy to debug. But many users have requested to make the file creation independent of the terminal, done inside the microcontroller, so I'm showing here how to use those functions independent of terminal. If you have directly landed on this page, it would be more helpful if you visit the original post first as it would be a better starting place for learning SD or FAT32 functions.

Here is the schematic (click on the images for larger view or download PDF):

The project contains RTC interface (for date and time storage), RS232 (for connection with PC) and a microSD module. Here, the hyper-terminal connection is required only for setting RTC date and time. Once the date/time are set, the RS232 connection is not required anymore for normal data-logging operation (It can be used for debugging purpose if there is a problem).

The microSD module used here is from eXtreme Electronics.
The module is shown in the figure here. Other than the microSD socket, this low-cost module also contains on-board 3.3v regulator for the microSD card, a 5v-3.3v level converter and other safety features required for the card. This module is used here as it provides a stable interface and makes the the card compatible with 5v supply and 5v signals of microcontroller.

The module is available at: http://store.extremeelectronics.co.in/MicroSD-TF-Module.html

The schematic also shows two LEDs and a push-button. The LEDs are used for indications of power and recording and the push-button is used to start-stop recording.

Operation of the circuit:
-----------------------------------------------
For setting RTC date/time (or for debugging mode):
  • Connect the microSD module, insert the microSD card
  • Connect the RS232 cable with the circuit. Set-up hyper terminal with 19200 baud, no parity, 8-bit data, 1 stop-bit and flow-control as 'None'
  • Connect the power cable and power on the circuit while keeping the push-button pressed
  • Green LED will glow in the circuit board
  • A menu will be displayed on the Hyper terminal as shown in the figure below. Select desired option and follow the displayed instructions
  • When date/time is set or debugging done, select option '0' to come out of the menu and start functioning a s data-logger
  • At this point, the RS232 cable can be removed


Operation as Data-Logger:
  • Connect the power cable and power on the circuit
  • Green LED will glow
  • Whenever the data-logging is required, press the push-button
  • Red LED will glow, indicating that the recording has started
  • To stop recording, press the push-button again, recording will stop and red LED will turn off
  • Files stored in the card can be read using a PC card-reader or using hyper-terminal with the circuit started in debugging mode

The operation is very simple as it uses just one push-button and an LED indication. In case of any error in accessing the card, red LED will blink continuously. In such a case, you can start circuit in debug mode (with terminal) and see the error messages.

Files are stored with the date as a name and .CSV extension. For example, data-logging done on 10 May 2011 would be stored in "10052011.CSV" file. Since the date is the name of file, everyday a single file is created and all the data recording done in a day goes into single file, no matter how many times the recording is stopped/started. First column of the file shows date, second shows time and next 8 columns show data from the 8 channels.

A file created during testing is shown in the figure below, where 5 sec interval was set for measurements (click on the image to enlarge it). Here channel-0 was used for LM35 temperature sensor, and remaining channels measure voltage. 5v was connected to channel-1 and 3v Li cell was connected to channel-3 (Channel 2 & 4 show some small voltages due to noise from voltages connected to nearby channels, which can be corrected by using bypass caps).


The interval between two measurement cycles is defined in main.c file, which can be set as per the user requirement. Basically, the program forms a dataString in every measurement cycle and appends this string to the file, if the file already exists or it creates a new file (for example, during the first recording in a day). You may go through the comments in the source code file for more info.

Note: Make sure that RTC circuit is properly connected, otherwise the code will simply hang waiting for receiving date & time from RTC

Download project files
--------------------------------------
The source code is written in AVR-GCC format using winAVR with AVRStudio-4, complete AVRStudio project folder can be downloaded from here:

- Download Source Code

Download schematic:
- Schematic (PDF)
- Schematic (EAGLE)

References:
--------------
Visit my earlier post for the references on SD card & FAT32 which are given at the end of the post

58 comments:

Anonymous said...

Great job Sir!

James said...

Gr8!! I have already tested your earlier project on SD cards and FAT32 without any problem. I'm glad u finally posted this one which doesn't need the terminal to create files. Thanks for sharing!

Nagaraj.P.R said...

Great job!!!

Matej said...

Excellent! I think this will be easily portable to other AVRs, like ATmega128.
Are the other files the same as in the earlier project? For example I2C? I have added other I2C peripherals and would like to reatin their functionality while implementing the datalogging features to the device.
Perhaps a little "feature request": Would it be possible to keep datalogging running while temporarily ejecting the card (for the purposes to download the data to PC)? The data would be temporarily going to AVR internal EEPROM (or maybe even flash?) and after inserting the card back in, the data would be flushed. This way, no data would be lost in the process .. ?

CC Dharmani said...

Hi Matej,

the remaining I2C functions are same as the earlier project.

The EEPROM part will ask for addition of little bit of hardware and some more software functions, which may not be required in case of most users. But yes, in case of non-stop data recording, even when the card is ejected, EEPROM part would be necessary. May be in the future I can provide this customization as a separate download, but till then, you can also give it a try to do the modifications.

santoshaxl said...

how to set time and date before starting system ?

Venice said...

Great job, and thanks. I am trying to configure this circuit as well.
By the way, is it 8 Mhz-internal used or is it external 16MHz crystal?
because i noticed that the schematic diagram written 16MHz external crystal, but the coding .c file mentioned 8MHz-internal. So which one is correct? I programmed the code into Atmega32 but cant see anything on the hyper terminal. If it is 8MHz-internal, how to configure it?

CC Dharmani said...

@Santosh: During the first start-up, as mentioned in the operation of the circuit, connect the RS232 cable, open hyper terminal (19200 baud, no parity, flow control-none) and power on the circuit while keeping the pushbutton pressed. You'll see the options on the terminal for setting date/time etc.

@Venice: Yes, the controller is working at 8MHz internal. The external crystal is optional. For setting the internal clock, you need to change the fuse values. You can use following values:
Low fuse: 0xE4
High fuse: 0xD9

For more details of fuse values, refer to 'memory programming' section of the avr datasheet.

Venice said...

I re-checked and yes, I have previously set the fuse bit correctly. However, I still cant see anything in hyper terminal although I pressed the button once the circuit is powered. Baud rate 19200 and COM port are all correct. Well, I will continue to try and to figure out which part is wrong.
by the way,
1) the button is active low right?
2) in the schematic diagram, the microSD module, pin 6 connecting to PB3, from the extremeelectronics module, I saw pin 6 is CD. What is this pin? Since on MicroSD, there are only pins such as GND, VDD, CS, MOSI, MISO, SCK. So where comes the CD in normal MicroSD adapter.
Please advice and thank you.

CC Dharmani said...

Hi Venice,
if you want to see the full menu, you need to keep the button pressed while powering on, that means you press the button first and keep it pressed when you power the circuit on. But anyways, even if you don't press the button, you'd atleast get the first three lines on the terminal, mentioning the project title. May be you can recheck the RS232 connections, because the terminal would show atleast some garbage, if only the baudrate is not matching.

Yes, the button is active low.

The CD pin is actually 'Card Detect' pin which is a switch mechanically connected with card insert mechanism. I've shown it in the schematic, but not used it in the program, just to make the code compatible with previous versions. But it can be used to detect whether the card is really inserted or not.

paresh said...

hello sir, it's a great post but my question is do we need to attach external module for sd card connection compulsary. and if so why to use that one.

Anonymous said...

can we read the data again? for example I want to log data first. after complete, i read the file again and sent the data to computer using serial

CC Dharmani said...

@Paresh:
No, it's not compulsory to use external module for the microSD card, it's used here to make job simpler. But if you don't have access to such a module, you can use method suggested in schematics of my previous post:
http://www.dharmanitech.com/2009/01/sd-card-interfacing-with-atmega8-fat32.html

@Anonymous:
Yes, you can. Once the data is written and file is created or updated, you can stop the logging and go to debug mode as mentioned in the operational procedure, to read the file using hyper-terminal.

Walther Grube said...

Great job! I'm currently building a similar project, using an Arduino nano. I'm using FAT16lib, works fine, but I will try FAT32 eventually. I'm also using LM35 sensors, combined with a RC damper (as in the datasheet), this gives better results. I'm waiting for some sensors (CO2, humidity, pressure, etc) that I've bought to complete my circuit.

Venice said...

Hi, I have managed to log the values into SD card.
However, for the 3v lithium battery, should the '-' of the 3v batt common ground with the entire circuit?
Because if I common ground the 3v batt with the entire circuit, the LED indicating error kept blinking once I press the button to start the data log. If without the 3v Li Batt, the data can be logged, but the time cannot keep counting once the circuit is power off. So what's the problem that causing the LED to keep blinking everytime when I common ground the '-' of 3v natt?

CC Dharmani said...

@Venice:
The battery -ve terminal has to be connected with common GND of the circuit. It's recommended in the datasheet of the chip, too. The problem you are facing must be related to some other connections, as I've used the RTC chip in this way in many of my circuits and I do not find such problem. You can check out if the +ve terminal of battery is touching to other lines. Do write back here if problem still persists, I will try to create it in my circuit to find how it happens.

biswajit said...

sir myself biswajit from bbsr orissa. i am a big fan of your blog. i have tried all your posts and it works fine. but i have a doubt regarding that cluster, sector and block in microsd. i will be thankful if you help me out. thanks in advanc

Anonymous said...

I have tried to run this code in avrstudio 4 but at the time of 'build' operation it gives 43 numbers of error. (e.g- undefined reference to 'spi_init', undefined reference to twi_init' etc. Could anyone help me

Xavi said...

Great post man, keep up the good work!! Very useful ;)

CC Dharmani said...

@Anonymous:
Hey man, lemme know what's wrong!! The only reason I find about the errors is that u might be using some other compiler!! Use winAVR with AVRStudio and you won't have fortune to see a single error as compared to your 43 errors!!!

@Xavi:
Thanks, buddy!!

Anonymous said...

I want to measure the time difference between two events (using different ports) in nano second range using atmega 32 microcontroller. I will be highly obliged if anyone help me.

aslan said...

Great Job sir!

amiR said...

hello..
great post.
may i ask a stupid question because i am getting started with building a uC project.
can i start recording without press the button first??
i am building a security system.
i want to start recording all events captured by lm35 and magnetic switch after the system being activated and automatically stop recording after deactivate it.

CC Dharmani said...

@amiR:

Certainly u can do that.. U need not press the button at all, just put up the condition which makes the system to activate and deactivate, in place of pressing the button, you'll get the recording..! You need not press the button at all..

If you still have some confusion, send me your schematics and mention which input you are using for button input, I'll send u the modified code..

CC Dharmani said...

Thank you, aslan!!

amiR said...

i've sent my last schematic to your email.

i wish you could help me,thank you so much..

Anonymous said...

do you have a sd card version?
thanx

Madhu said...

Dear sir,

I used your schematic of ATmega8 interfaced with SD card and the code that you have provided. I tried to fill the memory of 1GB card. I am able to write 128 text files only using FAT 32. If i write more files they are getting corrupted. I am able to write only 32 MB of 1GB card. Please suggest me how to access whole of 1 GB memory or how to write more than 128 text file into sd card?

Expecting for you reply,

Thanks and regards,

Madhusudan

haziqchi said...

hello sir...i like your idea of this project...and i intend to do this project for my final year project and i would like to combined it with this project (http://embedded-lab.com/blog/?p=2425) can you list all the components needed to build this data logger project....really appreciated if you can reply asap....here are my email peace_world91@yahoo.com

lee english said...

hello sir..can i know where i can get this project??and how much cost of it??how many days for shipping to malaysia?? please email me at hazel_must@yahoo.com..thanks..

Luca said...

nice job and namastē for having shared it. Just a curiosity of mine: what is the maximum sample rate? I mean, according to the source it can goes up to 100 Hz (100 S/s) but the rtd you used has 1 s of resolution.
BR
Luca

CC Dharmani said...

@Luca: Ya, though I've mentioned the max. sampling of 100Hz (min. delay of 10ms), due to RTC's resolution limit, time will be written same for those readings taken during the same second.
(Note: The actual max. rat will be less than 100Hz, since the delay loop written there doesn't account for overheads occurring in calling functions, writing to the card etc.)

james said...

hye sir!!i'm glad 2 see your work!!can u help me please??can RPM replace the temperature sensor and what is RPM ic??can it be combine together with your circuit sir??

Shagufta said...

Hai...
Great work sir. Iam doing a project to send 32 bit data from atmega32 to pc using RS232 serial communication. I know to transfer 8 bit data but I dont know how to transfer 32 bit data. Please help me.

Regards,
Shahgufta

Mika said...

Hi,
I,ve downloaded your code, put it to my new atxmega256a3b and nothing worked...Initialization passed(returned 0, card type-standart capacity), but when I'm trying to get boot sector data, function returns 1...I was debugging code for whole day, piece by piece and find out, that uC doesn't get start block token (0xfe). I've formatted my SD card (2GB) as FAT32, with 512 b. MAby something wrong with initialization, that card doesn;t understand next commands or card formatted wrong...
Please help :)

Mika said...
This comment has been removed by the author.
Mika said...

Hi again,
I've solved problem, now it's working :) My avr studio 5 Gcc compiler doesn't change types of variables, no typecasting.

kiptoo said...

hi sir been trying to build and compile the source code given in thi post version 1.0 but it has so many undeclared functions.
your assistance on this will highly be appreciated as it will help me in debugging of codes

kiptoo said...

I have tried to run this code in avrstudio 4 with AVRGCC compiler but at the time of 'build' operation it gives 43 numbers of error. (e.g- undefined reference to 'spi_init', undefined reference to twi_init' etc.i believe the winAVR you were refering to is the AVRgcc

Anonymous said...

Good Job, great work.
However, I found there's a bug. Whenever the appended filesize equals to the multiples of the cluster size (512bytes), forexample, when I write 128 times of 20 bytes data, = 2560bytes. Starting from 2560bytes, the file will be corrupted and can't be recognised by Windows.

Mika said...

yes, there are bugs with corrupted files...my uC restarts, because of SD card writing failure and trying to write to the same file, the file is corrupted and uC restarts over and over again...could you tell me where is mistake in code?thank you

Mika said...

Hi again,

In my opinion(it may be wrong), there is something wrong with function - writeFile. When function appending an existing file i overflows 512 -max value:

i = fileSize % bytesPerSector;
while((data != '\n') && (k < MAX_STRING_SIZE)) {
data = dataString[k++];
buffer[i++] = data;
}

bytesPerSector=512, so if fileSize is not mulptiple by 512, i overflows...

Maby something is wrong in my thoughts, please comment.
Thank you.

Mika said...

I'm very sorry about last comments, I'm newby in programming and electronics... i was looking for mistakes in code and analyzed doubtfull parts:) it is everything OK with code, my Xmega permanently restarted due to my fault, not because of the code... my hardware was bad -I forgot to put schottky to my switch mode power supply, so when uc was starting writing to SD card, voltage dropped down and BOD protection restarted uC...

rinki arora said...

can u please tell me how to attach file in any blog for new post. i have just now create my eblog nw i have copy my word document in one of the post but images r not copied to it....and i don't want to insert single image. i ust want to attached some file which i have made...so plz help me..i also want to create blogs.
my email id is rinki.arora88@gmail.com

Ken said...

Hello Sir,
nice job here. Please I want to find out if I can use this data logger to measure and store the voltage and current from a 12V car battery. Thanks.

kajianmaskam said...

thats awesome sir...
but i totally dont understand :), because maybe i havent learn avrgcc yet, i usual use codevision for the compiler, and the sintax little bit different between them...
would you write it in codevision?
thanks a lot.. :)

kajianmaskam said...

especially, when write data with elm chan lib.
like in the example:
char text[]="i like codevision avr";

to logging the data like:
char text[]=" %d , %d \r\n", temp, volt; <<-- but it doesnt work..

Mohan Meena said...

Hi
I was going though your code which supports SDHC card. The routine which converts normal short file name into FAT format. I could not get this part of the code which is used for setting file extension. Can you please explain the concept used in this. For setting file extension we use first three letters of file extension. Are there any other rules also for that? What if file have only 2 character extension?
Please reply soon


for(k=8; k<11; k++) //setting file extention
{
if(fileName[j] != 0)
fileNameFAT[k] = fileName[j++];
else //filling extension trail with blanks
while(k<11)
fileNameFAT[k++] = ' ';
}

sidd said...

sir we need a code to display content of text file stored in mmc on 16x2 lcd

mst said...

Hi, how can I faster sampled data. I need about 100-200 Hz. I'm not using RTC, I tried different settings in ADX, but it does not change anything. I removed the loop with the INTERVAL. Please help me.

mst said...

Not in the ADX but the ADC ;)

Vicky V.K. said...

Respected Sir,
Its great project, I am final year student, i choose your project for my final year project but i want to add another four different sensors for remaining port pins in portA so which kind of changes required in schematic as well as in your posted coding, i was tried to add by taking yours posted coding reference but bugs are in my coding still remains. I really want your valuable Guidance Sir.
Thank you

Yours Student
Vicky V.K.

Vicky V.K. said...

If you suggest me the changes in coding and schematic please send on my mail id: vkcontrol@rediffmail.com

sunil said...

Hi sir,
Gr8 job.I am using silabs mcu i.e c8051f930 using this with SPI i need to write and read the data from micro sd please suggest whether your code is working with this fine or not?.Please let me know.
Thanks.

Julchen said...

After reading this, I've decided NOT to buy microSD cards from China. Ever. I'll stick to official trusted retailers only. I'm sure SD cards and FAT32 will really work flawlessly for me too.

ivante said...

Just drop by to say thanks a lot for the share!
I had a datalogger project which log current, voltage, and temperature data for recording photovoltaic activities. At first I kinda confused but thanks again it's now Done!

Regards,
ivantriyadi@gmail.com

nghia said...

Hi you, I have an electronic scales the second line RXD and GND pins, please help me save the signal circuit on sd card, always ask for their code and how to load code, thank you very much

nghia said...

Hi you, I have an electronic scales the second line RXD and GND pins, please help me save the signal circuit on sd card, always ask for their code and how to load code, thank you very much