Save data with atmega 328

Can you help me? I need to open a fat file system on the SD Card and save the data from the sensors and inputs to this file at 1 minute intervals.

What sensors? How many? What board? What have you tried?

Oh, I missed one - what inputs? ... and... do you need exact time stamps in the file?

temperature and pressure sensors 3 and 3 voltage inputs

That's only "what kind of sensors". Please provide details or links. You didn't answer about the board. Generally you haven't told us enough about your hardware, the state of your project, or what your problem is.

Did you have a look at the examples that come with the SD or SDFat library? They will show you how to write to a file.

You can write the value of a sensor followed by a comma followed by the value of another sensor followed by a comma and so on; after the last value, you can use an empty println for a new line (end-of-record).

The timing can be achieved with millis().

...unless you require absolute time stamps. Then you need an RTC. That is why many SD shields also have an RTC on board.

Here is the code I used to prove out the SD function in my project. I suggest you do the same. Meaning get the SD working then add in the sensors.

  SD card read/write
  Mod 01 make file name a string and iterate until we find a unused file name.
  Mod 02 add capability to increment filename.
  Rev 03 tested successfully on Arduino M0

  SD card attached to SPI bus on ICSP Header.
  SD Board power = 5V (for large board with EEPROM)

  based on code created: Nov 2010 by David A. Mellis, 9 Apr 2012 by Tom Igoe
  This code is in the public domain.

driver has a 512 byte buffer then write to SD

Closing the file forces any buffered data to be written to the SD and also updates
the file's directory entry.

If you don't close the file, you will lose all data written the file since it was opened,
not just the last buffer, since the directory entry will not be updated.

#include <SPI.h>
#include <SD.h>

// *** SD Card declarations **************************************************
// ***************************************************************************

#define SDCARD_CS_PIN 4

uint8_t fileNumb = 100;
char dataFile[8];
bool SD_Error = false;

File myFile;			// create instance of a "File" class

void setup() {
  delay (2000);
// Initializing SD card....
  if (!SD.begin(SDCARD_CS_PIN))
    {SerialUSB.print("initialization failed");
     SD_Error = true;

// loop until we find a file that doesn't already exist.......
     itoa(fileNumb, dataFile, 10);  // (value, Array, base)
     const char *extension = ".csv";
     strcat(dataFile, extension);  // syntax  strcat(dest, source)
    } while (SD.exists(dataFile));

  myFile =, FILE_WRITE);   // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  // if the file opened okay, write to it:
  if (myFile) {
    myFile.println("data from boiler");     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    SerialUSB.println("data from boiler");
    //SerialUSB.print(" data written to file:   ");
    myFile.close();                         // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  else {
    // if the file didn't open, print an error:
    SD_Error = true;
  SerialUSB.print("SD_Error = ");
void loop() {
  // nothing happens after setup

Random nerd should have a tutorial

I don't know if you care about the amount of data you collect, however I wanted to compress my data by not logging data that has not changed.
So I add an RTC clock and only logged data if an input changed.

when i compile the code I got error SerialUSB not declared in this scope

What Arduino board are you using? They do not all handle USB the way you want to use it.


I used none of them only I was copmlier this code

OK so… what board did you select in the IDE?

Did you install the library you are trying to use?


you must select a board and it must match the board you have in order for the compiler to work.

if you want our help, you have to let us know what you have.

yes, I installed the SD library in Arduino IDE, I just compiled it, I didn't install it because I don't have a card

i am working on ardunio uno,

Replace all SerialUSB by Serial.

You are right Its succesfully done thank you

A library must have changed, This is old code.

I'll see what I can find.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.