1

Currently I'm working with a MSP-EXP430F5529LP microcontroller and a LM35, because I need to built a thermometer. The results will show on a seven segment, 4 digit display. I'm using the following conversion, in order to show the temperature in Celsius:

voltage = (conversion * 3.3)/ 4095;
celsius = (((voltage - 0.5) * 100) / 10);

Problem is the reading of my own thermometer it's around 27°C and the reading I'm getting on the display it's around 53.4°C. I checked the voltage around the LM35 and the reading seems to be around 270mV, so I don't know what's wrong with the code.

I also tried with the following formula:

temp = (conversion*8.05); // 3.3/4096 it's around 8.05
tempC = (temp/10);

The previous one will give an acurrate reading at ambient temperature, but as I soon as I tried to drop it with an ice, it won't come down.

  • 2
    What are the types of the variables, int or floating point? Integer divide will just drop any decimals, like 5/10 is 0. – BoP May 04 '23 at 21:06
  • Try calibrating your sensor input using a range of known temperatures as established by a reference thermometer. Your conversion formulae shown above do not account for negative temperatures and therefore must be suspect. Your conversions also assume a linear sensor response across all temperature ranges, which may also not be true. – Jim Rogers May 04 '23 at 21:06
  • check the voltage out of the LM35 with a voltmeter and compare against the datasheet's volts/degree spec – OldProgrammer May 04 '23 at 21:28
  • 1
    @BoP: Every subexpression shown except for possibly `temp/10` contains at least one floating-point operand, so there is no integer arithmetic in the code shown except the last statement. And that one will introduce error of no more than the final unit (e.g., 1 ºC). – Eric Postpischil May 04 '23 at 21:30
  • What does the documentation specify for the value in `conversion`? Nobody can know what function maps `conversion` to a temperature without knowing what the value in `conversion` means. – Eric Postpischil May 04 '23 at 21:32
  • I'm using float points, instead of int. I also checked the voltage already and the output seems to be working accordingly. Also, the conversion means the analog input, which is being storaged into the memory of the microcontroller. – Alexa Martínez May 04 '23 at 21:53
  • only use float in an mcu when absoutely necessary or if the mcu has an fpu (rare). in general it should be avoided at all costs. does not appear to be needed here. (division is something you want to avoid as well on some architectures, and in this case a divide is not needed). – old_timer May 05 '23 at 19:13

1 Answers1

4

From looking at the datasheet for the LM35, it seems that it outputs a voltage that scales as 10mV per degree Centigrade.

VOUT = 1500 mV at 150°C VOUT = 250 mV at 25°C VOUT = –550 mV at –55°C

This includes outputting a negative voltage if the temperature is below 0C. I am going to assume that you don't care about that, since I'm sure the ADC on your micro can't measure negative voltages.

I'm also assuming that the ADC on the micro has 12-bit resolution, based on your attempt to divide by 4095. So I'd guess that an input voltage of 3300mV on your ADC pin would read as 4095, and 0V on the input reads as zero.

Therefore, the formula you require is:

celsius = ((conversion * 3300) / 4095) / 10;

This can be simplified, by both absorbing the / 10 into the multiplication, and dividing by 4096, which is just a bit-shift on most CPUs. This gives:

celsius = (conversion * 330) / 4096;

Note that this can all be done with integer arithmetic if you're happy not to have a fractional result.

pmacfarlane
  • 3,057
  • 1
  • 7
  • 24