| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 | AnalogBinLogger.ino logs analog data to a binary SD file at high rates.Samples are logged at regular intervals by using timer1.  Timer/Counter1Compare Match B is used to trigger the ADC for the first pin in a sample.The ADC is triggered for remaining sample pins in the ADC conversion completeinterrupt routine.Data is captured in the ADC interrupt routine and saved in 512 byte buffers.Buffered data is written to the SD in a function called from loop().  Theentire data set is written to a large contiguous file as a single multi-blockwrite.  This reduces write latency problems.Many inexpensive SD cards work well at lower rates.  I used a $6.00SanDisk 4 GB class 4 card for testing.SanDisk class 4 cards work well at fairly high rates.  I used the 4 GB SanDiskcard to log a single pin at 40,000 samples per second.You may need to increase the time between samples if your card has higherlatency.  Using a Mega Arduino can help since it has more buffering.The bintocsv folder contains a PC program for converting binary files toCSV files.  Build it from the included source files.  bintocvs is a command line program.bintocsv binFile csvFile AnalogBinLogger requires a recent version of the SdFat library.  The SdFatfolder contains a beta version I used for development.The latest stable version is here:http://code.google.com/p/sdfatlib/downloads/listYou also need to install the included BufferedWriter library.  It providesfast text formatting.Example data for a 2 kHz sine wave logged at 40,000 samples per second isshown in DATA.PNG and FFT.PNG shows a FFT of the data.  See ExcelFFT.pdfin the ADCdocs folder for details on calculating a FFT.The accuracy of the ADC samples depends on the ADC clock rate.  See theADC_ENOB.PNG file for a plot of accuracy vs ADC clock frequency.See files in the ADCdocs folder for more information on ADC accuracy.To modify this program you will need a good knowledge of the ArduinoADC, timer1 and C++ programming.  This is not for the newbie.I have an LED and resistor connected to pin 3 to signal fatal errors anddata overruns. Fatal errors are indicated by a blinking led.  Overrun errorsare indicated by a solid lit led.  The count of samples dropped is writtento the SD and data logging continues.You can disable the error led feature by setting the error pin number negative:To use AnalogBinLogger, install these items. Place the BufferWriter and SdFat folders in your sketchbook libraries folder.Place the AnalogIsrLogger folder in your sketchbook folder.You must edit the configuration constants at the beginning of the program to set the sample pins, sample rate, and other configuration values.Initially the program is setup to log the first five analog pins at 5000samples per second.  Change these values to suit your needs.See RateTable.txt for maximum allowed sample rates vs pin count and ADC clockfrequency.The program has four commands:c - convert file to CSVd - dump data to Seriale - overrun error detailsr - record ADC dataAll commands can be terminated by entering a character from the serial monitor.The c command converts the current binary file to a text file.  Entering acharacter on the serial monitor terminates the command.The d command converts the binary file to text and displays it on the serialmonitor.  Entering a character on the serial monitor terminates the command.The e command displays details about overruns in the current binary file.Data overruns happen when data samples are lost due to long write latencyof the SD.The r command will record ADC data to a binary file.  It will terminatewhen a character is entered on the serial monitor or the the maximum fileblock count has been reached.A number of program options can be set by changing constants at the beginningof the program.  
 |