Max6675 - temp always reads zero without delay

I've got a Max6675 being read from during loop, without delay this always reads zero.

I've faked delay (logic, time passed since last check) since the rest of my system won't work without it (PID for heating element through SSR)

around a 200ms delay is required, is this normal for the max6675? it's on 3.3v if that makes a difference:

SO = 8; CS = 9; SCK = 10; GND and VCC (3.3v) connected.

in reality temp shouldn't change a huge amount in 200ms, but adjustments done as much as possible is pretty key to getting an accurate temp, so ideally there would be no delay.

I had the same delay on the temp logging as well since it was pointless logging temp if i didn't know current readings, but this caused an issue with the logging (stopped after 35 seconds) so this has since been removed - i couldn't work out why logging stopped.

Posted Jul 3, 2019, 8:26 PM UTC

111
triffid_hunter•

It states in the chip's datasheet that the sample rate is 4Hz (250ms), and if you read before it's ready, it cancels the current reading and starts a new one, so yes the chip is doing exactly what it's datasheet says.

Reading it at 5Hz (200ms) is technically overclocking, which is likely why you're still losing readings after a while ;)

No idea why everyone else is talking about SPI speed, that's not a problem at all.

2
NoPlayTimeOP•

I'm not particularly technical when it comes to electronics so I would have easily missed a detail like that, I will reduce read time on it to meet datasheet specs and try again, thanks

1
NoPlayTimeOP•

so i've added a delay of 250ms back in to my program and i've now got logging throughout my roast, which is great.

the problem now seems to be that after 32seconds (the previous point where logging would stop), there appears to be no temperature change logged - it just stays consistently at the same temp until roast is complete (even after cooling my most recent readings were 52c).

any idea why this might be? -I'm looking at the script now to check to ensure temp variable is still always being updated.

edit: I believe i may have found the source of the error, though not the exact cause at this point. I have a function that builds in the logic to determine if enough time has passed (250ms) to take a temp reading. it appears after ~32 seconds the logic that says "take a new reading" never becomes true, this was previously shared logic on within the code that determined if it was worth logging the data. so in essence this explains both issues i was facing.

I suspect heavily at this point that i'm simply doing something silly like logging the number of miliseconds since last log/temp read in a type that cannot go beyond a certain size.

edit 2: seems very likely, found an int where realistically i should have used an unsigned long... math works out about right too - max value of a 2 byte int would be 32,767 if this is milliseconds then it's 32 seconds.... :|

1
triffid_hunter•

Ah yep, sounds like you need to do your timing properly and take overflow into account..

Pro tip: use something like unsigned long nextTime = millis() + 250; ... if ((nextTime - millis()) & 0x80000000) { doStuff(); nextTime += 250; } which correctly handles overflow by detecting unsigned negative result of subtraction, rather than using < or > which don't handle overflow at all.

1
NoPlayTimeOP•

Interesting, thanks for the tip.

1
stockvu•

The chip appears to send data via SPI. That will take some amount of time depending on how fast your SPI is running (SPI clock rate setting in library?).

I understand your concern for the PID.

Perhaps you can set up a Timer to interrupt every so often and allow the 200 mSec needed for transfer (seems like a long time for 12 bits though). Would that be problematic if done every 30 seconds or so?

Or perhaps you need an interrupt driven way to clock in each bit using a bit-bang technique and build the 12 bit word up and then update a value in loop(). For each pass thru loop, you get one bit. After 12+ passes, you could update and start another conversion.

hth

1
NoPlayTimeOP•

Are there alternatives that don't rely so heavily on SPI? If I can avoid the delay in someway all together that seems the best approach

1
stockvu•

I believe you can use Arduino port pins to "bit-bang" the Max6675 interface.

As I said before, you could do this 1 bit-at-a-time. That is, 1 bit for each complete pass thorugh loop(). You would still do all the things you do now in loop() including PID.

Example: (NOT USING ARDUINO SPI BUS)

At the beginning of loop(), let's say you use some (bit-banged) port pin-states to fool the Max into giving you 1-bit over its SPI pins. (This should take only a few instructions). Your port-pins will clock that bit out at whatever speed is happening.

You shift that bit into a uInt32 variable you have allocated somewhere. You do this 12 times to shift in the complete 12 bits. That takes 12 complete passes thru loop().

On the 13th pass thru loop, you convert that 12 bit value into whatever form you need and store it for whatever you need it for.

This might use up 20-30 micro-Seconds of time.

On the next pass thru loop(), you could start this process again. Or you could wait a while.

Either way, you should be able to sneak the data out of the Max chip (1-bit-per-loop-pass at at time) with almost no effect on other things you're doing in loop(),

That's what I was trying to say...

hth

1
NoPlayTimeOP•

Thanks for the detailed explanation I'll read in to this more.

1
stockvu•

Max6675

Sorry, I messed up on this question.

The other user stating 250 mSec per conversion is sharper than I am. That explains everything. My bit-bang idea is useless for this problem.

I read the datasheet but missed the conversion-time. DOH...

1
[deleted]•

[deleted]

1