# GetMAX6675.py. A pyhton routine to take 5 samples and # return an average of a K type thermocouple Connected # to a MAX6675 Cold junction compensated K-thermocouple to digital converter. # Samples are 170 ms arart, therefore rourine is approx # 1 second. If thermocouple is open circuit, the word # "ALARM" is returned. # MAX6675 returns temperature in Celsius in 0.25 steps import spidev import time import sys spi = spidev.SpiDev(0,0) #open SPI port 0, spidev controlls chip enable spi.max_speed_hz = 1000 #select a slow speed. samples = 5 #set routine for 5 samples sample_delay = .170 #set samle delay to 170 ms temperature_total = 0 #reset variable temperature_out = 0 #reset variable alarm = 0 #reset variable for count in range(samples): bytes_in = spi.readbytes(2) #read 2 bytes form MAX6675 if (bytes_in[1] & (1<<2)): #test for open cct thermocouple alarm = 1 #set alarm bit, exit loop if true break else: word_in = ((bytes_in[0]<<8) + bytes_in[1]) #2 bytes to word word_shift = ((word_in >> 3) * .25) # shift word then * 0.25 temperature_total = word_shift + temperature_total #add total samples time.sleep(sample_delay) #delay to allow MAZ6675 to convert new sample if (alarm): #test form alarm bit print ("ALARM") #return word "ALARM" if set else: #if alarm not set, return averaged temperature temperature_out = temperature_total / samples print (temperature_out) spi.close() #close SPI