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
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.
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
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.... :|
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.Interesting, thanks for the tip.