Now that we have some idea of the system architecture, it’s time to connect the thermocouple to the Arduino and see if it works.

From the datasheet, we see that it’s a pretty simple chip.  We connect the power to Vcc and Ground, the thermocouple to T+ and T-, and the SPI interface to the microcontroller.  Wait, what’s SPI?

SPI stands for Serial Peripheral Interface, a bus standard invented by Motorola.  It’s a serial protocol, and allows bidirectional communication between a master and an arbitrary number of slaves at a cost of 3 wires + 1 additional wire per slave.

The physical layer specifies TTL or CMOS driven wires, with a logic 1 being +3.3V or +5V, and logic 0 being 0V.

The Data Link layer gives us four wires, Slave Select (SS), which is active low, Serial Clock (SCK), which is configurable in any of four modes, Master Out Slave In (MOSI), and Master In Slave Out(MISO).  Data transmission can be Most Significant Bit(MSB) or LSB first.  The master initiates and controls all communication with the slaves.  It starts by bringing the SS line low for the appropriate slave, then shifting bits out synchronously with the clock pin, and simultaneously shifting bits back in.  Once the agreed-upon word size has been shifted, the master may shift another word, or bring SS high again, ending the transaction.

The four modes for the clock determine whether the clock is active low or active high, and whether the bit should be read at the leading or trailing edge of the clock pulse.  Most of the time the clock is active high, and the bit is shifted at the leading edge.  The MSB is sent first, and the word size is 8 bits.

Fortunately, most of that is handled for you by the SPI hardware built into the Arduino, and the SPI library that’s included.  I put together the following sketch, that just reads the temperature from the chip and displays it at the serial port:

#include <SPI.h>
const int SS = 10;
unsigned short buf[8] = {0};
byte index = 0;
unsigned long sum = 0;
const unsigned short bufsiz = sizeof(buf) / sizeof(unsigned short);
void setup() {
  pinMode(SS, OUTPUT);
  digitalWrite(SS, HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV16);  Serial.begin(115200);
}
void loop() {
  delay(200);
  digitalWrite(SS, LOW);
  unsigned short value = (SPI.transfer(0) << 8 ) + SPI.transfer(0);
  digitalWrite(SS, HIGH);
  unsigned short rdg = (value >> 3);
  sum -= buf[index];
  sum += rdg;
  buf[index++] = rdg;
  index &= (bufsiz - 1); 
  unsigned long avg = sum / bufsiz;
  Serial.println("<reading>");
  Serial.print("\t<millis>");
  Serial.print(millis());
  Serial.println("</millis>");
  Serial.print("\t<disconnected>");
  Serial.print((value & 4) >> 2);
  Serial.println("</disconnected>");
  Serial.print("\t<temp unit=\"celsius\">");
  double degc = avg / 4.0f;
  Serial.print(degc);
  Serial.println("</temp>");
  Serial.print("\t<temp unit=\"fahrenheit\">");
  Serial.print(degc * 9.0f / 5.0f + 32.0f);
  Serial.println("</temp>");
  Serial.println("</reading>");
  //Serial.println("F)");
}

Have a look at the video to see a live test with a blowtorch.

Next post: Controlling a fan.

References

Part 1

Video of the thermocouple in action

Advertisements