Temperatuur sensor omrekenen naar graden Celsius

Projecten die niet passen in bovenstaande onderwerpen
Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor nicoverduin » 01 Sep 2016, 16:34

DeDraak schreef:Is hier trouwens een Library van te maken?

Kan altijd....
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Advertisement

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor Koepel » 01 Sep 2016, 19:50

De SteinhartHart() functie is 4 regels code, daar ga ik geen library van maken. En nu worden de A, B, C getallen gewoon als constante in de sketch gezet. Dat zou dan ook nog bij de library gemaakt moeten worden.

Hoe heten je probes ?
"Een" en "Twee", of "Pork" en "Beef" ? Misschien is "A" en "B" wel zo handig.

Als de beide 22k weerstanden nauwkeurig zijn, dan kun je volstaan met één 'series_resistor' variabele.

De probes pins naar zoiets:
Code: Alles selecteren
// Pins
const int probeAPin = A0;
const int probeBPin = A1;


Je kunt dezelfde variabelen gebruiken, of beide analoge ingangen in een loop, of alles apart. Ik geef de voorkeur om nu afzonderlijke variabelen te gebruiken, zodat je alle gegevens naast elkaar hebt.
Code: Alles selecteren
  float adcA = getADC( probeAPin);
  float adcB = getADC( probeBPin);

  float rA = series_resistor / ((1024.0 / (adcA + 0.5)) - 1.0);
  float rB = series_resistor / ((1024.0 / (adcB + 0.5)) - 1.0);

  float tA = SteinhartHart( rA);
  float tB = SteinhartHart( rB);


Kun je zelf er een werkende sketch van maken ? Als het niet lukt, dan graag laten zien wat je hebt.

Berichten: 247
Geregistreerd: 03 Okt 2015, 13:39

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor DeDraak » 02 Sep 2016, 14:13

De probes gaan Food en Dome heten, maar dat maakt voor nu niks uit.
Ik heb geprobeerd wat je zei maar ik krijg een foutmelding, hierbij de aangepaste versie:

Code: Alles selecteren
// A sketch for a ET73
//
// 18 august 2016, by Koepel, Public Domain / WTFPL license.
//    Version 1.0, first version for two possible series resistors.
//    For: http://arduinoforum.nl/viewtopic.php?f=25&t=1958
//    Tested with Arduino Uno and Arduino.cc IDE 1.6.11
// 31 august 2016
//    Version 2.0, just one 22k series resistor. Added averaging.
//    Resistor of 22k to 5V and A0, probe to GND and A0.
//    Not using the 3.3V and not using AREF.
//
//
//
//

// Calculate the resistance of the NTC thermistor.
// -----------------------------------------------
// With a 5V Arduino board, the voltage at a pin is:
//    V = ( adc + 0.5 ) / 1024 * 5V
// Half a bit is added, because the ADC goes to the next step if the voltage is at the next step.
// The average voltage is in the middle of that step.
// There are 1024 steps, so it must be divided by 1024 and not 1023.
// This means that an analog input can never measure exactly 5.0V, and that is indeed
// how the ADC works, because 5.0V would be the 1025-th step.
//
// When a series resistor is connected to 5V, and the NTC to GND, it is:
//   V = Rntc / (Rntc + Rseries) * 5V
// Combining them together:
//   ( adc + 0.5 ) / 1024 * 5V = Rntc / (Rntc + Rseries) * 5V
//   Rntc = Rseries / ((1024 / (adc + 0.5)) - 1)
//   


// Measured values
const float measured_T[] =
  { 0,      20,     40,     60,    80,    100,   120,    140,    160,     180,     200,    220, 240, 250 };
const float measured_R[] =
  { 750E+3, 300E+3, 120E+3, 52E+3, 25E+3, 13E+3, 7.5E+3, 4.1E+3, 2.48E+3, 1.56E+3, 1.0E+3, 680, 480, 400 };


// Using: http://www.thinksrs.com/downloads/programs/Therm%20Calc/NTCCalibrator/NTCcalculator.htm
// With the measured values at 0, 100, 250 degrees:
//    A = 0.6742E-3, B = 2.030E-4, C = 0.9711E-7, beta = 4133K
const float A = 0.6742E-3;
const float B = 2.030E-4;
const float C = 0.9711E-7;
const float beta = 4133.22;   // This is the 'beta', not the 'B'.

// The resistance at 25 degrees is also taken from that thinksrs page.
const float ntc_nominal_T = 25.0;
const float ntc_nominal_R = 210876.87;        // It is probably a 210k NTC resistor

// Pins
const int probeAPin = A0;
const int probeBPin = A1;

// Values for the series resistor to 5V
const float series_resistor = 22E+3;      // 22k


void setup()
{
  Serial.begin( 9600);
 
  Serial.println();
  Serial.println(F( "\nET-73"));
  Serial.println(F( "-------"));

  for( int i=0; i<sizeof(measured_T) / sizeof(float); i++)
  {
    // Calculate what the ADC value would be with this resistor value.
    // Simulate the ADC, by rounding to lower integer with floor().
    float adc = floor( measured_R[i] / (measured_R[i] + series_resistor) * 1024.0);

    // Calculate the resistance value of the NTC thermistor
    float r  = series_resistor / ((1024.0 / (adc + 0.5)) - 1.0);
   
 
    //Serial.print(F( ", Stein = "));
    //Serial.print( SteinhartHart( r));

    if( adc < 2 || adc > 1021)
      Serial.print(F( "  out of range"));

    Serial.println();
  }
  Serial.println();
}


void loop()
{
  // Measure the real temperature.
 
   

  // Calculate the resistance value of the NTC thermistor
  float adcA = getADC( probeAPin);
  float adcB = getADC( probeBPin);
 
  float rA  = series_resistor / ((1024.0 / (adcA + 0.5)) - 1.0);
  float rB  = series_resistor / ((1024.0 / (adcB + 0.5)) - 1.0);
  //Serial.print(F( "R = "));
  //Serial.print( r);
  Serial.print(F( "Stein = "));
  Serial.print( SteinhartHart( rA));
 
  if( adcA < 2.0 || adcA > 1021.0)
    Serial.print(F( "  out of range"));

  Serial.println();

 
  delay( 5000);
}


// getADC
// ------
// This function returns the adc value as a float.
// It is ment for the ATmega chips with 10-bits ADC.
// Because the average is used, a little noise could increase
// the number of bits. Therefor a float number is returned.
//
float getADC( int pin)
{
  // About 60 samples fit into a unsigned int.
  // Therefor the 'n' should be not above 60.
  int n = 20;                      // maximum 60
  unsigned int total = 0;         
  for( int i=0; i<n; i++)         
  {
    total += analogRead( pin);
  }
  float adc = (float) total / (float) n;
  return( adc);
}


// SteinhartHart
// -------------
// Calculation with Steinhart-Hart to get the temperature of a NTC thermistor.
// This calculation does not use the simplified Beta,
// but the A, B, C numbers.
// The function "log()" in 'c' or 'c++' is the natural logarithm.
float tA = SteinhartHart( rA);
float tB = SteinhartHart( rB);
{
  float t1 = log( r);
  float t2 = C * t1 * t1 * t1;
  float t3 = A + (B * log( r)) + t2;
  return( (1.0 / t3) - 273.15);
}

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor Koepel » 02 Sep 2016, 14:33

De SteinhartHart() functie is nu geen functie meer. Je hoeft daar niets aan te veranderen. Die functie kan aangeroepen worden voor beide probes.

Kun je die functie weer maken zoals het was ? Het gaat dus om die eerste regel met "float SteinhartHart( float r)".
Code: Alles selecteren
float SteinhartHart( float r)
{
  float t1 = log( r);
  float t2 = C * t1 * t1 * t1;
  float t3 = A + (B * log( r)) + t2;
  return( (1.0 / t3) - 273.15);
}


Uitleg:
float SteinhartHart( float r)
blauw 'float' : Dat is het type dat de functie terug geeft. Het definieert de return waarde.
rood 'SteinhartHart' : Dat is de naam van de functie die gedefinieerd gaat worden.
groen 'float' : Het type van de parameter.
orange 'r' : Een zelfgekozen naam voor de parameter die in de functie gebruikt gaat worden.


Daarna sketch nog een wat aanpassen zodat je beide temperaturen ziet.

Het aanroepen van die functie gebeurt in de sketch in de loop(), nadat je rA en rB hebt berekend.
[code]
float tA = SteinhartHart( rA);
float tB = SteinhartHart( rB);
[/float]

Berichten: 247
Geregistreerd: 03 Okt 2015, 13:39

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor DeDraak » 02 Sep 2016, 14:49

Nu is het gelukt inderdaad, heb gelijk de benamingen aangepast!


Code: Alles selecteren
// A sketch for a ET73
//
// 18 august 2016, by Koepel, Public Domain / WTFPL license.
//    Version 1.0, first version for two possible series resistors.
//    For: http://arduinoforum.nl/viewtopic.php?f=25&t=1958
//    Tested with Arduino Uno and Arduino.cc IDE 1.6.11
// 31 august 2016
//    Version 2.0, just one 22k series resistor. Added averaging.
//    Resistor of 22k to 5V and A0, probe to GND and A0.
//    Not using the 3.3V and not using AREF.
//
//
//
//

// Calculate the resistance of the NTC thermistor.
// -----------------------------------------------
// With a 5V Arduino board, the voltage at a pin is:
//    V = ( adc + 0.5 ) / 1024 * 5V
// Half a bit is added, because the ADC goes to the next step if the voltage is at the next step.
// The average voltage is in the middle of that step.
// There are 1024 steps, so it must be divided by 1024 and not 1023.
// This means that an analog input can never measure exactly 5.0V, and that is indeed
// how the ADC works, because 5.0V would be the 1025-th step.
//
// When a series resistor is connected to 5V, and the NTC to GND, it is:
//   V = Rntc / (Rntc + Rseries) * 5V
// Combining them together:
//   ( adc + 0.5 ) / 1024 * 5V = Rntc / (Rntc + Rseries) * 5V
//   Rntc = Rseries / ((1024 / (adc + 0.5)) - 1)
//


// Measured values
const float measured_T[] =
{ 0,      20,     40,     60,    80,    100,   120,    140,    160,     180,     200,    220, 240, 250 };
const float measured_R[] =
{ 750E+3, 300E+3, 120E+3, 52E+3, 25E+3, 13E+3, 7.5E+3, 4.1E+3, 2.48E+3, 1.56E+3, 1.0E+3, 680, 480, 400 };


// Using: http://www.thinksrs.com/downloads/programs/Therm%20Calc/NTCCalibrator/NTCcalculator.htm
// With the measured values at 0, 100, 250 degrees:
//    A = 0.6742E-3, B = 2.030E-4, C = 0.9711E-7, beta = 4133K
const float A = 0.6742E-3;
const float B = 2.030E-4;
const float C = 0.9711E-7;
const float beta = 4133.22;   // This is the 'beta', not the 'B'.

// The resistance at 25 degrees is also taken from that thinksrs page.
const float ntc_nominal_T = 25.0;
const float ntc_nominal_R = 210876.87;        // It is probably a 210k NTC resistor

// Pins
const int probeFoodPin = A0;
const int probeDomePin = A1;

// Values for the series resistor to 5V
const float series_resistor = 22E+3;      // 22k


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

  Serial.println();
  Serial.println(F( "\nET-73"));
  Serial.println(F( "-------"));

  for ( int i = 0; i < sizeof(measured_T) / sizeof(float); i++)
  {
    // Calculate what the ADC value would be with this resistor value.
    // Simulate the ADC, by rounding to lower integer with floor().
    float adc = floor( measured_R[i] / (measured_R[i] + series_resistor) * 1024.0);

    // Calculate the resistance value of the NTC thermistor
    float r  = series_resistor / ((1024.0 / (adc + 0.5)) - 1.0);

    if ( adc < 2 || adc > 1021)
      Serial.print(F( "  out of range"));

   //Serial.println();
  }
  Serial.println();
}


void loop()
{
  // Measure the real temperature.



  // Calculate the resistance value of the NTC thermistor
  float adcFood = getADC( probeFoodPin);
  float adcDome = getADC( probeDomePin);

  float rFood  = series_resistor / ((1024.0 / (adcFood + 0.5)) - 1.0);
  float rDome  = series_resistor / ((1024.0 / (adcDome + 0.5)) - 1.0);
  //Serial.print(F( "R = "));
  //Serial.print( r);
  Serial.print(F( "Food = "));
  Serial.print( SteinhartHart( rFood));
 

  if ( adcFood < 2.0 || adcFood > 1021.0)
    Serial.print(F( "  No Food probe"));
   
Serial.print(F( ", Dome = "));
  Serial.print( SteinhartHart( rDome));
    if ( adcDome < 2.0 || adcDome > 1021.0)
    Serial.print(F( "  No Dome probe"));

  Serial.println();


  delay( 5000);
}


// getADC
// ------
// This function returns the adc value as a float.
// It is ment for the ATmega chips with 10-bits ADC.
// Because the average is used, a little noise could increase
// the number of bits. Therefor a float number is returned.
//
float getADC( int pin)
{
  // About 60 samples fit into a unsigned int.
  // Therefor the 'n' should be not above 60.
  int n = 20;                      // maximum 60
  unsigned int total = 0;
  for ( int i = 0; i < n; i++)
  {
    total += analogRead( pin);
  }
  float adc = (float) total / (float) n;
  return ( adc);
}


// SteinhartHart
// -------------
// Calculation with Steinhart-Hart to get the temperature of a NTC thermistor.
// This calculation does not use the simplified Beta,
// but the A, B, C numbers.
// The function "log()" in 'c' or 'c++' is the natural logarithm.
float SteinhartHart( float r)
{
  float t1 = log( r);
  float t2 = C * t1 * t1 * t1;
  float t3 = A + (B * log( r)) + t2;
  return ( (1.0 / t3) - 273.15);
}

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor Koepel » 02 Sep 2016, 15:26

Mooi ! :D

Berichten: 247
Geregistreerd: 03 Okt 2015, 13:39

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor DeDraak » 03 Sep 2016, 17:12

Ik heb een lcd 1602 I2c display toegevoegd, dit werkt ook prima ik heb wel alle informatie in de sketch even verwijderd om het wat overzichtelijker te maken..
Nu heb ik nog een ESP8266 module liggen en wil ik gaan proberen om deze 2 temperaturen in een soort webserver te zetten, is dat überhaupt mogelijk?

Code: Alles selecteren
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// Measured values
const float measured_T[] =
{ 0,      20,     40,     60,    80,    100,   120,    140,    160,     180,     200,    220, 240, 250 };
const float measured_R[] =
{ 750E+3, 300E+3, 120E+3, 52E+3, 25E+3, 13E+3, 7.5E+3, 4.1E+3, 2.48E+3, 1.56E+3, 1.0E+3, 680, 480, 400 };

const float A = 0.6742E-3;
const float B = 2.030E-4;
const float C = 0.9711E-7;
const float beta = 4133.22;   // This is the 'beta', not the 'B'.

// The resistance at 25 degrees is also taken from that thinksrs page.
const float ntc_nominal_T = 25.0;
const float ntc_nominal_R = 210876.87;        // It is probably a 210k NTC resistor

// Pins
const int probeFoodPin = A0;
const int probeDomePin = A1;

// Values for the series resistor to 5V
const float series_resistor = 22E+3;      // 22k

void setup()
{
  lcd.begin (16, 2);
  lcd.setCursor(0, 0);
  lcd.print(F( "   Pitmaster"));
  //lcd.setCursor(0, 1);
  //lcd.print(F( "  "));
  delay(10000);
  lcd.clear ();

  Serial.begin( 9600);
  Serial.println();
  Serial.println(F( "\nET-73"));
  Serial.println(F( "-------"));


  for ( int i = 0; i < sizeof(measured_T) / sizeof(float); i++)
  {
    // Calculate what the ADC value would be with this resistor value.
    // Simulate the ADC, by rounding to lower integer with floor().
    float adc = floor( measured_R[i] / (measured_R[i] + series_resistor) * 1024.0);

    // Calculate the resistance value of the NTC thermistor
    float r  = series_resistor / ((1024.0 / (adc + 0.5)) - 1.0);

    if ( adc < 2 || adc > 1021)
      Serial.print(F( "  out of range"));

    //Serial.println();
  }
  Serial.println();
}


void loop()
{

  // Measure the real temperature.

  // Calculate the resistance value of the NTC thermistor
  float adcFood = getADC( probeFoodPin);
  float adcDome = getADC( probeDomePin);

  float rFood  = series_resistor / ((1024.0 / (adcFood + 0.5)) - 1.0);
  float rDome  = series_resistor / ((1024.0 / (adcDome + 0.5)) - 1.0);
  //Serial.print(F( "R = "));
  //Serial.print( r);
  Serial.print(F( "Food = "));
  Serial.print( SteinhartHart( rFood));


  if ( adcFood < 2.0 || adcFood > 1021.0)
    Serial.print(F( "  No Food probe"));

  Serial.print(F( ", Dome = "));
  Serial.print( SteinhartHart( rDome));
  if ( adcDome < 2.0 || adcDome > 1021.0)
    Serial.print(F( "  No Dome probe"));

  Serial.println();

  lcd.setBacklight(HIGH);
  lcd.setCursor(0, 0);
  lcd.print(F( "Dome= "));
  lcd.print(SteinhartHart(rDome));                       //("Hello World");
  lcd.setCursor(0, 1);
  lcd.print(F( "Food= "));
  lcd.print(SteinhartHart(rFood));
  delay(1000);


}

float getADC( int pin)
{
  // About 60 samples fit into a unsigned int.
  // Therefor the 'n' should be not above 60.
  int n = 20;                      // maximum 60
  unsigned int total = 0;
  for ( int i = 0; i < n; i++)
  {
    total += analogRead( pin);
  }
  float adc = (float) total / (float) n;
  return ( adc);
}

float SteinhartHart( float r)
{
  float t1 = log( r);
  float t2 = C * t1 * t1 * t1;
  float t3 = A + (B * log( r)) + t2;
  return ( (1.0 / t3) - 273.15);
}


Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor Koepel » 03 Sep 2016, 19:26

Er zijn meerdere versies van de ESP8266, en de software is niet volledig compatible.
Je kunt het proberen. Ik heb zelf een ESP8266 liggen, maar nog niets mee gedaan :(

Dat commentaar is belangrijker dan de code zelf. Dus je had beter de code regels kunnen verwijderen en het commentaar laten staan :shock:

Bijvoorbeeld deze regel:
Code: Alles selecteren
const float A = 0.6742E-3;

De "0.6732E-3" is een magisch getal waarvan niet meer duidelijk is waar het vandaan komt. Ik ben van mening dat een sketch niet bruikbaar meer is als er ook maar één magisch getal in staat. De code is dan namelijk niet meer te onderhouden.

Maar ja, ik ben gewend om code te lezen, dus ik heb geen last van heel veel commentaar of heel veel defines en zo.

Berichten: 247
Geregistreerd: 03 Okt 2015, 13:39

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor DeDraak » 09 Aug 2017, 11:10

Ik gebruik de et73 nu al een tijdje voor mijn Uno en dat gaat goed. Wat moet ik veranderen als ik dit voor 3,3Volt wil gaan doen?
Voor bijvoorbeeld een pro mini


Code: Alles selecteren
// A sketch for a ET73
//
// 18 august 2016, by Koepel, Public Domain / WTFPL license.
//    Version 1.0, first version for two possible series resistors.
//    For: http://arduinoforum.nl/viewtopic.php?f=25&t=1958
//    Tested with Arduino Uno and Arduino.cc IDE 1.6.11
// 31 august 2016
//    Version 2.0, just one 22k series resistor. Added averaging.
//    Resistor of 22k to 5V and A0, probe to GND and A0.
//    Not using the 3.3V and not using AREF.
//
//

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Temperatuur sensor omrekenen naar graden Celsius

Berichtdoor Koepel » 09 Aug 2017, 12:42

Niets. Het voltage is al uit de berekening.

De NTC wordt berekend aan de hand van de serieweerstand. Stel dat de serieweerstand 22k is, en de Arduino meet een waarde van 512 met analogRead() die dus die precies in het midden zit, dan moet de NTC ook 22k zijn. De rest is de formule. Dus het voltage maakt niet meer uit.

VorigeVolgende

Terug naar Overige projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 11 gasten