Hello.
I have some problems with LM35 values. I have connected LM35 and common anode RGB LED to arduino. When I upload the program, it seems like the values of LM35 goes incorrectly when LED turns ON when the LM35 values is more then 35 degree Celsius.
For example, temperature now is 32 degree Celsius and LED is OFF. When temperature is more than 35 degree Celsius, LED is ON but the temperature goes high and high. It hardly to get low temperature back.
What is the problems and what should I do?
....and what should I do?
You should post your code and schematic so we can see the details of your project
This my code
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int lcd_backlight = 6;
int tempPin = A0;
int rled = 8;
int bled = 7;
int temp;
void setup()
{
pinMode(lcd_backlight, OUTPUT);
digitalWrite(lcd_backlight, HIGH);
lcd.begin(16,2);
pinMode(tempPin, INPUT);
pinMode(rled, OUTPUT);
pinMode(bled,OUTPUT);
}
void loop()
{
int voltage = analogRead(tempPin);
float milivolts = (voltage/1024.0) * 5.0;
int temp = milivolts * 100;
if(temp > 35)
{
setColor(HIGH, LOW, LOW); // red
}
else
{
setColor(LOW, LOW, LOW); // OFF
}
// Display the temperature
lcd.setCursor(0,0);
lcd.print("TEMP: ");
lcd.setCursor(6,0);
lcd.print(temp);
lcd.print((char)223);
lcd.print("C");
delay(1000);
}
void setColor(int red, int green, int blue)
{
digitalWrite(rled, red);
}
what you could do is to average 4, 8 or 16 readings (powers of 2 gives best performance)
void loop()
{
float raw = 0;
for (int i=0; i< 16; i++) raw = raw + analogRead(tempPin);
raw = raw / 16;
float milivolts = (raw /1024.0) * 500;
float temp = milivolts * 100;
give it a try
The problem is well known:
When the leds light current flows through the ground wire.
As the wire resistor can not be zero there is a small voltage drop which modifies the measure (LM35 = 10mV per Celsius degree)
The solution is simple and is only hardware :
Use a ground wire for the LM35 and another one for the leds.
The two wire being connected in star mode only on the arduino board.
Use a ground wire for the LM35 and another one for the leds.
The two wire being connected in star mode only on the arduino board.
I dont understand. Could you show me with schematic diagram?
Thanks.
I have wiring it, but a same problem happens. But this time I use LCD. When I not use LCD, the same happens too.
I also add Fan. But now temperature change every time. Not normal change, but weird change. For example, now temperature is 34, but it change until 40 then return to 34 back and it always like that. Sometime it increases, but still return to 34. This is my code.
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int fan = 9;
int tempPin = A2;
int rled = 8;
int bled = 7;
int temp;
void setup()
{
lcd.begin(16,2);
pinMode(tempPin, INPUT);
pinMode(fan, OUTPUT);
pinMode(rled, OUTPUT);
pinMode(bled,OUTPUT);
}
void loop()
{
// Formula to change from volts to celcius
int reading = analogRead(tempPin);
float voltage = reading * 5.0;
voltage = voltage/1024.0;
int temp = voltage * 100;
if(temp >= 50)
{
analogWrite(fan, 255); // Fan speed at full speed
setColor(HIGH, LOW, LOW); // red
}
else
{
analogWrite(fan, 127); // Fan speed at half of fan speed
setColor(LOW, LOW, HIGH); // blue
}
// Display the temperature
lcd.setCursor(0,0);
lcd.print("TEMP:");
//lcd.setCursor(6,0);
lcd.print(temp);
lcd.print((char)223);
lcd.print("C");
delay(1000);
}
void setColor(int red, int green, int blue)
{
digitalWrite(rled, red);
digitalWrite(bled, blue);
}
What is your board ?
If it is an UNO Aref track is too noisy and not decoupling at the good place..
You can improve the layout by soldering a capacitor (100nF ceramic ) between GND and Aref directly on the ATMega socket. --> see picture.
I use a SMD 0805 but, even it is the best, it is not mandatory. Any capacitor works.
If it is a Mega no possibility to improve the layout.
Edit: there is two capacitors only the one on the socket is mandatory, the second was for a try.
I use leonardo.
I don't know Léonardo.
On leaonardo there is a SMD package --> ![]()
