SD Temperaturlogger

Für Arduino


/* Libraries */
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <EEPROM.h> // required for EEPROMAnything.h
#include "./EEPROMAnything.h"

/* Display Adresse und Connections setzen */
 LiquidCrystal_I2C lcd(0x3F, 20,4); // I2C adress, Cols, Rows

#include "./Timer.h"
Timer updateLCD(1*TIMER_SECONDS);
Timer storeValue(15*TIMER_SECONDS);
 
// Thermistor data
#define THERMISTORPIN A0        // which analog pin to connect      
#define THERMISTORNOMINAL 100000 // resistance at 25 degrees C
#define TEMPERATURENOMINAL 25   // temp. for nominal resistance (almost always 25 C)
#define NUMSAMPLES 5            // how many samples to take and average, more takes longer but is more 'smooth'
#define BCOEFFICIENT 3950       // The beta coefficient of the thermistor (usually 3000-4000)
#define SERIESRESISTOR 100000    // the value of the 'other' resistor
int samples[NUMSAMPLES];

int storePos = -1;

void setup() {
  lcd.begin(); //Display initialisieren
  lcd.backlight(); // backlight anlassen
  
  lcd.setCursor(0,0); //Zeilen und Spalten starten mit 0 -> Spalte 1 Zeile 1
  lcd.print("Temp.:");

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  if(updateLCD.check()){
    int tmp = Thermistor();

    if(storePos==-1){
      
    }else if(storePos>=0 && storeValue.check()){
      
    }

    lcd.setCursor(7,0); //Spalte 8, Zeile 1
    lcd.print("   ");// reset
    lcd.setCursor(7,0); //Spalte 8, Zeile 1
    lcd.print(tmp);
   
  }
  if(Serial.available()){
    if(Serial.read()=='t'){ // Speicher auslesen und uebertragen
      for(int i=0; i<EEPROM.length(); i++){
        Serial.print(EEPROM.read(i));
        Serial.print(" ");
      }
    }
  }
}

float Thermistor(void){
  uint8_t i;
  float temp;
  for (i=0; i< NUMSAMPLES; i++) { // take N samples in a row, with a slight delay
   samples[i] = analogRead(THERMISTORPIN);
   delay(10);
  }
  temp = 0; // average all the samples out
  for (i=0; i< NUMSAMPLES; i++) {
     temp += samples[i];
  }
  temp /= NUMSAMPLES;
  temp = 1023 / temp - 1; // convert the value to resistance
  temp = SERIESRESISTOR / temp; // now temp contains the resistor
  Serial.print("Resistor "); 
  Serial.print(temp);
  Serial.println(" Ohm");
  temp = temp / THERMISTORNOMINAL;        // (R/Ro)
  temp = log(temp);                       // ln(R/Ro)
  temp /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  temp += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  temp = 1.0 / temp;                      // Invert
  temp -= 273.15;                         // convert to C
  return temp;
}

Die loop-Funktion wird kontinuierlich ausgeführt. Wenn der Timer "updateLCD" abgelaufen ist, wird die Temperatur mit der Funktion "Thermistor()" gemessen und auf dem LCD-Display angezeigt. Bei entsprechenden Bedingungen werden die Werte zur Speicherung vorbereitet. Über die serielle Kommunikation kann mit Kommando 't'
/* Libraries */
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <EEPROM.h> // required for EEPROMAnything.h
#include "./EEPROMAnything.h"

/* Display Adresse und Connections setzen */
 LiquidCrystal_I2C lcd(0x3F, 20,4); // I2C adress, Cols, Rows

#include "./Timer.h"
Timer updateLCD(1*TIMER_SECONDS);
Timer storeValue(15*TIMER_SECONDS);
 
// Thermistor data
#define THERMISTORPIN A0        // which analog pin to connect      
#define THERMISTORNOMINAL 100000 // resistance at 25 degrees C
#define TEMPERATURENOMINAL 25   // temp. for nominal resistance (almost always 25 C)
#define NUMSAMPLES 5            // how many samples to take and average, more takes longer but is more 'smooth'
#define BCOEFFICIENT 3950       // The beta coefficient of the thermistor (usually 3000-4000)
#define SERIESRESISTOR 100000    // the value of the 'other' resistor
int samples[NUMSAMPLES];

int storePos = -1;

void setup() {
  lcd.begin(); //Display initialisieren
  lcd.backlight(); // backlight anlassen
  
  lcd.setCursor(0,0); //Zeilen und Spalten starten mit 0 -> Spalte 1 Zeile 1
  lcd.print("Temp.:");

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  if(updateLCD.check()){
    int tmp = Thermistor();

    if(storePos==-1){
      
    }else if(storePos>=0 && storeValue.check()){
      
    }

    lcd.setCursor(7,0); //Spalte 8, Zeile 1
    lcd.print("   ");// reset
    lcd.setCursor(7,0); //Spalte 8, Zeile 1
    lcd.print(tmp);
   
  }
  if(Serial.available()){
    if(Serial.read()=='t'){ // Speicher auslesen und uebertragen
      for(int i=0; i<EEPROM.length(); i++){
        Serial.print(EEPROM.read(i));
        Serial.print(" ");
      }
    }
  }
}

float Thermistor(void){
  uint8_t i;
  float temp;
  for (i=0; i< NUMSAMPLES; i++) { // take N samples in a row, with a slight delay
   samples[i] = analogRead(THERMISTORPIN);
   delay(10);
  }
  temp = 0; // average all the samples out
  for (i=0; i< NUMSAMPLES; i++) {
     temp += samples[i];
  }
  temp /= NUMSAMPLES;
  temp = 1023 / temp - 1; // convert the value to resistance
  temp = SERIESRESISTOR / temp; // now temp contains the resistor
  Serial.print("Resistor "); 
  Serial.print(temp);
  Serial.println(" Ohm");
  temp = temp / THERMISTORNOMINAL;        // (R/Ro)
  temp = log(temp);                       // ln(R/Ro)
  temp /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  temp += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  temp = 1.0 / temp;                      // Invert
  temp -= 273.15;                         // convert to C
  return temp;
}
Die Funktion "Thermistor" ist verantwortlich für die Messung und Berechnung der Temperatur basierend auf den Analogsamples des Thermistors. Zunächst wird eine Schleife verwendet, um eine festgelegte Anzahl von Samples einzulesen und im Array "samples" zu speichern. Anschließend werden die Samples gemittelt und die entsprechende Widerstandswert des Thermistors berechnet. Die Temperatur wird anhand der Widerstandswerte und der definierten Konstanten berechnet. Die berechnete Temperatur wird sowohl über die serielle Schnittstelle ausgegeben als auch zurückgegeben, um sie in der Hauptfunktion anzuzeigen.
float Thermistor(void) {
  uint8_t i;
  float temp;
  for (i = 0; i < NUMSAMPLES; i++) {
    samples[i] = analogRead(THERMISTORPIN);
    delay(10);
  }
  temp = 0;
  for (i = 0; i < NUMSAMPLES; i++) {
    temp += samples[i];
  }
  temp /= NUMSAMPLES;
  temp = 1023 / temp - 1;
  temp = SERIESRESISTOR / temp;
  Serial.print("Resistor ");
  Serial.print(temp);
  Serial.println(" Ohm");
  temp = temp / THERMISTORNOMINAL;
  temp = log(temp);
  temp /= BCOEFFICIENT;
  temp += 1.0 / (TEMPERATURENOMINAL + 273.15);
  temp = 1.0 / temp;
  temp -= 273.15;
  return temp;
}
Sie können den vollständigen Programmcode des "SD Temperaturloggers" herunterladen, indem Sie auf den folgenden Link klicken: Download SD Temperaturlogger




Weitere Artikel

© Veit Götz 2012 - 2024
Impressum
Datenschutz
Statistiken
Spenden