Categories

MCP9808 temperature readings on a Digit Shield Example

In this example we want to display temperature readings from an MCP9808 temperature sensor on an Arduino Digit Shield. We have already looked at the shield and sensor in previous articles.

We have covered the digit shield in the following post

Arduino Digit Shield – link here

We have covered the MCP9808 sensor in the following post

MCP9808 temperature sensor – link here

 

Connection

The shield fits onto your Arduino UNO – so thats easy. The MCP9808 is an I2C device so its easy to connect to the shield header

 

Shield MCP9808
3v3 Vin
Gnd Gnd
A4 SDA
A5 SCl

 

Code

Basically you just have to tray and merge two examples together and you end up a working example, here is our code

#include <Wire.h>
#include "Adafruit_MCP9808.h"
#include <DigitShield.h>
 
// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
 
void setup()
{ 
  // initiliase the DigitShield
  DigitShield.begin();
  // Begin serial connection at 9600 baud for debug
  Serial.begin(9600);
 
  if (!tempsensor.begin()) 
  {
    Serial.println("Couldn't find MCP9808!");
    while (1);
  }
}
 
void loop()
{
  float c = tempsensor.readTempC();
  DigitShield.setPrecision(2);
  DigitShield.setValue(c);
  delay(250);
  // Wait a while
  tempsensor.shutdown_wake(1);
  delay(1000);
  tempsensor.shutdown_wake(0);
}

 

Video

This example and some of the default examples in the library joined together in this video

 

Links

Digit Shield 4 digit tube display module fast 5641 digital tube module

High Accuracy Temperature Sensor MCP9808 I2C Breakout Board Module 2.7V-5V Logic Voltage for Ardunio in Stock

Share

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

MCP9808 digital temperature sensor example

MCP9808 digital temperature sensor example

The MCP9808 digital temperature sensor converts temperatures between -20°C and +100°C to a digital word with ±0.5°C (max.) accuracy. The MCP9808 comes with user-programmable registers that provide flexibility for temperature sensing applications. The registers allow user-selectable settings such as Shutdown or low-power modes and the specification of temperature Event and Critical output boundaries. When the temperature changes beyond the specified boundary limits, the MCP9808 outputs an Event signal. The user has the option of setting the event output signal polarity as an active-low or active-high comparator output for thermostat operation, or as temperature event interrupt output for microprocessor-based systems. The event output can also be configured as a Critical temperature output. This sensor has an industry standard 2-wire, SMBus and Standard I2C™Compatible compatible (100kHz/400kHz bus clock) serial interface, allowing up to eight sensors to be controlled in a single serial bus.
Features

Accuracy:
±0.25°C (typical) from -40°C to +125°C
±0.5°C (maximum) from -20°C to +100°C

User Selectable Measurement Resolution:
0.5°C, 0.25°C, 0.125°C, 0.0625°C

User Programmable Temperature Limits:
Temperature Window Limit
Critical Temperature Limit

User Programmable Temperature Alert Output
Operating Voltage Range: 2.7V to 5.5V

More details about the sensor at http://www.microchip.com/wwwproducts/en/MCP9808

This typically comes in a breakout such as the one in the breakout below

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"
 
// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
 
void setup() {
  Serial.begin(9600);
 
  if (!tempsensor.begin()) 
  {
    Serial.println("Couldn't find MCP9808!");
    while (1);
  }
}
 
void loop() {
  // Read and print out the temperature, then convert to *F
  float c = tempsensor.readTempC();
  float f = c * 9.0 / 5.0 + 32;
  Serial.print("Temp: "); 
  Serial.print(c); 
  Serial.print(" C\t"); 
  Serial.print(f); 
  Serial.println(" F");
  delay(250);
 
  tempsensor.shutdown_wake(1);
  delay(2000);
  tempsensor.shutdown_wake(0);
 
 
}

 

Results

Open the serial monitor and you should see something like this

Temp: 21.69 C 71.04 F
Temp: 21.62 C 70.93 F
Temp: 24.50 C 76.10 F
Temp: 25.06 C 77.11 F
Temp: 25.37 C 77.68 F
Temp: 25.56 C 78.01 F
Temp: 24.25 C 75.65 F
Temp: 23.87 C 74.97 F

 

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

Share