Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.5 kB
0
Indexable
Never
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <string.h>
#include <math.h>

using namespace std;

//OLED Display
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
void displayDebugPrint(String text){
    display.clearDisplay();
    display.setTextColor(WHITE);
    display.setTextSize(2);
    display.setCursor(1,1);
    display.println(text);
    display.display();
}


// which analog pin to connect
#define THERMISTORPIN 26         
// resistance at 25 degrees C
#define THERMISTORNOMINAL 100000      
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25   
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 100000    

int samples[NUMSAMPLES];

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

    //OLED Display
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.display();
    delay(2000);
    display.clearDisplay();

}

void loop(){
    
    uint8_t i;
    float average;

    // take N samples in a row, with a slight delay
    for (i=0; i< NUMSAMPLES; i++) {
        samples[i] = analogRead(THERMISTORPIN);
        delay(10);
    }

    // average all the samples out
    average = 0;
    for (i=0; i< NUMSAMPLES; i++) {
        average += samples[i];
    }
    average /= NUMSAMPLES;

    Serial.print("Average analog reading "); 
    Serial.println(average);

    // convert the value to resistance
    average = 4095 / average - 1;
    average = SERIESRESISTOR / average;
    Serial.print("Thermistor resistance "); 
    Serial.println(average);

    float steinhart;
    steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
    steinhart = log(steinhart);                  // ln(R/Ro)
    steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
    steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
    steinhart = 1.0 / steinhart;                 // Invert
    steinhart -= 273.15;                         // convert absolute temp to C

    Serial.print("Temperature "); 
    Serial.print(steinhart);
    Serial.println(" *C");


    displayDebugPrint(std::to_string(steinhart).c_str());


    delay(1000);
}