I am trying to connect the LM35 temperature sensor to the pin A0, but whenever i use the analogRead() i get 1023 instead of the temperature value. can anyone help please?
Here is the code im using.
#include <LiquidCrystal.h>
LiquidCrystal lcd (8, 9, 4, 5, 6, 7);
float temp = 0;
void setup() {
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
lcd.begin (16, 2);
Serial.begin(9600);
}
void loop() {
digitalWrite(10,HIGH); //Turns on the Laser Pointer for accurate aiming
temp = analogRead(A0);
temp = (temp * 500) / 1023;
lcd.setCursor (0,0); // Display (first LCD line)
lcd.print ("Temp = ");
lcd.print (temp);
lcd.print ("");
lcd.write (0xDF); // Degree sign
lcd.print ("C");
Serial.println(temp);
if(temp < 37.5){//Turns on the RGB based on the temp range
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,LOW);}
else if(37.5 <= temp < 38.5){
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);}
else if (temp >= 38.5){
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);}
delay(1000);
lcd.clear();
}
I dont know if the connections are correct so here is a picture of the proteus simulation
I guess 1023 is a missing LM35 ground.
Check your wires/breadboard, or upload a 'real' picture of the setup.
You might want to change the LM35 part of the sketch after you have it working.
temp = (temp * 500) / 1023; // is potentially poor/unstable/inaccurate code
Leo..
Have you measured the Vin to your Arduino? The 1023 lends me to believe and maximum input to your ADC. You have checked the pinout of the LM35? The code math is fine from what I can see.
Connect +Vs pin to +5 volts, GND pin to ground, wait 5 minutes for handling heat to dissipate, read the voltage between Vout pin and ground with your voltmeter, should read room temperature times 10 milliVolts. If room temperature is 25 degrees C, you should read near 250 milliVolts. If not, you may have a counterfeit (fake).