Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#include <SPI.h>
#include <SD.h>
// Set the pins used
#define cardSelect 4
File logfile;
// blink out an error code
void error(uint8_t errno) {
while(1) {
uint8_t i;
for (i=0; i<errno; i++) {
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
for (i=errno; i<10; i++) {
// This line is not needed if you have Adafruit SAMD board package 1.6.2+
// #define Serial SerialUSB
void setup() {
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.println("\r\nAnalog logger test");
pinMode(13, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(cardSelect)) {
Serial.println("Card init. failed!");
char filename[15];
strcpy(filename, "/ANALOG00.TXT");
for (uint8_t i = 0; i < 100; i++) {
filename[7] = '0' + i/10;
filename[8] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
logfile =, FILE_WRITE);
if( ! logfile ) {
Serial.print("Couldnt create ");
Serial.print("Writing to ");
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
uint8_t i=0;
void loop() {
digitalWrite(8, HIGH);
logfile.print("A0 = "); logfile.println(analogRead(0));
Serial.print("A0 = "); Serial.println(analogRead(0));
digitalWrite(8, LOW);
Copy link

WillyKirsch commented Jan 17, 2016

I've tried this sketch on Feather 32u4 Adalogger and doesn't write any data, males the file, but don't write the A0 data...

I've tried with logfile.flush() at 5s intervals and worked perfect!!

Copy link

jameswalker300 commented Jan 26, 2016

I am having the same issue with the M0 adalogger. How did you get it to flush every 5 seconds? thanks

Copy link

bioman666 commented May 14, 2016

Same here with m0.

Copy link

mikeprevette commented Oct 8, 2016

Yeah, had to add logfile.flush()

Copy link

ZonkerHarris commented Nov 20, 2017

The sketch still writes incremental filenames, but doesn't write any data to the files.
(I'm using an RTC, would love to be able to write a valid timestamp as well...)
When I add logfile.flush() at the end of my loop, all is well.
I suppose this is the same as putting the Open and Close into the loop, yes?

Copy link

0xD0M1M0 commented Nov 20, 2021

As per documentation of SD (

Opening/Closing files
When you use file.write(), it doesn't write to the card until you flush() or close(). Whenever you open a file, be sure to close it to save your data.

The documentation of the "Adafruit Feather M0 Adalogger" needs to be updated, as "" is saying that it writes on the SD-Card once the buffer is full and with this code it will not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment