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.
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?
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..