Help with LM34 Temperature Sensor

Hello! I am trying to display temperature and change fan speed based on the temperature read. I am following a tutorial from Science Fun on youtube. The code is as follows:

float temperature;
const int fan_control_pin = 3;
int count = 0;
unsigned long start_time;
int rpm;
void setup() {
pinMode(fan_control_pin, OUTPUT);
analogWrite(fan_control_pin, 0);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2), counter, RISING);
}
void loop() {
temperature = float(analogRead(A10)) * 500.0 / 1024.0;

if (temperature < 100.0) analogWrite(fan_control_pin, 0);
if ((temperature >= 100.0) && (temperature < 120.0)) analogWrite(fan_control_pin, 126);
if (temperature >= 120.0) analogWrite(fan_control_pin, 255);
start_time = millis();
count = 0;

while((millis() - start_time) < 1000){
}
rpm = count * 60 / 2;
Serial.print("The temperature is ");
Serial.print(temperature);
Serial.print((char)176);
Serial.println("F");
Serial.print("The fan speed is ");
Serial.print(rpm);
Serial.println(" rpm");
}
void counter() {
count++;
}

The problem is that it is not displaying the correct temperature on the terminal. I believe I have it wired correctly, it is a simple circuit and I had it checked over. Also, I have swapped the temperature sensors to make sure I am not using broken parts.

I am using an Arduino Due and analog pin A10 because my other pins are being used right now for a larger project.

Here is the terminal output:

Any help will be appreciated, thank you.

temperature = float(analogRead(A10)) * 500.0 / 1024.0;

Pretty sure you wanna be looking at A0, not A10.

HTH

a7

Hello spancari01
Add some function descriptive comments to the sketch for better understanding of the coding.
Have a nice day and enjoy coding in C++.

Is '176' supposed to be a "degree sign" ?
Not supported.

Does it matter which analog input I use? I figured since they are all analog inputs they would be the same but I could be wrong. I am connecting multiple sensors to this Arduino so if the A0 is already taken what should I do?

Yes sorry, forgot to change that

  1. 500.00 is for a 5volt processor, and you're using a 3.3volt-logic Due. Try 330.0
  2. analogRead returns an int, not a float. Remove 'float' from that line.
  3. readout of an LM34 with default Aref also depends on MCU supply. The tutorial/code does not compensate for that.
    Leo..

I believe the LM34 needs a minimum 5V supply.
https://www.ti.com/lit/ds/symlink/lm34.pdf

Sry, didn't pay attention!

a7

No problem! Thanks for the help

Thank you, I will try this soon.
But should I do 330 is I am supplying 5V to the sensor, not 3.3V or should I change it to supply 3.3V?

Minimum supply voltage of the LM34 is 4volt, so you should power it from 5volt.
You still can connect it's output to a 3.3volt-logic analogue pin of the Due,
because the sensor will normally output a voltage much lower than 3.3volt.
e.g. ~1volt at body temp (37C,100F).

330 (330.0 actually) is for calculating the temp (with 3.3volt default Aref of a 3.3volt processor).
1024 in that line is for the default 10-bit A/D of the Due.
Leo..

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