Categories

Logging MCP9808 readings to an SD card

In this simple project we will log out the temperature from our MCP9808 sensor and using a data logging shield we will log the readings to an SD card.

The sensor was mentioned in a previous post at http://arduinolearning.com/code/mcp9808-digital-temperature-sensor-example.php and also we looked at the data logging shield on http://arduinolearning.com/code/arduino-data-logger-shield-rtc-example.php /

Here is a reminder of the Arduino logging shield

data logging shield

data logging shield

You will need to connect the MCP9808 module to your data logging shield, here is what this module looks like

mcp9808 sensor

Vdd – Vcc (5v)
Gnd – Gnd
SCL – Arduino A5
SDA – Arduino A4

Parts List

1 x Arduino Uno
1 x Data logging shield
1 x MCP9808 module
1 x SD card (any size)
Connecting wires

Code

You will need the adafruit MCP9808 library which is available at https://github.com/adafruit/Adafruit_MCP9808_Library/archive/master.zip

#include <Wire.h>
#include "Adafruit_MCP9808.h"
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
File myFile;
const int chipSelect = 10;
 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  RTC.begin();
  //sd setup
  if (!SD.begin(chipSelect)) 
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization complete.");
  //temp sensor setup
  if (!tempsensor.begin()) 
  {
    Serial.println("Couldn't find MCP9808!");
    while (1);
  }
  //RTC setup
  if (! RTC.isrunning()) 
  {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}
 
void loop()
{
  float c = tempsensor.readTempC();
  float f = c * 9.0 / 5.0 + 32;
  DateTime now = RTC.now();
  //create a file
  File myFile = SD.open("mcp9808.txt", FILE_WRITE);
  //write to file
  if (myFile) 
  {
    Serial.print("Writing to mcp9808.txt...");
    //date and time section
    if(now.hour() <10)
    {
      myFile.print("0");
    }
    myFile.print(now.hour());
    myFile.print(":");
 
    if(now.minute() < 10)
    {
      myFile.print("0");
    }
    myFile.print(now.minute());
    myFile.print(":");
 
    if(now.second() < 10)
    {
       myFile.print("0");
    }
    myFile.print(now.second());
    myFile.print(",");
    //sensor section
    myFile.print(c);
    myFile.print(",");
    myFile.print(f);
    myFile.println();
    //close the file
    myFile.close();
    Serial.println("done.");
    tempsensor.shutdown_wake(1);
    delay(2000);
    tempsensor.shutdown_wake(0);
  } 
  else 
  {
    Serial.println("error opening mcp9808.txt file");
  }
  delay(1000);
}

 

Results

Open the serial monitor and you will see something like this

Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.
Writing to mcp9808.txt…done.

now switch of the arduino, remove the sd card and open the mcp9808.txt file. You should see something like the following logged

11:39:36,23.00,73.40
11:39:39,22.94,73.29
11:39:42,22.94,73.29
11:39:45,23.00,73.40
11:39:48,22.94,73.29
11:39:51,22.94,73.29
11:39:54,22.87,73.18

 

 

Links
Data Logger Module Logging Data Recorder Shield for Arduino UNO SD Card

Adafruit MCP9808 High Accuracy I2C Temperature Sensor Breakout Board [ADA1782]

Share

Comments are closed.