https://www.ti.com/product/LMT86
Main changes are call analogReadResolution(12) in setup. We need all 12 bits.
I'm not too sure how to go about changing the ADC_SAMPLING_TIME, it seemed predefined.
Choosing a longer sampling time provides more stable values as these devices provides very little currents.
Code: Select all
#include <Arduino.h>
#include <math.h>
void sleep(uint16_t msecs);
void setup() {
Serial.begin();
pinMode(LED_BUILTIN, OUTPUT);
// we need all 12 bits
analogReadResolution(12);
pinMode(PA0, INPUT_ANALOG);
}
float f(float t, float v) {
return 1777.3F - (10.888*(t-30)) - 0.00347*(t-30.0F)*(t-30.0F) - v;
}
float df(float t) {
return -10.888*t - 0.00347*2*(t-30.0F);
}
float nstep(float t, float v) {
return t - f(t,v) / df(t);
}
#define TOLERANCE 0.005
float newton(float v) {
float t0 = 30.0F;
float t;
uint16_t n = 0;
while(true) {
t = nstep(t0, v);
n++;
if(fabs(t-t0) < TOLERANCE)
break;
t0 = t;
}
// Serial.print("loop ");
// Serial.println(n);
return t;
}
void loop() {
digitalToggle(LED_BUILTIN);
Serial.print("pa0:");
uint16_t val = analogRead(PA0);
Serial.print(val);
Serial.print(", ");
float mv = 3.3 * (float) val * 1000.0 / 4096.0;
Serial.print(mv);
Serial.print("mV, ");
float temp = newton(mv);
Serial.print(temp);
Serial.println(" deg C");
sleep(1000);
}
void sleep(uint16_t msecs) {
for(uint16_t i=0; i<=msecs; i++)
asm("wfi");
}
Not quite a library as Newton's method may not converge.
But otherwise, it is fast converting volts to temperature and works well so far. The key is that FPU in stm32f4, this'd likely run just as well on those without FPU, but it's likely slower.
Edit:
I tried defining in my makefile
-DADC_SAMPLINGTIME=ADC_SAMPLETIME_84CYCLES
This works, it changes the sample time, but a rebuild is needed (at least including the analog.o object).
In Arduino IDE, the same can be achieved via minor edits in boards.txt or platform.txt.
Note that this codes works OK with the default sample time. i.e. a longer sample time may not be needed after all.
I'd guess due to the challenges of various different hardware, the core codes select a sample time with a set of defines.
That works well generally.