Problem using ADT7310 SPI temperature sensor with arduino ethernetshield

Hello,
I'm using the ADT4310 temperature sensor for a school project with the library discribed here: An Arduino Library for the ADT7310 SPI Temperature Sensor – The inability to follow simple instructions

And I'm usinging the following example:

/*************************************************

Arduino Sketch to demonstrate the ADT7310 library

Steven Cogswell
November 2010 

This uses the Hardware SPI port 

Arduino   ADT7310
pin 13    pin 1  (SCLK)
pin 12    pin 2  (DOUT)
pin 11    pin 3  (DIN)
pin 9     pin 4  (CS)

ADT7310's VDD is connected to Arduino 5v, and GND to arduino GND.  


Output from the serial line looks like this: 

ADT7310 Demo
Writing config:  done
reading Tcrit:  Read complete [0x4980]
Temperature: 24.547 C, [0xC46], [0b110001000110]
Temperature: 24.547 C, [0xC46], [0b110001000110]
Temperature: 24.547 C, [0xC46], [0b110001000110]
Temperature: 24.500 C, [0xC40], [0b110001000000]
Temperature: 24.562 C, [0xC48], [0b110001001000]

**************************************************/

#include <SPI.h>
#include <ADT7310.h>

#define SELPIN 9             //Selection Pin  - to chip pin 4 (CS-)
#define MUST_BE_OUTPUT 10    // This pin must be set to output for Hardware SPI to work, even if we don't use it. 

ADT7310 adt7310(SELPIN); 
unsigned int ADTcheck = 0; 

void setup()
{
	// Serial communication back to the user. 
	Serial.begin(9600);
	Serial.println("ADT7310 Demo"); 

	//set pin modes for ADT7310
	pinMode(SELPIN, OUTPUT); 
	pinMode(MUST_BE_OUTPUT,OUTPUT); 
        digitalWrite(MUST_BE_OUTPUT, HIGH);
       
	adt7310.init(); 
        adt7310.reset(); 

	Serial.print("Writing config: "); 
	adt7310.setmode(ADT7310_1FAULT | ADT7310_CT_POLARITY_LOW | ADT7310_INT_POLARITY_LOW | 
		ADT7310_INTCT_INTERRUPT | ADT7310_CONTINUOUS | ADT7310_16BIT); 
	Serial.println(" done"); 

	// On powerup, register 0x04 Tcrit should be value 0x4980
	Serial.print("reading Tcrit: "); 
	unsigned int value = adt7310.read(0x04, 16);
	Serial.print(" Read complete [0x");
	Serial.print(value,HEX);
	Serial.println("]");
	// Save this value for later to compare against to make sure communications still synchronized.  
	ADTcheck=value;     
	delay(240);   // First conversion takes ~ 240 ms 
}

void loop() {
	unsigned int value = 0; 
	unsigned long ok = 0; 
	float t = 0; 

	value=0;
	// Read the 16-bit temperature value from register 0x02
	value = adt7310.read(0x02, 16);
	// Convert to a floating point temperature in C
	t=adt7310.temperature(value,16); 

	Serial.print("Temperature: "); 
	Serial.print(t,5); 
	Serial.print(" C, [0x"); 
	Serial.print(value,HEX); 
	Serial.print("], [0b"); 
	Serial.print(value,BIN); 
	Serial.println("]"); 

	// Read Tcrit register 0x04 again, if the communications are still good
	// then should return the value read at the start, 0x4980. 
	value = adt7310.read(0x04, 16);
    if (ADTcheck != value) {
		Serial.print(" ERROR expect [0x"); 
		Serial.print(ADTcheck,HEX);
		Serial.print(" - 0b"); 
		Serial.print(ADTcheck,BIN); 
		Serial.print("] got [0x"); 
		Serial.print(value,HEX); 
		Serial.print(" - 0b"); 
		Serial.print(value,BIN); 
		Serial.println("] - reset"); 
		// No choice, reset chip to get communications back. 
		adt7310.reset(); 
		adt7310.setmode(ADT7310_1FAULT | ADT7310_CT_POLARITY_LOW | ADT7310_INT_POLARITY_LOW | 
			ADT7310_INTCT_INTERRUPT | ADT7310_CONTINUOUS | ADT7310_16BIT); 
		Serial.print(ok); 
		Serial.println(" measurements without error"); 
		WAIT_FOR_KEY(); 

		ok=0; 
    } else {
		ok=ok+1;
    }
    
    delay(1000);
    
}

// Just a little routine to wait for someone to press a key. 
void WAIT_FOR_KEY() {
    Serial.println("press a key to continue"); 
    while (!Serial.available());
      Serial.flush();
}

When I run this program without the ethernetshield it works just fine. But when I plug it in the ethernetshield the output are just random values.
Here are the libraries:

Can anybody help me?
Thanks

Include these lines in your setup() and try again:

pinMode(4, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(4, HIGH);
digitalWrite(10, HIGH);

To explain what it does: the Ethernet Shield includes two SPI devices, the WizNet5100 chip and the SD card. The SD card usually is not critical as long as there is no card installed, but if a card is in place, it reacts on SPI commands as long as pin 4 is LOW. The WizNet5100 chip reacts on SPI commands as long as pin 10 is LOW. If you do nothing, they might be in either state and you're never sure about which device is replying to your requests.

I fixed a part of it. You were right. That was one part of the problem. The other part was resetting the SPI settings of the ethernetshield. (SPI.setDataMode from SPI_MODE3 to SPI_MODE0 etc.).
Now I got it to work, but it's still losing bits and thus the data will not be very consistent and accurate. But I don't know why and what other settings I could be forgetting.

I changed the webclientcode to sent and receive data from a server. There's nothing wrong with the ethernet output. Just the SPI sensordata. (which is oke when I don't use a ethernetshield).

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen
 
 */

#include <SPI.h>
#include <Ethernet.h>
#include <ADT7310.h>
#define SELPIN 9
ADT7310 adt7310(SELPIN); 
unsigned int ADTcheck = 0;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x80, 0x23 }; //physical mac address
byte ip[] = { 192,168,1,101 }; // ip in lan assigned to arduino
byte myserver[] = { 192,168,1,102 }; 
EthernetClient client;

void setup() {
  digitalWrite(9,HIGH);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
    while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
    }

  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
 
  
  pinMode(SELPIN, OUTPUT); 
  digitalWrite(10,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);

  adt7310.init(); 
  adt7310.reset(); 

  Serial.print("Writing config: "); 
  adt7310.setmode(ADT7310_1FAULT | ADT7310_CT_POLARITY_LOW | ADT7310_INT_POLARITY_LOW | ADT7310_INTCT_INTERRUPT | ADT7310_CONTINUOUS | ADT7310_16BIT); 
  Serial.println(" done"); 

  //On powerup, register 0x04 Tcrit should be value 0x4980
  Serial.print("reading Tcrit: "); 
  unsigned int value = adt7310.read(0x04, 16);
  Serial.print(" Read complete [0x");
  Serial.print(value,HEX);
  Serial.println("]");
  // Save this value for later to compare against to make sure communications still synchronized.  
  ADTcheck=value;     
  delay(240);   // First conversion takes ~ 240 ms 
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  SPI.setBitOrder(MSBFIRST);

  unsigned long ok = 0; 
  float t = 0; 
  value=0;
  //Read the 16-bit temperature value from register 0x02
  value = adt7310.read(0x02, 16);
	// Convert to a floating point temperature in C
  t=adt7310.temperature(value,16); 
  value = adt7310.read(0x04, 16);
  ok=0; 
      //} else {
  ok=ok+1;
  Serial.print("Temperature: "); 
  Serial.print(t,3);      
  delay(10);
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  digitalWrite(9,HIGH);
  delay(10);

}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
  unsigned int value = 0;
  delay(5);
  sendGET();
  delay(5);
  sendTemp();
  delay(5);
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  SPI.setBitOrder(MSBFIRST);
  digitalWrite(10,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  delay(50);
  unsigned long ok = 0; 
  float t = 0; 
  value=0;
	// Read the 16-bit temperature value from register 0x02
  value = adt7310.read(0x02, 16);
	// Convert to a floating point temperature in C
  t=adt7310.temperature(value,16); 
  value = adt7310.read(0x04, 16);
  ok=0; 
  ok=ok+1;
  Serial.print("Temperature: "); 
  Serial.println(t,3);     
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  delay(2);
  // if the server's disconnected, stop the client:
}

Why are you setting pin 3 high ?

Check all the places where you are setting the CS signal for the various SPI devices.

I just forgot to delete it. I set it high because I was not sure which port was the cs for the SD-card. But nothing's connected to pin 3.

  digitalWrite(10,HIGH);
  digitalWrite(4,HIGH);

Don't you think that a pinMode() call for these pins is also necessary? Otherwise you just activate a very weak pull-up resistor which might not be enough to set the CS of the devices to inactive.

You are right.

pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(4,OUTPUT);

I've added that to the code but it didn't have any effect.(Or barely noticeable). Still receiving unaccurate values.
But I'm using the ethernetshield because I'm still waiting for an adafruit CC3000 breakoutboard which will be arriving in a few days. I hope that I won't have the same problems there. But it would've been nice to get the ethernet connection going.

Can you make a wiring diagram of how you connected everything? And if possible make photographs of your setup and post them here.

Are you using an Arduino UNO? Because your ADT7310 library cannot handle Arduinos with other pinouts.

I connected everything like in the blogpost: An Arduino Library for the ADT7310 SPI Temperature Sensor – The inability to follow simple instructions

I got it to work by removing the sd-card. Even though I set digital pin 4 to high and cast it as an output. It still kept messing with my temperature values. But once I removed the card from the sd-card-slot it works just fine. I still was having some other problem with random values being read out, but I solved that problem by doing a NOP to the sensor after I used the ethernetshield.

I connected everything like in the blogpost: An Arduino Library for the ADT7310 SPI Temperature Sensor – The inability to follow simple instructions

This is not possible because there they don't use an Ethernet Shield. I asked for your wiring and not the guide after which you think you've done it. Make photos of your setup and post them. Take care that all connections are visible on the photos.

And I ask again: Are you using an UNO?

Can you check if your pin 4 is working (remove the ethernet shield, then load a sketch that is switching pin 4 HIGH and measure the pins voltage, post the results)?