Measuring temperature with lm35


I’m trying to measure the temperature with a LM35 sensor. If the board is connected via USB, the temperature seems valid (near to 22°C). If the board is connected via an external power supply, the measured temperature is about 31°C.
Here’s my code:

void loop() {      
  float resultTemp = 0.0;
  for(int i = 0; i < cycles; i++) {
    int analogValue = analogRead(analogPin);
    float temperatur = (5.0 * 100.0 * analogValue) / 1024;
    resultTemp += temperatur;
  resultTemp /= cycles;

Where’s my mistake? The delivered voltage from the power supply is controllable and adjusted to 5V (same as it should be via USB).


Check the voltage actually supplied and adjust your algorithm to suit The voltage on the arduino is probably closer to 4.78V. try float temperature = (4.8 * 100.0 * analogValue) / 1024;

These arduino boards can be as much as 2 degrees C out

You may be able to calibrate the voltage levels by measuring the bandgap voltage between the Arduino internal AREF and the external voltage.

See,38119.0.html and,51708.0.html for further information. Keep in mind, you have to calibrate your bandgap voltage first to be sure it's accurate.

What exactly does your normalize function do?

The normalize function is only used to create an appropriate string for serial communication (no calculation, etc.). My next step will be to evaluate the bandgap voltage an hope that this solves my problem.