Why are my temps fluctuating so much?

My temps are fluctuating from ~40F to ~110F.
Please help!
here is my sketch...

#include <LiquidCrystal.h>
#include <Wire.h>
#include <RealTimeClockDS1307.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//RS, E, D4, D5, D6, D7

#define DS3231_I2C_ADDRESS 0x68
byte decToBcd(byte val)// Convert normal decimal numbers to binary coded decimal
{
 return( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val)// Convert binary coded decimal to normal decimal numbers
{
 return( (val/16*10) + (val%16) );
}
#define Display_Clock_Every_N_Seconds 10 
#define Display_ShortHelp_Every_N_Seconds 60
String tz;
int hours = 0;
int minutes = 0;
int seconds = 0;
int dates = 0;
int months = 0;
int years = 0;
int ap = 0;
int RelayL = 8;//Relay Large Tank
int RelayS = 9;//Relay Small Tank
float temp;
float tempC;
//int tempMax = 37.78;// 100F
float tempF;
const int HTempSensorPin0 = 0;// A0 Hot Side Large Tank
const int HTempSensorPin1 = 1;// A1 Hot Side Small Tank
const int CTempSensorPin2 = 2;// A2 Cold Side Large Tank
const int CTempSensorPin3 = 3;// A3 Cold Side Small Tank

void setup()
{
 Wire.begin();
 // set the initial time here:
 //DS3231 seconds, minutes, hours, day, date, month,  year;
 //setDS3231time(30,33,20,5,05,11,15);
 Serial.begin(9600);
 pinMode(RelayL,OUTPUT);
 pinMode(RelayS,OUTPUT);
 lcd.begin(20, 4);
 lcd.setCursor(3, 1);
 lcd.print("Loading Temps");
 lcd.setCursor(4, 2);
 lcd.print("Please Wait");
 {
   delay(700);
 }
 {
   lcd.clear();
 }
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)

{
 // sets time and date data to DS3231
 Wire.beginTransmission(DS3231_I2C_ADDRESS);
 Wire.write(0); // set next input to start at the seconds register
 Wire.write(decToBcd(second)); // set seconds
 Wire.write(decToBcd(minute)); // set minutes
 Wire.write(decToBcd(hour)); // set hours
 Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
 Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
 Wire.write(decToBcd(month)); // set month
 Wire.write(decToBcd(year)); // set year (0 to 99)
 Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
 Wire.beginTransmission(DS3231_I2C_ADDRESS);
 Wire.write(0); // set DS3231 register pointer to 00h
 Wire.endTransmission();
 Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
 // request seven bytes of data from DS3231 starting from register 00h
 *second = bcdToDec(Wire.read() & 0x7f);
 *minute = bcdToDec(Wire.read());
 *hour = bcdToDec(Wire.read() & 0x3f);
 *dayOfWeek = bcdToDec(Wire.read());
 *dayOfMonth = bcdToDec(Wire.read());
 *month = bcdToDec(Wire.read());
 *year = bcdToDec(Wire.read());
}

void loop()
{
 {
 RTC.readClock();
   if(ap == 1)
   {
   tz = "PM";
   }
   else
   {
   tz ="AM";
   }
   lcd.home();
   hours = RTC.getHours();
   minutes = RTC.getMinutes();
   seconds = RTC.getSeconds();
   ap = RTC.isPM();
   dates = RTC.getDate();
   months = RTC.getMonth();
   years = RTC.getYear();
   lcd.setCursor(0, 3);
   if (hours >12)
   {
     hours -= 12;
     tz = "PM";
   }
   else
   tz = "AM";
   lcd.print(hours);
   lcd.print(":");
   lcd.print(minutes);
   //lcd.print(":");
   //lcd.print(seconds);
   lcd.print(" ");
   lcd.setCursor(5, 3);
   lcd.print(tz); 
   lcd.setCursor(13, 3);
   lcd.print(months);
   lcd.print("/");
   lcd.print(dates);
   lcd.print("/");
   lcd.print(years);
   delay(1);   
}
 int value = analogRead(HTempSensorPin0);// Read Sensor
 float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
 float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
 lcd.setCursor(2, 1);
 lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
 lcd.print("F");//Fahrenheit 
 lcd.setCursor(0, 0);
 lcd.print("LG Tank");
 lcd.setCursor(0, 1);
 lcd.print("H");//Hot 
 if (celsius >37.78)//100F
 {
   digitalWrite(8,LOW);
 }
 if (celsius <31.11)//88F
 {
   digitalWrite(8,HIGH);
 }
 {
    int value = analogRead(HTempSensorPin1);// Read Sensor
    float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
    float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
    lcd.setCursor(14, 1);
    lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
    lcd.print("F");//Fahrenheit
    lcd.setCursor(13, 0);
    lcd.print("SM Tank");
    lcd.setCursor(12, 1);
    lcd.print("H");//Hot
    if (celsius >37.78)//100F
 {
   digitalWrite(9,LOW);
 }
 if (celsius <31.11)//88F
 {
   digitalWrite(9,HIGH);
 }
 {
    int value = analogRead(CTempSensorPin2);// Read Sensor
    float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
    float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
    lcd.setCursor(2, 2);
    lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
    lcd.print("F");//Fahrenheit
    lcd.setCursor(0, 0);
    lcd.print("LG Tank");
    lcd.setCursor(0, 2);
    lcd.print("C");//Cold
    {
       int value = analogRead(CTempSensorPin3);// Read Sensor
       float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
       float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
       lcd.setCursor(14, 2);
       lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
       lcd.print("F");//Fahrenheit
       lcd.setCursor(13, 0);
       lcd.print("SM Tank");
       lcd.setCursor(12, 2);
       lcd.print("C");//Cold
       {
          delay(1000);//Delay Before Next Temp Reading
       }
    }
 }
 }
}

Please edit your post and add code tags (select and use </> button)

robmawe91:
My temps are fluctuating from ~40F to ~110F.

Sounds like a wiring error has left your analog inputs floating.

Should I change
float temp;
float tempC;
and
float tempF;
?
and to what?

I'm new to programming. I wrote this from reading tons of other programs. this was the only way I could get it to work besides the temps are fluctuating all over the place.

I worked on the wiring and I got it to stay somewhat more constant
all four sensors (LM35) and within 1 inch of each other on the breadboard.

each sensor fluctuates about 3 degrees
but one will say 100F and another one will say 77F.

add serial prints and read the raw data of the analog pins to see if they are stable. Take notes if the readings jump when relays are activated,
If you are powering the arduino via your computer usb you may notice the readings become unstable as the voltage from the usb may not be stable.

are all 3 readings unstable, are they unstable at the same time. e.g they all go up and down together?

I deleted alot and its still working :slight_smile: . each sensor fluctuates about 1 degree.
HTempSensorPin0 = A0 and HTempSensorPin1 =A1 1-2 degrees difference
CTempSensorPin2 = A2 and CTempSensorPin3 = A3 1-2 degrees difference
but
HTempSensorPin0 = A0 and HTempSensorPin1 =A1 is about 10 degrees cooler than CTempSensorPin2 = A2 and CTempSensorPin3 =A3

#include <LiquidCrystal.h>
#include <Wire.h>
#include <RealTimeClockDS1307.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//RS, E, D4, D5, D6, D7

String tz;
int hours = 0;
int minutes = 0;
int seconds = 0;
int dates = 0;
int months = 0;
int years = 0;
int ap = 0;
int RelayL = 8;//Relay Large Tank
int RelayS = 9;//Relay Small Tank
//float temp;
//float tempC;
//int tempMax = 37.78;// 100F
//float tempF;
const int HTempSensorPin0 = 0;// A0 Hot Side Large Tank
const int HTempSensorPin1 = 1;// A1 Hot Side Small Tank
const int CTempSensorPin2 = 2;// A2 Cold Side Large Tank
const int CTempSensorPin3 = 3;// A3 Cold Side Small Tank

void setup()
{
  Wire.begin();
  // set the initial time here:
  // seconds, minutes, hours, day, date, month,  year;
  // setDS3231time(30,33,20,5,05,11,15);
  Serial.begin(9600);
  pinMode(RelayL,OUTPUT);
  pinMode(RelayS,OUTPUT);
  lcd.begin(20, 4);
  lcd.setCursor(3, 1);
  lcd.print("Loading Temps");
  lcd.setCursor(4, 2);
  lcd.print("Please Wait");
  {
    delay(700);
  }
  {
    lcd.clear();
  }
}

void loop()

{
  {
  RTC.readClock();
    if(ap == 1)
    {
    tz = "PM";
    }
    else
    {
    tz ="AM";
    }
    lcd.home();
    hours = RTC.getHours();
    minutes = RTC.getMinutes();
    seconds = RTC.getSeconds();
    ap = RTC.isPM();
    dates = RTC.getDate();
    months = RTC.getMonth();
    years = RTC.getYear();
    lcd.setCursor(0, 3);
    if (hours >12)
    {
      hours -= 12;
      tz = "PM";
    }
    else
    tz = "AM";
    lcd.print(hours);
    lcd.print(":");
    lcd.print(minutes);
    //lcd.print(":");
    //lcd.print(seconds);
    lcd.print(" ");
    lcd.setCursor(5, 3);
    lcd.print(tz); 
    lcd.setCursor(13, 3);
    lcd.print(months);
    lcd.print("/");
    lcd.print(dates);
    lcd.print("/");
    lcd.print(years);
    delay(1);   
}
  int value = analogRead(HTempSensorPin0);// Read Sensor
  float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
  float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
  lcd.setCursor(2, 1);
  lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
  lcd.print("F");//Fahrenheit 
  lcd.setCursor(0, 0);
  lcd.print("LG Tank");
  lcd.setCursor(0, 1);
  lcd.print("H");//Hot 
  if (celsius >37.78)//100F
  {
    digitalWrite(8,LOW);
  }
  if (celsius <31.11)//88F
  {
    digitalWrite(8,HIGH);
  }
  {
     int value = analogRead(HTempSensorPin1);// Read Sensor
     float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
     float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
     lcd.setCursor(14, 1);
     lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
     lcd.print("F");//Fahrenheit
     lcd.setCursor(13, 0);
     lcd.print("SM Tank");
     lcd.setCursor(12, 1);
     lcd.print("H");//Hot
     if (celsius >37.78)//100F
  {
    digitalWrite(9,LOW);
  }
  if (celsius <31.11)//88F
  {
    digitalWrite(9,HIGH);
  }
  {
     int value = analogRead(CTempSensorPin2);// Read Sensor
     float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
     float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
     lcd.setCursor(2, 2);
     lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
     lcd.print("F");//Fahrenheit
     lcd.setCursor(0, 0);
     lcd.print("LG Tank");
     lcd.setCursor(0, 2);
     lcd.print("C");//Cold
     {
        int value = analogRead(CTempSensorPin3);// Read Sensor
        float millivolts = (value / 1024.0) * 5000;// Convert Reading to Millivolts
        float celsius = millivolts / 10;//Convert Millivolts to Celsius //9.31 - 10 - 12?
        lcd.setCursor(14, 2);
        lcd.print((celsius * 9) / 5 + 32);//Convert Celsius To Fahrenheit
        lcd.print("F");//Fahrenheit
        lcd.setCursor(13, 0);
        lcd.print("SM Tank");
        lcd.setCursor(12, 2);
        lcd.print("C");//Cold
        {
           delay(1000);//Delay Before Next Temp Reading
        }
     }
  }
  }
}

Its a DS3231

I know thats not the problem. The sensors were doing the same thing before I decided to add the RTC. Thats why I haven't worried about fixing it. I'm just using the 1307 to show time and date on the display. I'm going to use Wire.h (3231) to make my tank warmer during the day and cooler at night.

This is what I started with.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//RS, E, D4, D5, D6, D7
int reading;
float temp;
float tempC;
float tempF;
const int HTempSensorPin0 = 0;// A0 Hot Large Tank
const int HTempSensorPin1 = 1;// A1 Hot Small Tank
const int CTempSensorPin2 = 2;// A2 Cold Large Tank
const int CTempSensorPin3 = 3;// A3 Cold Small Tank

void setup()
{
  lcd.begin(20, 4);
}

void loop()
{
  tempC = (temp * 5) / 10;
  tempF = (tempC * 9) / 5 + 32;
  {
    int value = analogRead(HTempSensorPin0);
    float millivolts = (value / 1024.0) * 5000;
    float celsius = millivolts / 10;//9.31 - 10 - 12
    lcd.setCursor(2, 1);
    lcd.print((celsius * 9) / 5 + 32);
    lcd.print("F");
    lcd.setCursor(0, 0);
    lcd.print("Snake LG");
    lcd.setCursor(0, 1);
    lcd.print("H");
    {
      int value = analogRead(HTempSensorPin1);
      float millivolts = (value / 1024.0) * 5000;
      float celsius = millivolts / 10;
      lcd.setCursor(14, 1);
      lcd.print((celsius * 9) / 5 + 32);
      lcd.print("F");
      lcd.setCursor(12, 0);
      lcd.print("Snake SM");
      lcd.setCursor(12, 1);
      lcd.print("H");
      {
        int value = analogRead(CTempSensorPin2);
        float millivolts = (value / 1024.0) * 5000;
        float celsius = millivolts / 10;
        lcd.setCursor(2, 2);
        lcd.print((celsius * 9) / 5 + 32);
        lcd.print("F");
        lcd.setCursor(0, 0);
        lcd.print("Snake LG");
        lcd.setCursor(0, 2);
        lcd.print("C");
        {
          int value = analogRead(CTempSensorPin3);
          float millivolts = (value / 1024.0) * 5000;
          float celsius = millivolts / 10;
          lcd.setCursor(14, 2);
          lcd.print((celsius * 9) / 5 + 32);
          lcd.print("F");
          lcd.setCursor(12, 0);
          lcd.print("Snake SM");
          lcd.setCursor(12, 2);
          lcd.print("C");
          {
            delay(10000);
          }
        }
      }
    }
  }
}

robmawe91:
each sensor fluctuates about 3 degrees
but one will say 100F and another one will say 77F.

Well, either one of them is wrong, or they are measuring different things (one's a the floor, one's at the ceiling).

Or, the voltage dividers on the pins are using different valued resistors.

Or, you don't know that you have to use a voltage divider. Actually, this one is most likely. You just attached one end of the resistor to +5v and the other end to a pin.

All the LM35 temp sensors are on a breadboard with in a inch of each other. I have the ground going to ground positive going to 5V and the sensor pin going to analog input on the Arduino Mega. (A0, A1, A2 and A3)

I'm not using a voltage divider. I haven't seen any in any of the projects I have seen. Also I wouldn't know what ohm resistors I would use.

It is possible your LM35's are defective. If you swap them around does the reading move with the LM35 (defective LM35) or does the reading in each position remain the same (defective somewhere else)? Then try swapping around the analog input pins. If the readings move then the breadboard wiring is defective. If the readings stay in the same place, the ATmega analog input pins are defective.

As others have suggested, you probably need to make simple code to only read the device input and print that to the serial monitor for evaluation. That should identify any hardware issues with the temperature device. Also, in the past it has been posted that to get the most stable analog input, the point should be read twice and use the second reading. You might try something like below to see if the reading improves.

int value = analogRead(HTempSensorPin0);// Read Sensor
int value = analogRead(HTempSensorPin0);// Read Sensor

That code worked better and I also switch out my Arduino and im using A4, A5, A6, A7 now.
I added the RTC back into it and still working good.
Then I added the relays in back into it and its back to where I started. If I disconnect the Digital pins 89 going to the relays it works again. I tried with USB, 9v battery and a 10V power supply. So I believe its getting enough power. I also tried different relays. I also took out the...

if (celsius >37.78)//100F
  {
    digitalWrite(8,LOW);
  }
  if (celsius <31.11)//88F
  {
    digitalWrite(8,HIGH);
  }

Still don't work. I even tried it with out having the RTC in there with no luck.

Thank you for rewriting that for me. Looks a lot cleaner. :slight_smile:

Yes I'm using the Arduino to power the relays and I'm not using any transistor. I'll have to look up some schematics to see how people are doing it.

I'll read that page in a few. I'm using a DS3231 chip. I'm using Wire.h(3231) to set the time to the chip and realtimeclockDS1307.h to read the chip and display that time and date. and convert it to 12 hours instead of 24 hour clock. I had a very hard time getting the clock to work with the display. all the codes I could fine either used serial and I couldn't get it to work with liquidCrystal or used a library I couldn't find and download. It was a nightmare. And as you can see I'm new to programming and building circuits. I bought a few books to help me out but this is a little confusing.

Yes I have it. DS3231-master. So I should remove all the clock code and start over?

This is the relay I'm using...
http://www.ebay.com/itm/Effective-Stable-1-Channel-5V-Indicator-Light-LED-Relay-Module-For-Arduino-EA-/281782135553?hash=item419b862b01:g:RRUAAOSwyQtV3CfV
I believe it has a transistor on it (2TY). Show I should just need a 5V power supply to power it right?

robmawe91:
Yes I have it. DS3231-master. So I should remove all the clock code and start over?

it amazing how a good library can make a world of difference.

if you have the new library open the examples and play with them.

the relay uses 5v, grd and signal......The relay will work backwards so signal low is relay on. Remember to set the output high in setup so the relay does not activate before entering the loop