Arduino:如何正确关闭SD卡模块

时间:2014-11-24 21:27:54

标签: arduino sleep sd-card

我尝试使用SD卡构建数据记录器以保存传感器数据。我需要在电路进入休眠状态时立即降低功耗。问题是SD卡模块的功耗约为3mA。我读了很多关于省电的知识,很多人都把电源切换到SD卡模块,并在醒来时重新初始化卡。我无法做到这一点。只要切换SD卡模块,就会抛出错误消息。任何人都可以给我一些提示让我走上正轨吗?如何重新初始化SD卡模块?

由于

1。修改 一切正常,直到第一次醒来。抛出代码错误消息“卡失败或不存在”,循环将重新开始而不写入SD卡。 这是我到目前为止所得到的:

// DHT sensor library
#include "DHT.h"
// SD card library
#include <SD.h>
// for sleep modes
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>

#define DHTPIN 9     
#define DHTTYPE DHT22 //DHT11, DHT21, DHT22

DHT dht(DHTPIN, DHTTYPE);

// make sure that the default chip select pin is set
const int chipSelect = 4;
int counter = 0;
int sdPower = 8;
volatile int sleepcounter = 0; // count sleep cycles

void setup() {
 pinMode(sdPower, OUTPUT);
 // output, even if you don't use it to ensure proper SD library working:  
 pinMode(10, OUTPUT);
 digitalWrite(sdPower, LOW);
 watchdogOn();                // switch on Watchdog timer
 ADCSRA = ADCSRA & B01111111; // switch off ADC, ADEN bit7 zu 0
 ACSR = B10000000;            // switch off analog Comparator, ACD bit7 to 1
 DIDR0 = DIDR0 | B00111111;   // switch off digital input buffer, analog input pins 0-5 to 1

 dht.begin();
}

// -------------------------------------- LOOP ---------------------------------
void loop() {
 Serial.begin(9600);
 // -------------------------  initialize SD card  -------------------------
 digitalWrite(sdPower, HIGH);
 Serial.println("Start of recording");
 delay(500);
 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    delay(100);
    // don't do anything more:
    return;
 }
 Serial.println("card initialized.");  

// --------------------------------------------------------------------------
float humidity = dht.readHumidity();     //measure humidity
float temp = dht.readTemperature();      //measure temp

// make a string for assembling the data to log:
String dataString = "";
// -------------------------  read sensor and store in string  -----------------------------
// check for valid number, throw error for NaN (not a number)
if (isnan(temp) || isnan(humidity)) {
   Serial.println("no read for DHT22");
} 
else {
   dataString += "MP-";
   dataString += String(counter);
   dataString += ",";
   dataString += String(temp);
   dataString += ",";
   dataString += String(humidity); 
   // -------------------------  open SD card and write values  -----------------------------
   // open the file. note that only one file can be open at a time,
   // so you have to close this one before opening another.
   delay(1000);
   File dataFile = SD.open("datalog.txt", FILE_WRITE);

   // if the file is available, write to it:
   if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();  //dataFile.sync() doesn't change something
      delay(500);
      // print to the serial port too:
      Serial.println(dataString);
      delay(500);
      counter = counter + 1;
   }  
   // if the file isn't open, pop up an error:
   else {
      Serial.println("error opening datalog.txt");
   }
  //-------------------------------------- sleep mode activation ----------------------------
  // Stay awake for 0.5 second, then sleep.
  delay(500);
  digitalWrite(sdPower, LOW);
  delay(500);
  pwrDown(5);          // go to sleep for (x) sec.
}
} 

// some methods for sleep mode are not shown

1 个答案:

答案 0 :(得分:1)

经常使用的SD.h库无法管理断电的SD卡模块。断电后代码会抛出错误。该卡无法重新初始化。

我使用SdFat.h代替它,它的工作非常完美。 SD卡吸引了大量的电流。要切换SD卡模块,建议使用MOSFet。