-1

So I asked this question a couple days ago but did some more work with some of the coding, but I'm really stuck and need some help. I'm trying to display the temperature for each separate thermistor from input pins A0-A8 on the 2560 board. My current issue that it only reads the A8 input pin. Do I need to make more declarations such as "double readThermistor1, double readThermistor2..." etc. After I make those declarations, how would I go about displaying each separate thermistor? I'll post what I have so far:


//  Constants

const int    SAMPLE_NUMBER      = 10;
const double BALANCE_RESISTOR   = 10000.0;
const double MAX_ADC            = 1023.0;
const double BETA               = 3977.0;
const double ROOM_TEMP          = 298.15;   // room temperature in Kelvin
const double RESISTOR_ROOM_TEMP = 10060.0;


// Here is where we will save the current temperature
double currentTemperature = 0;


//  Pin Declarations

//Inputs:

int thermistorPin[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8};  // Where the ADC samples the resistor divider's output

//Outputs:


void setup() 
{ 
  // Set the port speed for serial window messages
  Serial.begin(9600);
}

double readThermistor() 
{
  // variables that live in this function
  double rThermistor = 0;            // Holds thermistor resistance value
  double tKelvin     = 0;            // Holds calculated temperature
  double tCelsius    = 0;            // Hold temperature in celsius
  double adcAverage  = 0;            // Holds the average voltage measurement
  int    adcSamples[SAMPLE_NUMBER];  // Array to hold each voltage measurement

  /* Calculate thermistor's average resistance:
     As mentioned in the top of the code, we will sample the ADC pin a few times
     to get a bunch of samples. A slight delay is added to properly have the
     analogRead function sample properly */
  
  for (int i = 0; i < SAMPLE_NUMBER; i++) 
  {
    adcSamples[i] = analogRead(thermistorPin[0]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[1]);
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[2]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[3]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[4]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[5]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[6]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[7]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
    adcSamples[i] = analogRead(thermistorPin[8]);  // read from pin and store
    delay(10);        // wait 10 milliseconds
  }

  /* Then, we will simply average all of those samples up for a "stiffer"
     measurement. */
  for (int i = 0; i < SAMPLE_NUMBER; i++) 
  {
    adcAverage += adcSamples[i];      // add all samples up . . .
  }
  adcAverage /= SAMPLE_NUMBER;        // . . . average it w/ divide

  rThermistor = BALANCE_RESISTOR * ( (MAX_ADC / adcAverage) - 1);

 
  tKelvin = (BETA * ROOM_TEMP) / 
            (BETA + (ROOM_TEMP * log(rThermistor / RESISTOR_ROOM_TEMP)));


  tCelsius = tKelvin - 273.15;  // convert kelvin to celsius 

  return tCelsius;    // Return the temperature in Celsius
}



void loop()

{
  currentTemperature = readThermistor();
  delay(3000);
  
  /* Here is how you can act upon a temperature that is too hot,
  too cold or just right. */
  if (currentTemperature > 0.0 && currentTemperature < 60.0)
  {
    Serial.print("It is ");
    Serial.print(currentTemperature);
    Serial.println("C. Ahhh, very nice temperature.");
  
  } 
  else if (currentTemperature >= 60.0)
  {
    Serial.print("It is ");
    Serial.print(currentTemperature);
    Serial.println("C. I feel like a hot tamale!");
  } 
}

Any help is appreciated, thank you.

5
  • My current issue that it only reads the A8 ... that is what you told the Arduino to do ... see the for (int i = 0; i < SAMPLE_NUMBER loop .... think about the adcSamples[i] variable
    – jsotola
    Apr 21 at 22:24
  • you probably want a 2D array ... use two nested for loops to read the sensor values into the array ... multiple analogRead() lines are not necessary
    – jsotola
    Apr 21 at 22:27
  • 1
    I would personally consider rolling all the data and logic for one thermistor into a class, then instantiating multiple classes for each thermistor.
    – Majenko
    Apr 21 at 22:29
  • for(s ...) for(i ...) adcSamples[i][s] = analogRead(thermistorPin[i]); ... i is sensor number ... s is sample number
    – jsotola
    Apr 21 at 23:02
  • Why use eight thermistors if you only want to read one temperature? Using a single, well calibrated thermistor would be more sensible. Apr 22 at 18:18

1 Answer 1

1

Here is my proposal:

Sum the readings (samples) of each sensor(thermistor) and store the sum in an array. Then divide each sum by the number of samples that were taken. Instead of computing one tCelsius, compute one tCelsius for each average values and store it in an array too. Then print each of them. Feel free to print it as you like.

BTW I did not compile and test the code. There might be errors in it. Use it as a hint/example.

const int    SAMPLE_NUMBER      =    10;
const double BALANCE_RESISTOR   = 10000.0;
const double MAX_ADC            =  1023.0;
const double BETA               =  3977.0;
const double ROOM_TEMP          =   298.15;   
const double RESISTOR_ROOM_TEMP = 10060.0;

// PIN_COUNT is the length of the thermistorPin list.
const double PIN_COUNT = 9;
int thermistorPin[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8};  

void setup() 
{ 
  Serial.begin(9600);
}

double readThermistor() 
{
  double rThermistor = 0.0; 
  double tKelvin     = 0.0; 

  // The count of the zeros below should be equal 
  // to the PIN_COUNT
  double tCelsius[] 
           = {0.0, 0.0, 0.0, 0.0, 0.0, 
              0.0, 0.0, 0.0, 0.0};
  double adcSampleSumPerPin[]
           = {0.0, 0.0, 0.0, 0.0, 0.0, 
              0.0, 0.0, 0.0, 0.0};

  for (int i = 0; i < SAMPLE_NUMBER; i++)
  { 
    for (int j = 0; j < PIN_COUNT; i++) 
    {
      adcSampleSumPerPin[j] += 
                   (double) analogRead(thermistorPin[j]);
      delay(10);
    }
  }

  for (int i = 0; i < PIN_COUNT; i++) 
  {
    adcSampleSumPerPin[i] /= SAMPLE_NUMBER;
    rThermistor = BALANCE_RESISTOR *
                  ((MAX_ADC / adcSampleSumPerPin[i]) - 1);
    tKelvin = (BETA * ROOM_TEMP) / 
              (BETA + (ROOM_TEMP * log(rThermistor / 
              RESISTOR_ROOM_TEMP)));
    tCelsius[i] = tKelvin - 273.15;  
  }

  return tCelsius; 
}
   
void loop()
{
  delay(3000);
  currentTemps = readThermistor();

  for (i = 0; i < PIN_COUNT; i++)
  {
    double currentTemperature = currentTemp[i];
    if (currentTemperature > 0.0 && currentTemperature < 60.0)
    {
      Serial.print("Sensor A");
      Serial.print(i);
      Serial.println(":"); 

      Serial.print("It is ");
      Serial.print(currentTemperature);
      Serial.println("C. Ahhh, very nice temperature."); 
    }
    else if (currentTemperature >= 60.0)
    {
      Serial.print("Sensor A");
      Serial.print(i);
      Serial.println(":"); 

      Serial.print("It is ");
      Serial.print(currentTemperature);
      Serial.println("C. I feel like a hot tamale!");
    } 
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.