I built this thermometer that reads the sensor value from the temperature sensor and displays it on the LCD display. It basically works fine, but the displayed temperature shows a fluctuation between +/-2 degrees due to varying sensor value and voltage every second. (This is my very first Arduino project and I am pretty new to electronics.) The Arduino gets the power via my Mac through USB.
When I check the sensor value and voltage, it slightly differs each second (or whatever delay interval I use in my code). Sensor value is between 123-125 and voltage usually 0.7, but also changes slightly, which causes the temperature calculation to vary around 2 degrees, which for a "thermometer" is quite a lot.
Is there any way to stabilize it? Should I use a capacitor or another resistor?
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const float powerSupply = 5.0;
const int sensorPin = A0;
const float baselineTemp = 23;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Temperature:");
}
void loop() {
int sensorVal = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
float voltage = (sensorVal/1024.0)*powerSupply;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees C: ");
float temperature = (voltage - .5)*100;
Serial.println(temperature);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(temperature);
// lcd.print(String(sensorVal) + ", " + String(voltage) + ", " + String(temperature));
delay(1000);
}