Thermistor Temperature not good ATtiny85

hi all,
everything here is doing what it must, but the temp is incorrect.
as a result, when I ‘settemp’, the difference between ‘test’ ‘settemp’ and ‘alarm’ is no good
lets say I ‘settemp’ @ 80deg. then ‘test’ must be 75deg and alarm 90deg

#include <C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM\EEPROM.h>
#include "DigiKeyboard.h"
int counter = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
float settemp;
void setup() {
  pinMode (1, OUTPUT);
  pinMode (0, INPUT); //0
  pinMode (2, INPUT);
  EEPROM.read (1);
  digitalWrite(1, HIGH);
  delay(500);
  digitalWrite(1, LOW);
}
void loop() {
  Vo = analogRead(2); //2
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  Tc = T - 273.15;
  DigiKeyboard.println(Tc);
  settemp = EEPROM.read(1);
  if
  (digitalRead(0) == HIGH ) //0
  {
    settemp = Tc;
    EEPROM.write (1, settemp);
    digitalWrite(1, HIGH);
    delay(500);
    digitalWrite(1, LOW);
    DigiKeyboard.println(" set; ");
  }
  else
  {
  }
  if
  ( (Tc > settemp - 5) && (counter < 1) )
  {
    digitalWrite(1, HIGH);
    delay(250);
    digitalWrite(1, LOW);
    DigiKeyboard.println(" test; ");
    counter++;
  }
  else
  {
  }
  if
  (Tc > settemp + 10)
  {
    digitalWrite(1, HIGH);
    DigiKeyboard.println(" alarm; ");
  }
  else
  {
    digitalWrite(1, LOW);
  }
  delay (500);
}

some examples of the temp diff(+/-):
tester ATtiny
15 7.68
30 18.2
50 28.8
60 33.5

so as you can see ‘test’ would be about 24deg and ‘alarm’ 50deg if ‘settemp’ was @ 30deg
that’s a difference of +/- 25deg between ‘test’ and ‘alarm’ and it should be 15deg

here is a pic of how it is connected:

ATtiny85.jpg

  if
  (digitalRead(0) == HIGH ) //0
  {

W
h
y

i
s

o
n
e

s
t
a
t
e
m
e
n
t

n
o
t

o
n

o
n
e

l
i
n
e
?

What value does that useless comment add?

Why are you using an absolute path for the EEPROM header file?

Why do you have useless (empty) else blocks? You are NOT required to have an else statement/block if there is nothing to do.

Your code layout is just too damned difficult for me to follow.

  1. for a start, I am a noob! a copy and paste expert! with some logical ability to make some codes from different projects work for me.

  2. What value does that useless comment add?

(digitalRead(0) == HIGH )
refers to the switch on ‘p0’ if you press it, it will read the current temp and save it to the eeprom as the ‘settemp’

  1. Why are you using an absolute path for the EEPROM header file?

if you are referring to

#include <C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM\EEPROM.h>

it is the only way I could get it into the sketch
#include "EEPROM.h"

did not work (cant remember why)

  1. Why do you have useless (empty) else blocks? You are NOT required to have an else statement/block if there is nothing to do.

thank you. I can remove it then.

  1. Your code layout is just too damned difficult for me to follow.

I am sorry, I did try my best

PaulS:
…..

I hope I could answer some of your questions.
and i will try make things a bit clearer.

float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

These values are characteristics of the thermistor. Did you get them from the thermistor supplier, calculate them yourself, or copy them from some place?

johnwasser:
or copy them from some place?

hi thanx. yip, its a copy and paste :wink:

here is the code with some explanation

//#include <C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM\EEPROM.h> (for me)
#include "EEPROM.h"   
#include "DigiKeyboard.h"      // for debug (no serial.print on ATtiny85)
int counter = 0;               // ***** dont remember could be a problem? maybe it should be "int counter = 2;" *****
int Vo;                                                                   // *** code i copied for the thermistor ***
float R1 = 10000;                                                         // *** code i copied for the thermistor ***
float logR2, R2, T, Tc;                                                   // *** code i copied for the thermistor ***
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;   // *** code i copied for the thermistor ***
float settemp;                // make a variable

void setup() {
  pinMode (1, OUTPUT);        // led
  pinMode (0, INPUT);         // switch to set current temp to eeprom "settemp"
  pinMode (2, INPUT);         // thermistor ON ATtiny85 PIN4!                                                     ***
  EEPROM.read (1);            // make the eeprom or atmega328 memory address 1
  digitalWrite(1, HIGH);      // flash once (ready)
  delay(500);
  digitalWrite(1, LOW);
}
void loop() {
  Vo = analogRead(2);                                                     // *** code i copied for the thermistor ***
  R2 = R1 * (1023.0 / (float)Vo - 1.0);                                   // *** code i copied for the thermistor ***
  logR2 = log(R2);                                                        // *** code i copied for the thermistor ***
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));             // *** code i copied for the thermistor ***
  Tc = T - 273.15;                                                        // *** code i copied for the thermistor ***
  DigiKeyboard.println(Tc);                    // (serial.print 'current temp' for ATtiny85)
  settemp = EEPROM.read(1);                    // read the settemp on the eeprom
  if                                           // chk settemp swtch
  (digitalRead(0) == HIGH )
  {
    settemp = Tc;                              // set current temp to eeprom
    EEPROM.write (1, settemp);
    digitalWrite(1, HIGH);                     // flash once (current temp set to eeprom)
    delay(500);
    digitalWrite(1, LOW);
    DigiKeyboard.println(" set; ");           // (serial.print when 'settemp to eeprom' for ATtiny85)
  }
  else
  {                                           // other wise do nothing
  }
  if
  ( (Tc > settemp - 5) && (counter < 1) )      // if settemp minus 5deg
  {
    digitalWrite(1, HIGH);                    // flash once only (when 'settemp minus 5deg')
    delay(250);
    digitalWrite(1, LOW);
    DigiKeyboard.println(" test; ");          // (serial.print when 'settemp minus 5deg' for ATtiny85)
    counter++;
  }
  else
  {                                          // other wise do nothing
  }
  if
  (Tc > settemp + 10)                        // if settemp plus 10deg
  {
    digitalWrite(1, HIGH);                   // turn on the led
    DigiKeyboard.println(" alarm; ");        // (serial.print when 'settemp plus 10deg' for ATtiny85)
  }
  else                                       // if that doesn't happen, then turn the led off
  {
    digitalWrite(1, LOW);                    // led on (when 'settemp plus 10deg')
  }
  delay (500);                              // wait 250 milliseconds
}

and here is the code that I copied for the thermistor

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

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

void loop() {

  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;
  Tf = (Tc * 9.0)/ 5.0 + 32.0; 

  Serial.print("Temperature: "); 
  Serial.print(Tf);
  Serial.print(" F; ");
  Serial.print(Tc);
  Serial.println(" C");   

  delay(500);
}

How accurate is this Thermistor code. by the looks of it, many people use it.
I am using a 10k Thermistor and resistor.

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

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

void loop() {

  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;
  Tf = (Tc * 9.0)/ 5.0 + 32.0; 

  Serial.print("Temperature: "); 
  Serial.print(Tf);
  Serial.print(" F; ");
  Serial.print(Tc);
  Serial.println(" C");   

  delay(500);
}

Hi,
Have you tried it?

That is the only way you will know, ice water is 0C and boiling is 100C.

Tom... :slight_smile:

TomGeorge:
ice water is 0C and boiling is 100C

That is not necessarily the truth; Water will boil at 85C if altitude is ~4KM above sea level :slight_smile:

Danois90:
That is not necessarily the truth; Water will boil at 85C if altitude is ~4KM above sea level :slight_smile:

The OP hasn't said if he/she is halfway up Mount Everest..... :slight_smile:
You can't make a decent cup of tea at the summit unless you use a pressure cooker.

Tom... :slight_smile:

TomGeorge:
Hi,
Have you tried it?

yes I have. but it does not look good. I will post some data soon

Danois90:
Instead of mocking what's wrong, teach what's right!

I like this

Water boiled at about 80C :wink:

my test got interrupted hence the two styles
(there is an discrepancy between the two tests @(21deg and 50deg) but i am not to worried about that.
the problem i have is the difference between my tester and T(C)

Thermistor temperature measurement: @1deg

Vo R2 T (C)
190 43842.10 -9.85
189 44126.98 -9.99

Thermistor temperature measurement: @10deg

Vo R2 T (C)
274 27335.77 0.50
275 27200.00 0.61
276 27065.22 0.72

Thermistor temperature measurement: @21deg------

Vo R2 T (C)
345 19652.17 8.10
344 19738.37 7.99

Thermistor temperature measurement: @25deg

Vo R2 T (C)
377 17135.28 11.34
377 17135.28 11.34

Thermistor temperature measurement: @50deg

Vo R2 T (C)
499 10501.00 23.43
494 10708.50 22.93
493 10750.51 22.83
492 10792.68 22.73

Thermistor temperature measurement: @80deg

Vo R2 T (C)
584 7517.12 32.15
583 7547.17 32.04
581 7607.57 31.83
582 7577.32 31.93


Thermistor temperature measurement:
Vo R2 T (C)

549 8633.88 28.49
548 8667.88 28.38
547 8702.01 28.28 @70deg
546 8736.26 28.18
545 8770.64 28.08

517 9787.23 25.23
516 9825.58 25.13 @60deg
515 9864.08 25.03

480 11312.50 21.54
479 11356.99 21.44 @50deg
478 11401.67 21.35
477 11446.54 21.25

438 13356.16 17.40 @40deg
439 13302.96 17.50
440 13250.00 17.59

392 16096.94 12.84
388 16365.98 12.44
391 16163.68 12.74 @30deg
392 16096.94 12.84

337 20356.08 7.27
336 20446.43 7.17
335 20537.32 7.06 @21deg
334 20628.75 6.96

330 21000.00 6.54
329 21094.23 6.44 @20deg
328 21189.03 6.33

here is the code i used

#include "DigiKeyboard.h"
int counter = 0;
int ThermistorPin = A2;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

void setup() {
  pinMode (2, INPUT);

  DigiKeyboard.println("Thermistor temperature measurement:"); 
  DigiKeyboard.println("\n Vo Rt T (C)");

}
void loop() {
  Vo = analogRead(ThermistorPin); //2
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  Tc = T - 273.15;

  DigiKeyboard.print(" "); DigiKeyboard.print(Vo); 
  DigiKeyboard.print(" "); DigiKeyboard.print(R2); 
  DigiKeyboard.print(" "); DigiKeyboard.println(Tc); 

  delay (500);
}

You probably need to replace the following constants in the thermistor response approximation with ones more suitable to your thermistor:

float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

Calculate them by using your multimeter to measure the resistance of the thermistor at some known temperatures, and enter them into this calculator: SRS Thermistor Calculator

The results are reasonably accurate only over a limited range of temperatures.

Hi,
Do you have a part number for your thermistor, or can you post a link to where you purchased it.

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Do you have a part number for your thermistor, or can you post a link to where you purchased it.

jremington:
Calculate them by using your multimeter to measure the resistance of the thermistor at some known temperatures, and enter them into this calculator:

this is what I have I both are 10k (I hope it helps) and how I connected them.

thF7V3EYX4.jpg

th.jpg

thTGTMI7W8.jpg

float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

Where do these numbers come from?

The standard Arduino playground tutorial for 10K NTC thermistors Arduino Playground - Thermistor2 suggests the following parameters as surprisingly "universal".

Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08

cattledog:
Where do these numbers come from?

The standard Arduino playground tutorial for 10K NTC thermistors suggests the following parameters as surprisingly "universal".

thnx will try

@12.3deg
I had... 'c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;'

Thermistor temperature measurement:

Vo R2 T (C)
278 26798.56 0.95
278 26798.56 0.95
278 26798.56 0.95
278 26798.56 0.95
278 26798.56 0.95

and with... 'A = 0.001129148, B = 0.000234125 and C = 8.76741E-08'

Thermistor temperature measurement:

Vo R2 T (C)
278 26798.56 3.92
278 26798.56 3.92
278 26798.56 3.92
278 26798.56 3.92
278 26798.56 3.92

With the correct numbers, you would do better.