MEGA AD2 & AD3 analogRead doesn't work

In the attached simple circuit, AD2 and AD3 don't work and I can't figure out what's going on.
I would appreciate your comments. Thanks.

// GENERAL VARIABLES
float temp_outdoor;
float temp_supply;
float temp_control;
float temp_return;

// INSTALLED NTCs
boolean NTC0_Instl = true;
boolean NTC1_Instl = true;
boolean NTC2_Instl = true;
boolean NTC3_Instl = true;

int NTC0pin = A0;
int NTC1pin = A1;
int NTC2pin = A2;
int NTC3pin = A3;

float NTC0_NOM = 2000, NTC0_TEMP_NOM = 25, NTC0_SERIES_RES = 2000, Bcoeff0 = 3322.84;
float NTC1_NOM = 2000, NTC1_TEMP_NOM = 25, NTC1_SERIES_RES = 2000, Bcoeff1 = 3435.80;
float NTC2_NOM = 2000, NTC2_TEMP_NOM = 25, NTC2_SERIES_RES = 2000, Bcoeff2 = 3451.53;
float NTC3_NOM = 2000, NTC3_TEMP_NOM = 25, NTC3_SERIES_RES = 2000, Bcoeff3 = 3417.01;

int sample[5];
int i;
float average;
float steinhart;

void sample_NTC0() {
    sample[i] = analogRead(NTC0pin);
    // take N samples in a row, with a slight delay
    for (i = 0; i < 5; i++) {
      sample[i] = analogRead(NTC0pin);
      delay(10);
    }
    // average all the samples out
    steinhart = 0;
    average = 0;

    for (i = 0; i < 5; i++) {
      average += sample[i];
    }
    average /= 5;
    // convert the value to resistance
    average = 1023 / average - 1;
    average = NTC0_SERIES_RES / average;

    steinhart = average / NTC0_NOM;               // (R/Ro)
    steinhart = log(steinhart);                   // ln(R/Ro)
    steinhart /= Bcoeff0;                         // 1/B * ln(R/Ro)
    steinhart += 1.0 / (NTC0_TEMP_NOM + 273.15);  // + (1/To)
    steinhart = 1.0 / steinhart;                  // Invert
    steinhart -= 273.15;                          // convert to C
    temp_outdoor = steinhart;

    Serial.print("temp_outdoor : "); Serial.println(temp_outdoor);
}

void sample_NTC1() {
	sample[i] = analogRead(NTC1pin);
    // take N samples in a row, with a slight delay
    for (i = 0; i < 5; i++) {
      sample[i] = analogRead(NTC1pin);
      delay(10);
    }
    // average all the samples out
    steinhart = 0;
    average = 0;

    for (i = 0; i < 5; i++) {
      average += sample[i];
    }
    average /= 5;
    // convert the value to resistance
    average = 1023 / average - 1;
    average = NTC1_SERIES_RES / average;

    steinhart = average / NTC1_NOM;               // (R/Ro)
    steinhart = log(steinhart);                   // ln(R/Ro)
    steinhart /= Bcoeff1;                         // 1/B * ln(R/Ro)
    steinhart += 1.0 / (NTC1_TEMP_NOM + 273.15);  // + (1/To)
    steinhart = 1.0 / steinhart;                  // Invert
    steinhart -= 273.15;                          // convert to C
    temp_supply = steinhart;

    Serial.print("temp_supply  : "); Serial.println(temp_supply);
}

void sample_NTC2() {
    sample[i] = analogRead(NTC2pin);
    // take N samples in a row, with a slight delay
    for (i = 0; i < 5; i++) {
      sample[i] = analogRead(A2); //analogRead(NTC2pin);
      delay(10);
    }
    // average all the samples out
    steinhart = 0;
    average = 0;

    for (i = 0; i < 5; i++) {
      average += sample[i];
    }
    average /= 5;
    // convert the value to resistance
    average = 1023 / average - 1;
    average = NTC2_SERIES_RES / average;

    steinhart = average / NTC2_NOM;               // (R/Ro)
    steinhart = log(steinhart);                   // ln(R/Ro)
    steinhart /= Bcoeff2;                         // 1/B * ln(R/Ro)
    steinhart += 1.0 / (NTC2_TEMP_NOM + 273.15);  // + (1/To)
    steinhart = 1.0 / steinhart;                  // Invert
    steinhart -= 273.15;                          // convert to C
    temp_outdoor = steinhart;

    Serial.print("temp_control : "); Serial.println(temp_control);
}

void sample_NTC3() {
	//sample[i] = analogRead(NTC3pin);
    // take N samples in a row, with a slight delay
    for (i = 0; i < 5; i++) {
      sample[i] = analogRead(NTC3pin);
      delay(10);
    }
    // average all the samples out
    steinhart = 0;
    average = 0;

    for (i = 0; i < 5; i++) {
      average += sample[i];
    }
    average /= 5;
    // convert the value to resistance
    average = 1023 / average - 1;
    average = NTC3_SERIES_RES / average;

    steinhart = average / NTC3_NOM;               // (R/Ro)
    steinhart = log(steinhart);                   // ln(R/Ro)
    steinhart /= Bcoeff3;                         // 1/B * ln(R/Ro)
    steinhart += 1.0 / (NTC3_TEMP_NOM + 273.15);  // + (1/To)
    steinhart = 1.0 / steinhart;                  // Invert
    steinhart -= 273.15;                          // convert to C
    temp_supply = steinhart;

    Serial.print("temp_return  : "); Serial.println(temp_return);
}

void Get_NTCsTemps() {
	sample_NTC0();delay(100);
	sample_NTC1();delay(100);
    sample_NTC2();delay(100);
	sample_NTC3();delay(100);
}

void setup() { 
Serial.begin(115200); // ESP01 Serial
  delay(100);
}

void loop() { 
Serial.println("START Get_NTCs ...");
	Get_NTCsTemps();
Serial.println("END Get_NTCs");
delay(5000);
 }

I cannot find AD2 or AD3 referenced in your code. The image is quite unreadable to me. How do you know "don't work"?

Please give some more precise hint which part of your code does not do what you want.

Hi, DrDiettrich,
"AD" is the symbology for the Arduino Analog/Digital pins in the Proteus simulator. In the code they follow the Arduino symbology.

int NTC0pin = A0;
int NTC1pin = A1;
int NTC2pin = A2;
int NTC3pin = A3;

I do not know why the scheme is not visible, it is the first time that I include an image. However, the schematic is very simple, an Arduino MEGA whose pins A0, A1, A2, A3 are connected to 4 identical 2k thermistors.

+5V
|
RES2K
|
+---- Ax
|
NTC2K
|
GND

The inputs A0 and A1 works good, A2 and A3 doesn't work. I think I have tried everything.
I don't know if the above data will suffice. By the way, I can't find how to upload an image.
Thanks

The first analogRead(i) goes into memory outside the array and overwrites what resides there.

1 Like

Take a look at this:

temp_outdoor = steinhart;
Serial.println(temp_control);

The 'temp_control' is never filled with a value and stays zero. The same for the fourth temperature sensor.

I don't like simulators. There are often weird things with them. There are questions on this forum about Proteus, and then we say to use real hardware.

The Tinkercad simulator is good :+1: It is a helpful tool.
I do like the Wokwi simulator :heart: It is developing all the time and bugs are fixed and new things are added.

You have a Arduino Mega 2560 with four NTC sensors and a virtual monitor ? Then this is your sketch with the fixes by DrDiettrich and me:

Start the simulation, then click on the temperature sensors to change their value. You need to log in to Wokwi to store it as your own project, to keep your changes.

Let's get serious. A programming language is made to be able to do things multiple times. Can you make a single function that calculates the temperature for a NTC sensor ? Then you can use that function four times.

2 Likes

Are you using PA2/AD2 and PA3/AD3 (physical pins 76 and 75)? Those are Address and Data pins for external RAM.

You want PF2/ADC2 and PF3/ADC3 (physical pins 95 and 94). Those are Arduino A2 and A3.

1 Like

Wokwi it is the demonstration that Proteus has incomprehensible flaws, both in MEGA and in UNO.
Thanks to everyone, DrDiettrich (I removed the lines), johnwasser (I use ADC0 to ADC3) and especially Koepel (Wokwi has been the light ;)).
Once again, thank you all for your time and attention.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.