## Reading LM19 analog temperature sensor

Hi,

So I am currently working on a STM32L072CZ board with a LM19 temperature sensor on analog pin A0.

I am a little confused about how to convert the output voltage in temperature. In the LM19's datasheet :

Vout = −11.69 mV/°C × T + 1.8663 V

So in my main.cpp file, I tried with that :

```AnalogIn LM19(A0);
float voltage;
float temperature;
temperature = (1.8663 - 3.3*voltage)/0.01169;

```

But it prints values around 23°C and it's pretty unstable ( it's around 25°C in the room, so 2-3°C are missing).

Do you think that I can adjust unstable values with a resistor? I also tried to adjust unstable values with :

```temperature = 38-27.5*voltage;

```

that gives me better results.

Rather than averaging 100 values, i prefer the root mean square of 100 values in my code. So i can prevent fluctuations in readings in the form of saw tooth

posted by Kamil M 11 Apr 2019

##### 1 year, 1 month ago.

Check the voltage on AVDD, connector CN1 and use that value as your '3.3' voltage. It may read 3.2875v so you will need to use 3.2875* voltage. It can make quite a difference.

Also Analogue measurements can be 'erratic' with MCU's depending on power stability from regulators and MCU structure.

One way to get round this is to take 100 readings in a loop and take the average, you will find the temperature will then be more stable.

I use something like this:

```
float ztemperature=0;
while(n<100){
temperature = (1.8663 - 3.3*voltage)/0.01169;
ztemperature = ztemperature + temperature;
n++;
}
temperature = (ztemperature/100);

```

And... you will need to have both temperature sensors very close (2-3cm maximum) to make a comparison, but they will always read slightly different depending on the accuracy of the devices.

From data sheet:

Accuracy at +30°C ±2.5 °C (max)

Accuracy at +130°C & −55°C ±3.5 to ±3.8 °C (max)

Ok thanks for your reply, I did with the 100 samples and it gives much more good results (24.9°C now)

I read a lot about how reducing analog input noises (including about sampling and computing an average)

I also have one more question :

For now I am prototyping on a breadboard so wires may add noises to the signal too. But for at the end, everything will be soldered on a prototype shield. Do you think that it will reduce the unstability?

Best regards

posted by elisa scheer 09 Apr 2019

Getting rid of the wires will help. As will making sure you have a good solid ground connection. If you want to push it to the limits then some very careful board layout can make a huge difference.

Adding a small capacitor as close as possible to the analog input pin also helps. You can even use an RC network to give you a basic low pass filter on the input to further reduce noise, just don't make the R too big or it could impact the reading.

Also on the software filtering side rather than taking the average of 100 readings I find you can normally get equally good results by taking a smaller number, say 5, discarding the highest and lowest values and then averaging the rest.

With a straight average you need to average lots to eliminate the effects you get from the occasional reading that's significantly wrong for some reason. By getting rid of these most extreme readings you don't include the bad points in the average and so can average less.

posted by Andy A 09 Apr 2019