External power vs. USB power and SD card initialization problems, Arduino Micro


hi all,

i writing post based on problem , solution discovered when using arduino micro (atmega32u4) , using sd library initialize adafruit micro sd card break-out board.

in short problem began when using not usb power computer supply power arduino while ran initialization. using combination of libraries taken sparkfun sd card functions.

to replicate problem try using code below drive breakout board mosi, rx_led/ss, sck, , miso pin connections on micro, specified datasheet on arduino website.

code: [select]


  serial.print("initializing sd card...");
  // make sure default chip select pin set to
  // output, if don't use it:
 
  // see if card present , can initialized:
  if (!sd.begin(chipselect)) {
    serial.println("card failed, or not present");
    // don't more:
    return;
  }
  serial.println("card initialized.");



in case noticed variable chipselect, did not matter value passed; code compile , run when connected usb power. if powered board via vin, +5v or usb via wall adapter code fail initialize. seems there "magical" (read variable declaration mismatch within pin mapping support files) connection using computer initialization.

the solution problem change variable chipselect instead ss (see below), assumed hardcoded pin name micro (matching pin names , numbers micro nightmare) , presto chango code works type of power supply.

 i commented out serial communication lines debugging , haven't gone replace function see if breaks code again.

code: [select]

  // see if card present , can initialized:
  if (!sd.begin(ss)) {
//    serial.println("card failed, or not present");
      digitalwrite(greenled, high); //indicates sd card being written to
 
    // don't more:
//    return;
  }
//  serial.println("card initialized.");



i hope helps else out there, know there lot of discussion around setting pin modes outputs , writing them high before initializing , tips well.

thanks,
se



as follow here code, before , after when debugging it.

before (with power problems):

code: [select]

//time stamped datalogger onto sd card


// change match sd shield or module;
// arduino ethernet shield: pin 4
// adafruit sd shields , modules: pin 10
// sparkfun sd shield: pin 8
const int chipselect = 4;
const int redled = 7;
const int greenled = 6;

//the rtc library needs wire
#include <wire.h>
#include "rtclib.h"
rtc_ds1307 rtc;


//the sdlibrary needs spi
#include <spi.h>
#include "sd.h"

//include accelerometer libraries

#include <sfe_mma8452q.h> // includes sfe_mma8452q library

//some sd card code borrowed adafruit library examples
// set variables using sd utility library functions:

file datafile;
#define logfile "datalog.txt"

//array declarations

int arrayavg[3];


void setup() {

   // open serial communications , wait port open:
  serial.begin(9600);
  
  serial.print("initializing sd card...");
  // make sure default chip select pin set to
  // output, if don't use it:
  pinmode(10, output);
  
  // see if card present , can initialized:
  if (!sd.begin(chipselect)) {
    serial.println("card failed, or not present");
    // don't more:
    return;
  }
  serial.println("card initialized.");


//rtc setup + initialization first time upload  
//  serial.println("starting real time clock");
  #ifdef avr
    wire.begin();
  #else
    wire1.begin(); // shield i2c pins connect alt i2c bus on arduino due
  #endif

  rtc.begin();
  rtc.adjust(datetime(f(__date__), f(__time__)));  //uncomment once , upload. comment out , upload again
  //this set time first time upload , if don't upload again commented out
  //reset clock time code compliled each time.
  
  if (! rtc.isrunning()) {  //code borrowed adafruit example rtc code
    serial.println("rtc not running!");
    // following line sets rtc date & time sketch compiled
    // line sets rtc explicit date & time, example set
    // january 21, 2014 @ 3am call:
    // rtc.adjust(datetime(2014, 1, 21, 3, 0, 0));
  }

// led setup

  pinmode(redled, output);
  pinmode(greenled, output);
  

}
void loop(){

  digitalwrite(redled, high); // power main code
  
  datetime = rtc.now();
    serial.print(now.year(), dec);
    serial.print('/');
    serial.print(now.month(), dec);
    serial.print('/');
    serial.print(now.day(), dec);
    serial.print(' ');
    serial.print(now.hour(), dec);
    serial.print(':');
    serial.print(now.minute(), dec);
    serial.print(':');
    serial.println(now.second(), dec);

  

  int sensorvalue = analogread(a0);  // read input on analog pin 0:
  double sum = 0;
  
    for (int =0; <3; i++)
    {
      arrayavg[i] = sensorvalue;
      sum = arrayavg[i] + sum;
    }
    // convert analog reading (which goes 0 - 1023) voltage (0 - 5v):
    float voltage = (sum/3) * (5.0 / 1023.0);
  // print out value read:
  
//  serial.println(voltage);
 
  int buttonstate = digitalread(12);
 // serial.println(buttonstate); //used troubleshooting button state
  if (buttonstate == high){
  
    // open file. note 1 file can open @ time,
    // have close 1 before opening another.
    // note: cannot manipulate data on sd card on computer
    file datafile = sd.open("datalog.txt", file_write);
    digitalwrite(greenled, high); //indicates sd card being written to
  
    // if file available, write it:
    if (datafile) {
    
      //time stamping
        datafile.print(now.year(), dec);
        datafile.print('/');
        datafile.print(now.month(), dec);
        datafile.print('/');
        datafile.print(now.day(), dec);
        datafile.print(' ');
        datafile.print(now.hour(), dec);
        datafile.print(':');
        datafile.print(now.minute(), dec);
        datafile.print(':');
        datafile.print(now.second(), dec);  
        datafile.print(' ');
      
      datafile.println(voltage);
      datafile.close();
      // print serial port too:
      serial.println(voltage);
    }  
    // if file isn't open, pop error:
    else {
      serial.println("error opening datalog.txt");
    }
  }
  else
  {
   digitalwrite(greenled, low); // turn off led when not in use
  }
  
  delay(100); //slow code down
}





Arduino Forum > Using Arduino > Storage > External power vs. USB power and SD card initialization problems, Arduino Micro


arduino

Comments