From UNO to Mega problems with analog read

Hello
I am new to this forum so sorry if I make some kind of mistake.

I have some code for reading the temperature from 4 analog inputs with 4 different temperature probes. On UNO everything works perfectly. As I need more PINs I want to move to MEGA but the readings on MEGA are completely wrong. The mega keeps returning only the values from 1 and 2 (and wrong) and nothing for 3 and 4. Also when nothing is connected to the analog pins it keeps returning some values that of course are wrong.

I tried so far a capacitor, adding 9V battery to power supply, adding an external reference (5v) but it did not work till now.

I would be glad if somebody could help me with the problem. I am posting now only the code for two analog readings but it is the same for 3 and 4 as for the code shown below.

byte NTCPin1 = A0;
#define SERIESRESISTOR1 100000
#define NOMINAL_RESISTANCE1 100000
#define NOMINAL_TEMPERATURE1 25
#define BCOEFFICIENT1 3950

const int numReadings1 = 60;
int readings1[numReadings1];       // števila iz analogengega vhoda
int readIndex1 = 0;                // postavimo začetni indeks na 0
float total1;                      // skupni seštevek
float average1;         

byte NTCPin2 = A1;
#define SERIESRESISTOR2 100000
#define NOMINAL_RESISTANCE2 100000
#define NOMINAL_TEMPERATURE2 25
#define BCOEFFICIENT2 3950

const int numReadings2 = 60;
int readings2[numReadings2];       // števila iz analogengega vhoda
int readIndex2 = 0;                // postavimo začetni indeks na 0
float total2;                      // skupni seštevek
float average2;         


void setup()
{
Serial.begin(9600);
 for (int thisReading1 = 0; thisReading1 < numReadings1; thisReading1++) {
    readings1[thisReading1] = 0;
 }
 for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++) {
    readings2[thisReading2] = 0;
 }

}
void loop(){
  float ADCvalue1;
  float Resistance1;
  float ADCvalue2;
  float Resistance2;


  ADCvalue1 = analogRead(NTCPin1);
  ADCvalue2 = analogRead(NTCPin2);


//Serial.print("Analog value ");
//Serial.print(ADCvalue1);
//Serial.print(" = ");
  Resistance1 = (1023 / ADCvalue1) - 1;           //vrednost spremenimo v upornost
  Resistance1 = SERIESRESISTOR1 / Resistance1;
  Resistance2 = (1023 / ADCvalue2) - 1;           //vrednost spremenimo v upornost
  Resistance2 = SERIESRESISTOR2 / Resistance2;
//Serial.print(Resistance1);
//Serial.println(" Ohm");

  float steinhart1;
  steinhart1 = Resistance1 / NOMINAL_RESISTANCE1;       // (R/Ro)
  steinhart1 = log(steinhart1);                         // ln(R/Ro)
  steinhart1 /= BCOEFFICIENT1;                          // 1/B * ln(R/Ro)
  steinhart1 += 1.0 / (NOMINAL_TEMPERATURE1 + 273.15);  // + (1/To)
  steinhart1 = 1.0 / steinhart1;                        // Invertiramo
  steinhart1 -= 273.15;                                 // konverzijav stopinje

  Serial.print("Temperature 1 ");

  total1 = total1 - readings1[readIndex1];            // Kalkulacija po steinhartovi enačbi
  readings1[readIndex1] = steinhart1;                 // preberi senzor 
  total1 = total1 + readings1[readIndex1];            // dodaj kar prebere v skupni seštevek
  readIndex1 = readIndex1 + 1;                        // pomakni števnik za eno gor 
  if (readIndex1 >= numReadings1) {                   // če smoprišli do konca štetja postavi števec na 0
    readIndex1 = 0;                                   
  }
  average1 = (total1 / numReadings1)*0.983;           //deli skupno število z številom odčitkov da dobiš povprečje. to množimo še z korekcijskim faktorjem
   
   if (average1 >= 102) {                             // vsi ndaljni if stavki namenjeni korekturi odčitkov iz senzorja
    average1 = average1 * 0.985;                                  
  }
  else if ((average1 >= 100)&&(average1 < 102)){                   
    average1 = average1 * 0.970;                                  
  }
    else if ((average1 >= 90)&&(average1 < 100)){                   
    average1 = average1 * 0.972;                                  
  }
   else if ((average1 >= 80)&&(average1 < 90)){                   
    average1 = average1 * 0.974;                                  
  }
   else if ((average1 >= 74)&&(average1 < 80)){                   
    average1 = average1 * 0.979;                                  
  }
    else if ((average1 >= 65)&&(average1 < 74)){                   
    average1 = average1 * 0.987;                                  
  }
   else if ((average1 >= 55)&&(average1 < 65)){                   
    average1 = average1 * 0.992;                                  
  }
    else {                   
    average1;                                 
  }
  
 if (average1 >= 5) {                                  // če je manj kot 5 stopinj ne prikaži. to je zato ker če sonda ni priključena kaže čudna števila
    Serial.println(average1);                                  
  }
   else {                   
    Serial.println(0);                                 
  } 
  delay(1);        // zakasnitev zaradi stabilnosti
 
///////////////////////////SENZOR 2/////////////////////////////
  float steinhart2;
  steinhart2 = Resistance2 / NOMINAL_RESISTANCE2;       // (R/Ro)
  steinhart2 = log(steinhart2);                         // ln(R/Ro)
  steinhart2 /= BCOEFFICIENT2;                          // 1/B * ln(R/Ro)
  steinhart2 += 1.0 / (NOMINAL_TEMPERATURE2 + 273.15);  // + (1/To)
  steinhart2 = 1.0 / steinhart2;                        // Invertiramo
  steinhart2 -= 273.15;                                 // konverzija v stopinje

 //Serial.print("Temperature 2 ");

  total2 = total2 - readings2[readIndex2];             // Kalkulacija po steinhartovi enačbi
  readings2[readIndex2] = steinhart2;                  // preberi senzor 
  total2 = total2 + readings2[readIndex2];             // dodaj kar prebere v skupni seštevek
  readIndex2 = readIndex2 + 1;                         // pomakni števnik za eno gor 
  if (readIndex2 >= numReadings2) {                    // če smoprišli do konca štetja postavi števec na 0
    readIndex2 = 0;                                   
  }
  average2 = (total2 / numReadings2)*0.983;            //deli skupno število z številom odčitkov da dobiš povprečje. to množimo še z korekcijskim faktorjem
   
   if (average2 >= 100) {                              // vsi ndaljni if stavki namenjeni korekturi odčitkov iz senzorja
    average2 = average2 * 0.968;                                  
  }
    else if ((average2 >= 90)&&(average2 < 100)){                   
    average2 = average2 * 0.972;                                  
  }
   else if ((average2 >= 80)&&(average2 < 90)){                   
    average2 = average2 * 0.974;                                  
  }
   else if ((average2 >= 74)&&(average2 < 80)){                   
    average2 = average2 * 0.979;                                  
  }
    else if ((average2 >= 65)&&(average2 < 74)){                   
    average2 = average2 * 0.987;                                  
  }
   else if ((average2 >= 55)&&(average2 < 65)){                   
    average2 = average2 * 0.992;                                  
  }
    else {                   
    average2;                                 
  }

 if (average2 >= 5) {                                   
    //Serial.println(average2);                                  
  }
   else {                   
    //Serial.println(0);                                 
  }  
  delay(1);                                             // zakasnitev zaradi stqbilnosti odčitkov
}

If you have nothing connected to an analog pin, it will "float" and pick up random noise/interference so you can easily get odd values. Are you sure the wiring is good/secure when you moved from the UNO to a Mega?

You could always try putting a potentimeter on one of the inputs that you could turn up/down to see the codes reaction.

Your program seems only to be reading from A0 and A1.

Also your program refers to the pins as NTCPinX which makes me wonder what exactly is connected to the pins and how it is powered and whether it is the way the sensor is being used that is the problem - rather than the program.

...R

I am using a 100k ohm food temperature probe with 100 k ohm resistor and powerd over the 5 V to measure it so nothing special and also on UNO no problems.

As I stated I only copied the code for the first two probes not to spam with the whole code. But for the other two the code is the same only the number reference is off course different.

Lenardic:
I am using a 100k ohm food temperature probe with 100 k ohm resistor and powerd over the 5 V to measure it so nothing special and also on UNO no problems.

I'm not sure what you mean by "the 5 v" but is it possible that the problem is due to the power supply?

The best way to test the analog inputs is as suggested in Reply #1 - with a potentiometer. That also gives you the opportunity to measure the voltage on the analog pin with your multimeter and relate it to the value produced by analogRead()

You could check if the Uno and Mega give the same result for the same voltage.

...R

First make the dimensions correct -- analogRead() returns int type value, but you are saving it into float type variable.

float ADCvalue1;
ADCvalue1 = analogRead(NTCPin1);