Temperature sensor returns weird numbers

I am trying to do simple temp temperature tool as it is described in Project 3 (Arduino Starter Pack).

I set everything correctly but getting random number from 0 to 70. What can be wrong?

Please help :pray:

Integer division resulting in a value of 0.

Change float voltage = (sensorVal/1024)*5.0; to float voltage = (sensorVal/1024.0)*5.0;

And please, please, PLEASE don't use screen shots of code!

Many things, you did not follow the forum guidelines.

  1. Provide Us Clear Documentation: Since we can’t see your complete project, share an annotated schematic (best) or a clear drawing of your setup. Pictures are welcome, but avoid using Fritzing diagrams as they are wiring diagrams, not schematics, and are not ideal for troubleshooting.

  2. Include Technical Details: If there is specific hardware involved, include links to technical information. There are often many versions of similar components, so precise details are essential. What version are you using?

  3. Reference Resources: For additional help, check out useful links and tutorials: Useful Links on Arduino Forum. Post links to your hardware's technical information.

  4. Obtain a copy: of the Arduino Cookbook and complete several of the projects. Many of them will help you achieve parts of your goals.

  5. Course Electronics For Beginners (eBook)

  6. Electronics For Beginners (eBook)

  7. Arduino Step-by-step Projects Course | Build 25 Projects

You are right, there was a bug around my voltage formula. Thank you so much!

But I am still getting weird numbers. This is not my room temperatore:

Senzor Value: 95, Volts: 0.46, Temperatore: -3.61
Senzor Value: 98, Volts: 0.48, Temperatore: -2.15
Senzor Value: 98, Volts: 0.48, Temperatore: -2.15
Senzor Value: 98, Volts: 0.48, Temperatore: -2.15
Senzor Value: 97, Volts: 0.47, Temperatore: -2.64
Senzor Value: 97, Volts: 0.47, Temperatore: -2.64
Senzor Value: 98, Volts: 0.48, Temperatore: -2.15
Senzor Value: 95, Volts: 0.46, Temperatore: -3.61
Senzor Value: 96, Volts: 0.47, Temperatore: -3.13
Senzor Value: 109, Volts: 0.53, Temperatore: 3.22
Senzor Value: 109, Volts: 0.53, Temperatore: 3.22
Senzor Value: 110, Volts: 0.54, Temperatore: 3.71
Senzor Value: 86, Volts: 0.42, Temperatore: -8.01
Senzor Value: 103, Volts: 0.50, Temperatore: 0.29
Senzor Value: 101, Volts: 0.49, Temperatore: -0.68
Senzor Value: 103, Volts: 0.50, Temperatore: 0.29
Senzor Value: 103, Volts: 0.50, Temperatore: 0.29
Senzor Value: 110, Volts: 0.54, Temperatore: 3.71
Senzor Value: 111, Volts: 0.54, Temperatore: 4.20
Senzor Value: 112, Volts: 0.55, Temperatore: 4.69
Senzor Value: 114, Volts: 0.56, Temperatore: 5.66
Senzor Value: 110, Volts: 0.54, Temperatore: 3.71
Senzor Value: 107, Volts: 0.52, Temperatore: 2.25
Senzor Value: 106, Volts: 0.52, Temperatore: 1.76
Senzor Value: 106, Volts: 0.52, Temperatore: 1.76
Senzor Value: 111, Volts: 0.54, Temperatore: 4.20
Senzor Value: 110, Volts: 0.54, Temperatore: 3.71
Senzor Value: 111, Volts: 0.54, Temperatore: 4.20
Senzor Value: 113, Volts: 0.55, Temperatore: 5.18
Senzor Value: 113, Volts: 0.55, Temperatore: 5.18
Senzor Value: 111, Volts: 0.54, Temperatore: 4.20

This is full source code:

const int senzorPin = A0;
const float baselineTemp = 20.0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int senzorVal = analogRead(senzorPin);
  Serial.print("Senzor Value: ");
  Serial.print(senzorVal);

  // convert ADC reading to voltage
  float voltage = (senzorVal/1024.0)*5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);

  float temperature = (voltage - 0.5) * 100;
  Serial.print(", Temperatore: ");
  Serial.println(temperature);


  delay(1000);
}

That sensor should put out 0.5V at 0C, increasing by 0.01V for each degree C increase in temperature. So that fact that your readings are all clustering around 0.5V is troubling unless it's in a fridge.

Things to check:

  1. What do you have the ground and Vdd pins of the sensor hooked up to?
  2. Is Vout in fact connected to A0?
  3. If you put a meter on Vout, what voltage are you seeing?
  4. Also, check what voltage you're actually getting on the 5V line and using that value in your calculations rather than assuming 5.0V.

Which Arduino are you using and what, exactly, is the "Arduino Starter Pack"?

A web search does not turn up a product matching that exact name, so please post a link to it.

Yes, I am using Arduino Starter Pack and its components

I use the simplest connection as you can see on attached photos. Vout to A0.

You have the sensor's Vdd pin hooked up to I/O pin #10, which is not referenced in any way in your sketch. So it defaults to an input pin, which is not capable of supplying power to the sensor.

Perhaps try it with the sensor's Vdd pin connected to a 5V pin on the Arduino.

Hmm, you are right, again, thank you so much! Whenever I started using 5V analogue pin, I was getting right temperature value! :partying_face:

What is still weird is that when I tried that variant with #10 digital pin and activate it via:

pinMode(10,OUTPUT);
digitalWrite(10, HIGH);

so I still got wrong temperature values. It seems that this way thought digital pin does not work at all. What do you think?

Not sure what pin that is...

What values did you get? They might be slightly different to when you used (I assume) the 5V pin.

According ChatGPT, it seems that Digital pins are not designed for powering devices right? Digital pins can only provide a limited current (typically a maximum of 40 mA, but 20 mA is recommended for safe operation).

Voltage is not stable:
Even when you set a digital pin to HIGH, its output voltage is lower than 5V (usually around 4.2V–4.5V).
This lower voltage causes the sensor to operate outside its normal range, resulting in inaccurate readings.
Measurement interference: Any noise or higher current draw can cause voltage fluctuations, leading to incorrect sensor values.

Thanks for putting those words at the start of your reply. Saved some time for me. I stopped reading your post after those words.

Can you answer my questions please?

Everything chat told you in post #12 is wrong

When I use #10 digital pin to power up my circuit, I am getting higher numbers around +10C see:

Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 178, Volts: 0.87, Temperatore: 36.91
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 178, Volts: 0.87, Temperatore: 36.91
Senzor Value: 178, Volts: 0.87, Temperatore: 36.91
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 178, Volts: 0.87, Temperatore: 36.91
Senzor Value: 178, Volts: 0.87, Temperatore: 36.91
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 178, Volts: 0.87, Temperatore: 36.91
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 177, Volts: 0.86, Temperatore: 36.43
Senzor Value: 176, Volts: 0.86, Temperatore: 35.94
Senzor Value: 178, Volts: 0.87, Temperatore: 36.91
Senzor Value: 176, Volts: 0.86, Temperatore: 35.94

This is my updated source code:

const int senzorPin = A0;

void setup()
{
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  digitalWrite(10, HIGH);
}

void loop()
{
  int senzorVal = analogRead(senzorPin);
  Serial.print("Senzor Value: ");
  Serial.print(senzorVal);

  // convert ADC reading to voltage
  float voltage = (senzorVal/1024.0)*5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);

  float temperature = (voltage - 0.5) * 100;
  Serial.print(", Temperatore: ");
  Serial.println(temperature);


  delay(1000);
}

If I use 4.5 V instead of 5 V in your formula I get 27.7ºC for a sensor value of 177. Try measuring what the D10 High output voltage actually is and using that in your equation.

Never mind. That doesn't really make any sense since D10 is not the ADC reference and the sensor output is not proportional to Vdd.

1 Like

I don't know why powering the sensor with pin 10 is not giving the same result as using the 5V pin. The sensor needs a supply of between 2.3V and 5.5V. If it gets that, it should work and the readings should be the same for any input voltage.

The sensor's operating current is a few microamps. The Arduino pin could probably provide enough current for 1000 of these sensors at the same time.

Try connecting pin 10 to A0, instead of the sensor output. What voltage does it report? (Ignore the temperature reading, that will be 100C+).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.