Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:56 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : K-Type Thermocouple help Please

     Page 1 of 2    
Author Message
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 06:59am 07 Apr 2018
Copy link to clipboard 
Print this post

Hi All,

I have struggled with trying to get a reading from a K-Type Thermocouple using a MAX31855 Chip. I know it will be something I am doing wrong and I have tried stripping out the relevant section of `working' code I got from Zonker but damned if I can get a good reading from my K-Types.

I know I am lousy at programming but I think all is right (except the actual calculation of Degrees Celsius as I am not 100% certain how to interpret that as I have seen conflicting methods on the net)

Anyway if someone can see what is wrong I would appreciate it greatly.

' Test MAX31855 read

CS = 15 ' Pin(15) for CS

SetPin CS, DOUT ' set CS to DData Out
Pin(15)=1 ' Disable CS
SPI OPEN 10000, 2 ,32 ' 100KHz Mode 1 32bits


Main:

GoSub ReadTemp
Print Bin$(RAW, 32) ' Print Raw 32 Bit read
TIP$=Left$(Bin$(RAW, 32),14) ' Print Thermocouple RAW TEMP read
internal$ = Mid$(Bin$(RAW, 32), 17, 12)
Print TIP$;" ";Internal$ '
Print Val("&B"+TIP$), Val("&B"+Internal$) ' Print in numbers
C=Val("&B"+TIP$)/4-Val("&B"+Internal$)
Print "C= ";c;"Celsius"
Print

Pause 2000

GoTo main



ReadTemp:
Pin(CS)=0 ' ENABLE CS
Pause 1 ' allow time to settle
RAW=SPI(0) ' read in 32 bits
Pause 1 ' allow time to settle
Pin(CS)=1 ' DISABLE CS
Return


Here is what I get returned from approx 18deg room temp. (the 2nd Row shows the Thermocouple TIP 14 bits and the Internal `cold' junction 12 bit temperature both in binary)




If I warm up the thermocouple it does change but non-sensibly, The fault bit is always 0 unless I short the thermocouple to GND then I can see it change.

Anyway I would appreciate if someone can see what is wrong or provide a snippet of complete working code for me to try on a 28 pin MM.

I have now tried 3 different Max PCBs and about 5 thermocouples of various shapes but all are K-Type.

Kind Regards,

Mick


EDIT**

I have tried swapping the leads to the Max Thermocouple connector and even tried 5v (yeah that may have killed one of my Max's)

Mick

Edited by bigmik 2018-04-08
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:21am 07 Apr 2018
Copy link to clipboard 
Print this post

What sort of variable is RAW ?
ie Float or Integer
I think if you are looking at Bin$(RAW, 32), it will need to be an Integer.

Jim
VK7JH
MMedit
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 07:25am 07 Apr 2018
Copy link to clipboard 
Print this post

Hi Jim,

Maybe that is my problem.. It is the raw 32 bits of data read from the MAX chip.. Maybe it needs to be defined as a 32bit number (not sure how to do this off hand).. I have no defines in my code..


Kind Regards,

Mick

Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
goc30

Guru

Joined: 12/04/2017
Location: France
Posts: 435
Posted: 08:18am 07 Apr 2018
Copy link to clipboard 
Print this post

hi
for me your pb is in SPI com. may be speed or timing of Chip Select. You don't have default bits, just default value
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 08:45am 07 Apr 2018
Copy link to clipboard 
Print this post

Dunno if this helps Mick,

Think from memory mine were MAX6675?
Similar but just a few more bits.

It was Geoff that pointed me in this direction.

  Quote  Do

PIN(51) = 1 ' set the pin to inactive
SETPIN 51, DOUT ' pin 10 is the enable signal
SPI OPEN 100000, 2, 16 ' speed is 100KHz, data size is 16 bits
PIN(51) = 0 ' assert the enable line (active low)
d = SPI(0) ' get the whole 16 bits
PIN(51) = 1 ' deselect the chip
SPI CLOSE ' close the SPI channel

Print Chr$(27)+"[f";
Print "Data Returned = ";Bin$(d,16)
Print

Print "Thermocouple reading =";Bin$(d>>3,16)+" "d>>3
Print " "

Print "Temperature =" (d>>3)/4 " degrees C"
Print " ";Bin$(d>>5,16)

Pause 1000
Loop




Phil.

Edit:-

Geoff did make a change on his original suggestion due to something a little obscure in the data sheets..

Still one of those get to it things....
Having the damper on the wood fire automated.

Along with the chopping & stoking.

Edit again..

Might have been mode 1 Vs 2.
Mine worked with mode 2 like you have.Edited by Phil23 2018-04-08
 
isochronic
Guru

Joined: 21/01/2012
Location: Australia
Posts: 689
Posted: 10:22am 07 Apr 2018
Copy link to clipboard 
Print this post

[don't know if it alters anything - the speed setup is 10k but commented 100k? ]

[edit] the 1 in the msb implies a negative ? I am wondering if the mode is right, the string of 0's look like a miscomm to me

PS if you reverse the thermocouple leads does the MSB bit change ?Edited by chronic 2018-04-08
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 11:27am 07 Apr 2018
Copy link to clipboard 
Print this post

Hi all,

I have tried various speeds, hence 10k where comment says 100k. ..100k was my first attempt!i also tried 1M. I also tried all four modes of SPI operation.

I also tried without the delays on CS.. I added these as the data sheet mentions a delay (not near my pc at the moment).

I agree it appears to be miscommunication but I now feel that Jim is on the right track re variable type..

I don’t know much about variable types.. I need to do a bit of reading of the manual, maybe an undefined variable can’t hold a 32bit value..

Will try again with different variable types tomorrow..

MickEdited by bigmik 2018-04-08
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 11:12pm 07 Apr 2018
Copy link to clipboard 
Print this post

You probably haven't set the default variable type so any undeclared ones will and up as floats.

Try changing RAW to RAW%
That will make sure it's an integer which will make more sense when you do
Bin$(RAW%, 32)

Jim
VK7JH
MMedit
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 08:54pm 08 Apr 2018
Copy link to clipboard 
Print this post

Thanks Jim, All,

I changed RAW into RAW% and still no better, I have also re-visited the different Modes (0-3) for SPI and I get differing results but nothing sensible.. ie. the returned values fluctuate can wildly with no change in temperature..

At this stage I am thinking I might be better off going down the path of the AD595 chip.. seems much easier to implement (famous last words) or I might try the MAX6675 as there is some working code available and it is only 16bits..

Zonker sent me some code but it was for a MM+ and used Cfunctions which seems over the top for a `simple’ single SPI read..

I tell you seriously that if I had hair I would have pulled it out by now.

It appears that nobody else has used the MAX13855 with a MM using native SPI so just possibly there is some crazy incompatibility.. or more likely.... I am just plain stupid..

Regards,

Mick

PS.

All this is in aid of trying to create a solder oven for SMD work... I have all the bits but thought I would play with getting one going and here I have stalled at the first hurdle.

MikEdited by bigmik 2018-04-10
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:05am 09 Apr 2018
Copy link to clipboard 
Print this post

HI Mik
NOt sure if you've seen this or not but I found Arduino code for this

Maybe you or someone can convert it to MM Basic?

Link here
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 08:07am 09 Apr 2018
Copy link to clipboard 
Print this post

Hi Mick
below some old code I did for this NOT tested on new versions of MMBasic


''''''''''''''''''''''''''''''''''''''''
'Max 31855
Dim temp(4)

Start:
''''''''''''''''''''''''''''
'Spi Read 32 bits
SetPin 4, Dout
Pin(4) = 1
SPI Open 40000, 0, 8

Pin(4)=0
Temp(0) = SPI(0)
Temp(1) = SPI(0)
Temp(2) = SPI(0)
Temp(3) = SPI(0)
SPI CLose
Pin(4)=1

error = temp(3) And &B111 'Error check
If error <> 0 Then
Print "Thermocouple Error"
Pause 5000
GoTo Start
EndIf

TempMSB = Temp(0)*16
TempLSB = ShiftR(Temp(1),4)
TempDP = (ShiftR(Temp(1),2) And &B00000011) * .25

Temperature = TempMSB + TempLSB + TempDP

Print "Temperature ";Temperature

TempIntMSB = Temp(2)
TempIntLSB = ShiftR(Temp(3),4)
TempIntDP = TempintLSB * 0.0625
TempInt = TempIntMSB + TempIntDP

Print "Internal Temperature ";TempInt

GoTo Start

Function ShiftR(byte_in, shift)
If shift <1 Or Shift > 7 Then Error "Invalid shift value"
ShiftR = (byte_in \ 2 ^ shift)
End Function

 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 11:40pm 09 Apr 2018
Copy link to clipboard 
Print this post

Hi Jman,

Ok I tried your code but similarly I get incorrect readings (basically the same as my 32 bit SPI code)

I had to change your code to suit my CS of pin(15) and also you used a variable called ERROR which gave me a syntax error so I changed that to ERROR1 I also added a print routine to show what all four SPI reads are and a 2 second delay to see what I received.

Here is the result I see after those changes





Here is the code with my Alterations



''''''''''''''''''''''''''''''''''''''''
'Max 31855
Dim temp(4)

Start:
''''''''''''''''''''''''''''
'Spi Read 32 bits
SetPin 15, Dout
Pin(15) = 1
SPI Open 40000, 0, 8

Pin(15)=0
Temp(0) = SPI(0)
Temp(1) = SPI(0)
Temp(2) = SPI(0)
Temp(3) = SPI(0)
SPI CLose
Pin(15)=1
print bin$(Temp(0)); " ";bin$(Temp(1)); " ";bin$(Temp(2)); " ";bin$(Temp(3))

error1 = temp(3) And &B111 'Error check
If error1 <> 0 Then
Print "Thermocouple Error"
Pause 5000
GoTo Start
EndIf

TempMSB = Temp(0)*16
TempLSB = ShiftR(Temp(1),4)
TempDP = (ShiftR(Temp(1),2) And &B00000011) * .25

Temperature = TempMSB + TempLSB + TempDP

Print "Temperature ";Temperature
pause 2000

TempIntMSB = Temp(2)
TempIntLSB = ShiftR(Temp(3),4)
TempIntDP = TempintLSB * 0.0625
TempInt = TempIntMSB + TempIntDP

Print "Internal Temperature ";TempInt

GoTo Start

Function ShiftR(byte_in, shift)
If shift <1 Or Shift > 7 Then Error "Invalid shift value"
ShiftR = (byte_in \ 2 ^ shift)
End Function


Something is seriously wrong here.. It surely shouldnt be so difficult..

I tried changing all 4 modes of SPI (0-3) and whilst the results change they are still nonsense. I have tried 4 or 5 K-Types (2 different styles) that work with a cheap temp meter I bought and 3 different MAX31855 modules although they are all the same type (I had to cut the GND from the Thermocouple itself)..

I will have to go back to basics, I will remove the chip from the test board and breadboard the circuit as the Datasheet shows..

Thank you all for your help..

Kind Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 12:41am 10 Apr 2018
Copy link to clipboard 
Print this post

Hey Mick.

I know you already have a thermocouple and SPI thing-y, but have you considdered using an AD595 K-type Thermocouple Amplifier - US$5.80

They connect to your K-type, and output a precise analog voltage representing the temperature. The one in the ceramic DIL package is VERY stable and accurate.

You simply read the analog voltage, and you have your temperature. The AD594 does the J-type thermocouple if needed.

I have used the 595 a few times, and they are SO EASY to use, and you don't need to do ANY conversion or SPI coms at all. Just connect the 595 to an analog pin on your MM, and read the voltage.

PDF attached.2018-04-10_104059_AD594_595_Thermocouple_Amplifier.pdf
Smoke makes things work. When the smoke gets out, it stops!
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 10:16am 10 Apr 2018
Copy link to clipboard 
Print this post

Hi Grogster,

I have an AD595 somewhere from the days you were considering a SMD oven..

Where?? You have seen the pic of my desk... :)

Yes I know it would be easier but what is the fun in that?

I am curious to see why I cant get a simple read of a MAX31855 chip to work.. There has to be a reason..

I believe JMans code worked on an earlier version.. I could roll back a couple of decades and test my hardware..

@JMan, do you know what version would have been in vogue back then?

Kind Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 10:26pm 10 Apr 2018
Copy link to clipboard 
Print this post

Understood. I just wanted to throw that idea out there. I am impressed with your memory. That SMD oven project was aborted, and I did not expect anyone to remember that, as it was a few years ago now. You can buy SMD ovens on eBay or Aliexpress quite cheaply these days anyway. But I digress........
Smoke makes things work. When the smoke gets out, it stops!
 
isochronic
Guru

Joined: 21/01/2012
Location: Australia
Posts: 689
Posted: 08:06am 11 Apr 2018
Copy link to clipboard 
Print this post

  Quote  I had to cut the GND from the Thermocouple itself


The thermocouples I have seen, only have two connections, they would both be connected directly to the IC - unless there is a grounded shield of some sort (?)
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 09:55am 11 Apr 2018
Copy link to clipboard 
Print this post

@Mick

I pull out the unit i made and give it an update once done i will flick you the progress

Regards
Jman
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 12:06pm 11 Apr 2018
Copy link to clipboard 
Print this post

@Grogs,

Yes, I have one of the PCBs you made and all the parts including the oven sitting on a shelf.

Every time I see the oven I remember the project..

@Jman,

No probs, thanks..

@Chronic,,

The modules I bought are all the same I will post a link next time I am in front of a pc..

They had pin 1 and 2 shorted together on the pcb so I cut the track between them..

The circuit buzzed out to the data sheet sample cct.. but of course I may have missed something else

I might desolder everything and start from scratch..

Regards,

MickEdited by bigmik 2018-04-12
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 09:36am 14 Apr 2018
Copy link to clipboard 
Print this post

@ Mick

Ok found the unit it is using an MX150 running Version 4.05 (A blast from the past)
I have tested it with no thermocouple connected and the internal temperature returns meaningfull results. Once I find the thermocouple I will post the result here


Dim temp(4)

Start:
''''''''''''''''''''''''''''
'Spi Read 32 bits
WatchDog 5000
SetPin 4, Dout
Pin(4) = 1
SPI Open 40000, 0, 8

Pin(4)=0
Temp(0) = SPI(0)
Temp(1) = SPI(0)
Temp(2) = SPI(0)
Temp(3) = SPI(0)
SPI CLose
Pin(4)=1

err = temp(3) And &B111 'Error check
If err <> 0 Then
Print "Thermocouple Error"
Pause 1000
EndIf

TempMSB = Temp(0)*16
TempLSB = ShiftR(Temp(1),4)
TempDP = (ShiftR(Temp(1),2) And &B00000011) * .25

Temperature = TempMSB + TempLSB + TempDP

Print "Temperature ";Temperature

TempIntMSB = Temp(2)
TempIntLSB = ShiftR(Temp(3),4)
TempIntDP = TempintLSB * 0.0625
TempInt = TempIntMSB + TempIntDP

Print "Internal Temperature ";TempInt

GoTo Start


Regards
Jman
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 10:34am 14 Apr 2018
Copy link to clipboard 
Print this post

Thanks Jman,

I look forward to hearing if it works with a later version 170 28pinner

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025