下手の横好きのスクラップブック!!

下手の横好きの、いろいろな記録です。
電子工作・PIC・Arduino・太陽光発電・写真などetc

Arduinoでデータロガーを作る

2011-02-14 | Arduino

Arduinoの統合開発環境である Arduino IDEは相当速いペースでバージョンアップされており、最新のものはArduino-0022になっております。

Arduino-0022をダウンロードして中身をみておりましたら、librariesに「SD」が追加されおり更に「Examples]に「Datalogger」のスケッチがありましたので、早速試してみました。

スケッチ

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors
 to an SD card using the SD library.
  
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10
 
 created  24 Nov 2010
 updated 2 Dec 2010
 by Tom Igoe
 
 This example code is in the public domain.
  
 */

#include

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 4; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 3) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

 

SDカードのピン番号に関連する部分とデータ入力するピン数を変更しております。


SDカード

動作電圧は 2.7v~3.6v
 Arduinoから3.3vが出力されていますので、vccにはそれをそのまま使います。
 信号ラインも同じ3.3vにしなければなりませんが、今回は簡単な抵抗分圧で接続しました。
 SDカードのピン配列とピン間隔は変則的でカードコネクタを直接ブレッドボードに接続は不能です。
 ブレッドボードへ装着するために、以前に購入してあったサンハヤト社製のメモリーカード変換基板「SDメモリーカード用」を使用しました。
 
 SDカード端子
 9  --
 1  CS
  2  DI
  3  VSS
  4  VDD
  5  SCLK
  6  VSS
  7  DO
  8  --

配線
SDカード部分

センサー部分
データ収集数を4個として、温度および湿度センサーを各2個取り付けました。
状況に応じて入力数は変更可能です。


動作確認
Arduino-0022を起動した後、OPEN→SD→Dataloggerでスケッチが表示できます。
内容を確認し、VerifyでコンパイルさらにUpload toI/O Boardで アップロードします。

SDカードを挿入しリセットをすると記録が始まります。Serial Monitorで内容を確認すると Initializing SD card.....card Initialized と記入され記録が始まります。
SDカードが挿入されていないと、Initializing SD card.....card or not present と表示されます。

SDカードに記録されたデータ

エクセルに外部データとして取り込みグラフ化

 

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする