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?

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

I don't see where you use any of those. You can probably just delete them.

The rest of this code is a formatting nightmare. What's with breaking the loop up into blocks and using the same variable names over and over? Move that stuff to a function.

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.

How about showing us how you have it wired up. A crude hand drawn schematic would suffice.

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?

 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();

Do you really need all this? Does the library you're including for the RTC not have a function already written to set the time?

And is it a 3231 or a 1307? The library name says one thing but your variable names and comments say something else.

See, this is the problem with the "hack and paste" style of coding. You almost always end up with an unreadable mess that nobody can sort out.

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
        }
     }
  }
  }
}

Delta_G:
And is it a 3231 or a 1307? The library name says one thing but your variable names and comments say something else.

Its a DS3231

robmawe91:
Its a DS3231

So why do you have a library for the 1307?

I know this isn't exactly your problem, but if we can clean this code up some the source of your issue may show itself.

Here is a library for the 3231.

https://github.com/rodan/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.

Wait what? Now youre talking about two different chips again. Do you have one of each or something?

Either way, it's silly to reinvent the code to,set the time on an RTC when you can just include a library with good code already written. Especially when you have this little understanding of how code works.

Why not let's do this. Write a simple code that just reads and displays the temps without any of the clock stuff. Lets reduce this down to the problem at hand and get that fixed.

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.

I'm going to do something I normally don't do and rewrite that for you. Isn't this much simpler and easier to understand? You should never ever ever have to divide a function up into blocks and reuse variable names. At worst you should make those variables once and just reuse them. But it's much easier to write a function instead of retyping the same code over and over. There's a rule in programming called "Once and Only Once" which means that anything you do in a program should be done once and only once. If you find yourself typing the same thing over and over then make it a function and call it.

I also got rid of the variables and code you weren't using.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int HTempSensorPin0 = A0;
const int HTempSensorPin1 = A1;
const int CTempSensorPin2 = A2;
const int CTempSensorPin3 = A3;

float readTemp(int aPin){
  int value = analogRead(aPin);
  float millivolts = (value / 1024.0) * 5000;
  float celcius = millivolts / 10;
  return (celcius * 9) / 5 + 32;
}


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


void loop(){
  lcd.setCursor(2, 1);
  lcd.print(readTemp(HTempSensorPin0));
  lcd.print("F");
  lcd.setCursor(0,0);
  lcd.print("Snake LG");
  lcd.setCursor(0, 1);
  lcd.print("H");
  
  lcd.setCursor(14, 1);
  lcd.print(readTemp(HTempSensorPin1));
  lcd.print("F");
  lcd.setCursor(12,0);
  lcd.print("Snake SM");
  lcd.setCursor(12, 1);
  lcd.print("H");
  
  lcd.setCursor(2, 2);
  lcd.print(readTemp(CTempSensorPin2));
  lcd.print("F");
  lcd.setCursor(0,0);
  lcd.print("Snake LG");
  lcd.setCursor(0, 2);
  lcd.print("C");
  
  lcd.setCursor(14, 2);
  lcd.print(readTemp(CTempSensorPin3));
  lcd.print("F");
  lcd.setCursor(12,0);
  lcd.print("Snake SM");
  lcd.setCursor(12, 2);
  lcd.print("C");
  
  delay(10000);
}