I have a problem with my lm 35 temperature sensor where I only get readings of zero celsius despite connecting the circuit right
My code is
//Temperature Controlled Fan using Arduino and Lm35 Code
//by Circuirdigest
const int lm35_pin = A0; // LM35 output pin
const int relay_pin = 2; // Relay control pin (change to the appropriate pin)
void setup() {
Serial.begin(9600);
pinMode(relay_pin, OUTPUT); // Set the relay pin as an output
digitalWrite(relay_pin, LOW); // Turn off the relay (fan)
}
void loop() {
int temp_adc_val;
float temp_val;
temp_adc_val = analogRead(lm35_pin); // Read temperature
temp_val = (temp_adc_val * 4.88); // Convert ADC value to equivalent voltage
temp_val = (temp_val / 10); // LM35 gives an output of 10mV/°C
Serial.print("Temperature = ");
Serial.print(temp_val);
Serial.println(" Degree Celsius");
if (temp_val < 40) {
Serial.println("Fan off");
digitalWrite(relay_pin, LOW); // Turn off the relay (fan)
} else {
Serial.println("Fan on");
digitalWrite(relay_pin, HIGH); // Turn on the relay (fan)
I suggest if you want some help you follow the forum guidelines and post your code accordingly using code tags. Since you have it connecting right post an annotated schematic of how you did it, be sure to include what Arduino you are using, they are different. This may help: Guidelines: How to get the best out of this forum
Right... Always "start simple" and make sure you are getting some kind of useful raw-reading that seems to track temperature.
Do you have a multimeter to read the voltage out of the LM35?
Do you have another LM35, and did you buy it from a reputable supplier? (There are rumors of fake ones.)
Another check would be to wire a potentiometer like the Analog Read Serial Example. You can try that example code and/or use the pot with your code to "fake out" some temperature measurements. If the pot works and the LM35 doesn't, the LM35 is bad or wired wrong. If the pot doesn't work you might have a bad Arduino (or something else).
...Once you get it working you can simplify your equations - There's no need to explicitly calculate voltage. The ADC reading is directly proportional to temperature (and to voltage, of course).