Page 1 of 3

Read MAX6675 with ESP8266

Posted: Sat May 22, 2021 3:25 pm
by mezjoc
Hi there.
I bought from Ebay:
MAX6675 K-TypThermoelement Cable Temperature sensor Fühler Arduino Raspberry Pi.
The central IC is MAX6675. He works with SPI communication. How can I read the sensor temperature in basic language using NodeMCU?
Thank you: mezjoc

Re: Read MAX6675 with ESP8266

Posted: Sat May 22, 2021 4:36 pm
by mezjoc
Wiring Diagram.
Is it appropriate?

Re: Read MAX6675 with ESP8266

Posted: Sat May 22, 2021 4:43 pm
by mezjoc
This is the corresponding wiring diagram.
Image

Re: Read MAX6675 with ESP8266

Posted: Sun May 23, 2021 9:47 pm
by mezjoc

Code: [Local Link Removed for Guests]

'/program/spi_max6675.bas
spi.setup 1000000,1,1    'b/s, data_mode(0-1-2-3), lsb_first(0)-msb_first(1)

'MAX6675/SO-->GPIO12/D6
'MAX6675/CS-->GPIO16/D0
'MAX6675/SCK-->GPIO14/D5
'POWER-->+3V3-GND

pin.mode 16, output ' CS-->GPIO16
timer0 1000,mytimer
wait

mytimer:
i=i+1
pin(16) = 0    'CS=LOW
wlog i;".",spi.byte(0)      'spi.byte(0)=0...4095
pin(16) = 1    'CS=HIGH
return

Re: Read MAX6675 with ESP8266

Posted: Sun May 23, 2021 10:07 pm
by Electroguard
Well done... and you made it look easy in the end.

Re: Read MAX6675 with ESP8266

Posted: Mon May 24, 2021 11:02 am
by mezjoc
Unfortunately, the program is not perfect.
At room temperature (+ 25 ° C) -> decimal 100 should be obtained. The result is decimal 6 !!! WELLER soldering iron (Num = 7) -> decimal 34. This is few.
I need help. Where is the mistake? :(

Re: Read MAX6675 with ESP8266

Posted: Mon May 24, 2021 11:33 am
by AndyGadget
Just a wild guess - Haven't had time to look at the code . . .
Is the device returning the result in the form you expect - BigEndian vs LittleEndian?

Re: Read MAX6675 with ESP8266

Posted: Mon May 24, 2021 12:15 pm
by Electroguard
I'm afraid you're the thermocouple expert here, pal!
But here's my thoughts, for what it's worth...

The only thing I can be sure about claimed specifications for Chinese manufactured components is that they are always massively exaggerated, and I consider myself fortunate if the item actually functions in a half-usable manner (I have bought a lot of junki.
I don't know what the working range of your item is supposed to be, but all devices can expect to be less accurate and non-linear towards the extremes of their range.

I have many different types of temperature sensors, but many give readings which are more than a degree out with the others.
Some sensors are so far out that I add or subtract an offset to make it read the same as the 'consensus', but this often drifts out with changing temperature ... eg: the offset might be correct at room temperatures, but gets progressively inaccurate as the temperature drops.

Back to your situation - If the sensor readings are incorrect, then either the readings are wrong, or they are being interpreted wrong.
Either way, assuming that the readings are reasonably linear it should be possible to adjust them to suit (eg: like converting between fahrenheit and centigrade) by applying an offset and multiplication factor.
And as it happens, there is an Annex function which might just help do the trick...

CONVERT.MAP
(number, fromLow, fromHigh, toLow, toHigh)
Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

Let me know if it works so I can send you my sensors for calibration!

Re: Read MAX6675 with ESP8266

Posted: Mon May 24, 2021 2:12 pm
by Electroguard
I could not get CONVERT.MAP to work on Annex 1.43.3, so I used some equivalent code.
In my case I was simply converting an esp32 ADC value from 0-4096 into an 8 bit result from 0-255, so I was using 0 instead of any offsets, whereas you'll need to adjust to suit the low temperature discrepancy.

fromMin=0: fromMax=4096: toMin=0: toMax=255
value = adc(34) 'my measured value I wish to normalise
result = ((value - fromMin) / (fromMax - fromMin) * (toMax - toMin)) + toMin

Re: Read MAX6675 with ESP8266

Posted: Mon May 24, 2021 3:51 pm
by cicciocb
Hi,
looking at the arduino code of the adafruit library,
it looks very easy to interface :

Code: [Local Link Removed for Guests]

/**************************************************************************/
/*!
    @brief  Read the Celsius temperature
    @returns Temperature in C or NAN on failure!
*/
/**************************************************************************/
float MAX6675::readCelsius(void) {

  uint16_t v;

  digitalWrite(cs, LOW);
  delayMicroseconds(10);

  v = spiread();
  v <<= 8;
  v |= spiread();

  digitalWrite(cs, HIGH);

  if (v & 0x4) {
    // uh oh, no thermocouple attached!
    return NAN;
    // return -100;
  }

  v >>= 3;

  return v * 0.25;
}
So, this simple code converted to annex gives :

Code: [Local Link Removed for Guests]

'/program/spi_max6675.bas
spi.setup 1000000,1,1    'b/s, data_mode(0-1-2-3), lsb_first(0)-msb_first(1)

'MAX6675/SO-->GPIO12/D6
'MAX6675/CS-->GPIO16/D0
'MAX6675/SCK-->GPIO14/D5
'POWER-->+3V3-GND

pin.mode 16, output ' CS-->GPIO16
timer0 1000,mytimer
wait

mytimer:
i=i+1
pin(16) = 0    'CS=LOW
v = spi.byte(0)
v = v << 8
v = v or spi.byte(0)
pin(16) = 1    'CS=HIGH
if (v and &h4) then
  wlog "no thermocouple attached!"
else
  v = v >> 3
  wlog "Temperature "; v * 0.25 ; "°C"
end if
return
Obviously I did not tested this code but it should work.

cicciocb