Arduino programmed ATtiny24, 5K thermistor input problem

Hello,

I am designing a battery charger for an NiMH battery pack, I decided to use a delta T/delta t (changing temperature) termination method as an end of charge signal, I ended up requiring an onboard microcontroller to monitor the temperature and could only find an ATtiny24 in the market, I got the tiny24 programmed using the Arduino uno and everything works well except for a small problem. I believe the attiny24 has a 10bit ADC, using the default internal reference (i.e Vcc = 5) I get a resolution of around 4.88mV/step for 0 - 1023 steps.

so with this if the voltage on one of the analog pins is 1.7 Volts then this should correspond to a value of around 348 from the analogRead() command but when measuring through a voltmeter the circuit always seems to terminate at around 1.86Volts. I am not sure if the problem is with the accuracy of my voltmeter or is it normal to expect such errors while working with an ADC?

//int Therm = A1; //Thermister analog input 1
int Ctrl=2; //Relay control output
int Temp_C,Temp_N,i,termcharge,deltaT,Estop; // variables for temperature measurement and minute looping
void setup()
{
  pinMode (0, OUTPUT);
  pinMode (Ctrl, OUTPUT);
  for(i=0;i<=5;i++) //5 second initialization indication 
  {
    digitalWrite(0,HIGH);
    delay(1000);
    digitalWrite(0,LOW);
    delay(1000);}
  digitalWrite(Ctrl, HIGH);
  i = 0;
  termcharge = 0; //Vaiable is 0 when charging is active
  Estop = 0;
}
void loop()
{
  Temp_C = analogRead(A1);
  while(i<=59)
    {
    digitalWrite(0,HIGH);
    delay(500);
    digitalWrite(0,LOW);
    delay(500);
    i++;
    }
  Temp_N = analogRead(A1);
  deltaT = Temp_C - Temp_N;
  if(deltaT >= 10 && Estop == 0) //If condition for temperature difference is greater or equal to 1 degree C/Minute
  {
    digitalWrite(Ctrl, LOW);
      digitalWrite(0,HIGH);
      i=61;
      termcharge =1;
  }
   
     if(Temp_C <= 348 || Temp_N <= 348)//Emergency cutoff charging
    {
      digitalWrite(0,LOW);
      digitalWrite(Ctrl, LOW);
      i=61;
      termcharge =1;
      Estop = 1;
    }
    if (termcharge == 0)
    {
      i=0;
    }
      
}

Check data for your multimeter. is 5V ... yeh 5V?

I tired using another multimeter, this one is supposed to be more precise, the output still switches at around 1.86 volts. I realized the 5 volts varies, I measured it at around 5.24 to 5.26 volts with such variations I can't be sure if the internal reference of the ADC is any good so now I am connecting an LM4040 4.096 volts shunt regulator for an external reference signal, the thing is pretty stable and might yield relatively accurate results, I haven't yet fully tested it though.

I' surprised that the voltage regulator is THAT much off 5V. The external ref. seems a good idea

Hello Knut_ny,

Thanks for the response, after providing an external reference through 4.1V LM4040 I was able to get a switching state at 1.718, I originally intended it to be 1.7 Volts for a step count of 425 but this is close enough and there’s a huge improvement in the accuracy, I think I can work with this, the LM4040 does switch between 4.10 to 4.12 volts I am planning on putting a capacitor to see if I can dampen out these slight variations and get a far more accurate result.

I do have a question though, I remember reading somewhere that when you shift from internal default ADC reference to an external reference the first few readings made through the ADC will have errors, is this right? I did notice something similar, my relay was supposed to switch at 1.7Volts however for the first 6 to 7 readings my relay switched at voltages like 2.1, 1.65 and slowly got stable at 1.718 volts, tried turning the circuit on and off a few times and every time the voltages were the same.

Appreciate your help.

can you post your schematic & code ?

It is true that it is smart to read the analog pin twice (when reading several analog pins) This is due to a small capacitance in the 328 internals.

Hello Knut_ny,

Thanks for the response, I don’t have schematic diagram so I drew up a rough sketch, I had to draw by what I remember so apologies if it looks messy.

The code which I am currently running on the tiny24 is

//int Therm = A1; //Thermister analog input 1
int Ctrl=2; //Relay control output
int Temp_C,Temp_N,i,termcharge,deltaT,Estop; // variables for temperature measurement and minute looping
void setup()
{
  pinMode (3, OUTPUT);
  pinMode (Ctrl, OUTPUT);
  for(i=0;i<=5;i++) //5 second initialization indication 
  {
    digitalWrite(3,HIGH);
    delay(1000);
    digitalWrite(3,LOW);
    delay(1000);}
  digitalWrite(Ctrl, HIGH);
  i = 0;
  termcharge = 0; //Vaiable is 0 when charging is active
  Estop = 0;
  analogReference(EXTERNAL);
  delay(3);
}
void loop()
{
  Temp_C = analogRead(A1);
  while(i<=59)
    {
    digitalWrite(3,HIGH);
    delay(500);
    digitalWrite(3,LOW);
    delay(500);
    i++;
    }
  Temp_N = analogRead(A1);
  deltaT = Temp_C - Temp_N;
  if(deltaT >= 10 && Estop == 0) //If condition for temperature difference is greater or equal to 1 degree C/Minute
  {
    digitalWrite(Ctrl, LOW);
      digitalWrite(3,HIGH);
      i=61;
      termcharge =1;
   }
   
     if(Temp_C <= 425 || Temp_N <=425 )//Emergency cutoff charging
    {
      digitalWrite(3,LOW);
      digitalWrite(Ctrl, LOW);
      i=61;
      termcharge =1;
      Estop = 1;
    }
    if (termcharge == 0)
    {
      delay(3);
      i=0;
    }
      
}

But I am modifying this code as we speak, I realized I need to implement some form of the Steinhart-Hart equation in the code to properly relate the ADC steps to the actual temperature which will allow for reliable temperature monitoring of the battery.