Adafruit MCP9808 Temperature Sensor

Hi there. I’m trying to use three MCP9808 sensors on one Arduino Uno. I’ve been able to get one sensor to work with the sample code Adafuit provided, but I keep getting errors when I try to use more than one sensor at a time. I set the sensor addresses to A0, A1, or A2 for simplicity sake. I’m still very new at coding and I’m having difficulty defining the sensors correctly in the loop so that they can be read. Has anyone used more than one of these sensors at a time on Arduino? Any suggestions?

Working test code for one sensor:
#include <Wire.h>
#include “Adafruit_MCP9808.h”
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
void setup() {
Serial.begin(9600);
Serial.println(“MCP9808 demo”);
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);
Serial.println("Shutdown MCP9808… ");
tempsensor.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere
delay(2000);
Serial.println("wake up MCP9808… "); // wake up MSP9808 - power consumption ~200 mikro Ampere
tempsensor.shutdown_wake(0); }

Test code for three sensors:
#include <Wire.h>
// the file Adafruit_MCP9808.h is in long form here so you can see changes I made
#if ARDUINO >= 100
#include “Arduino.h”
#else
#include “WProgram.h”
#endif
#ifdef AVR_ATtiny85
#include “TinyWireM.h”
#define Wire TinyWireM
#else
#include <Wire.h>
#endif
#define MCP9808_I2CADDR_DEFAULT 0x18
#define MCP9808_REG_CONFIG 0x01
#define MCP9808_I2CADDR_Sensor1 0x19
#define MCP9808_I2CADDR_Sensor2 0x1A
#define MCP9808_I2CADDR_Sensor3 0x1C

#define MCP9808_REG_CONFIG_SHUTDOWN 0x0100
#define MCP9808_REG_CONFIG_CRITLOCKED 0x0080
#define MCP9808_REG_CONFIG_WINLOCKED 0x0040
#define MCP9808_REG_CONFIG_INTCLR 0x0020
#define MCP9808_REG_CONFIG_ALERTSTAT 0x0010
#define MCP9808_REG_CONFIG_ALERTCTRL 0x0008
#define MCP9808_REG_CONFIG_ALERTSEL 0x0004
#define MCP9808_REG_CONFIG_ALERTPOL 0x0002
#define MCP9808_REG_CONFIG_ALERTMODE 0x0001

#define MCP9808_REG_UPPER_TEMP 0x02
#define MCP9808_REG_LOWER_TEMP 0x03
#define MCP9808_REG_CRIT_TEMP 0x04
#define MCP9808_REG_AMBIENT_TEMP 0x05
#define MCP9808_REG_MANUF_ID 0x06
#define MCP9808_REG_DEVICE_ID 0x07

class Adafruit_MCP9808_1 {
public:
Adafruit_MCP9808_1();
boolean begin(uint8_t a = MCP9808_I2CADDR_Sensor1);
float readTempF( void );
float readTempC( void );
int shutdown_wake( uint8_t sw_ID );

void write16(uint8_t reg, uint16_t val);
uint16_t read16(uint8_t reg);

private:

uint8_t _i2caddr;
};
class Adafruit_MCP9808_2 {
public:
Adafruit_MCP9808_2();
boolean begin(uint8_t a = MCP9808_I2CADDR_Sensor2);
float readTempF( void );
float readTempC( void );
int shutdown_wake( uint8_t sw_ID );

void write16(uint8_t reg, uint16_t val);
uint16_t read16(uint8_t reg);

private:

uint8_t _i2caddr;
};
class Adafruit_MCP9808_3 {
public:
Adafruit_MCP9808_3();
boolean begin(uint8_t a = MCP9808_I2CADDR_Sensor1);
float readTempF( void );
float readTempC( void );
int shutdown_wake( uint8_t sw_ID );

void write16(uint8_t reg, uint16_t val);
uint16_t read16(uint8_t reg);

private:

uint8_t _i2caddr;
};
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

void setup() {
Serial.begin(9600);
tempsensor.begin())
}

void loop() {
float c = tempsensor.readTempC();
Serial.print(“Temp: “); Serial.print(c); Serial.print(”*C\t”);
delay(2000);
}

Any help is appreciated. Thanks in advance!

Not sure, but the I2C interface may be corrupted by the use of the delay() function. I've have found a lot of cases where removing delay from my code got rid of flaky, unexplainable problems. When I need a delay function, I roll my own by creating a 2 Khz timer, and the associated timer interrupt just advances a counter by 1 tick. Then create a wait() function that just reads the clock, does nothing at all until the clock reaches a specified later time, then returns. This has never interfered with any of my other time-critical actions.

You do NOT need three classes. You need three instances of ONE class.