Last active
February 2, 2024 20:30
-
-
Save kokospalme/4960c5ce5e0e49d04aefdaf6f2341560 to your computer and use it in GitHub Desktop.
STM8 NTC example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
STM8 NTC example | |
*/ | |
#include <Serial.h> | |
#define MAX_ANALOG_VALUE 1023 | |
#define TEMP_PIN 6 //A0 | |
#define THERMISTORNOMINAL 10000 // resistance at 25 degrees C [ohm] | |
#define TEMPERATURENOMINAL 25 // temp. for nominal resistance (almost always 25 C) | |
#define NUMSAMPLES 10 //sampling | |
#define BCOEFFICIENT 3950 // The beta coefficient of the thermistor (usually 3000-4000) | |
#define SERIESRESISTOR 10000 // the value of the fixed resistor [ohm] | |
#define NTCTOVCC true //false: NTC to GND, fixed to VCC, true: NTC to vcc, fixed to GND | |
int samples[NUMSAMPLES]; | |
float average; | |
float my_log(float x) { | |
if (x <= 0) { | |
// Logarithmus ist für nicht-positive Zahlen nicht definiert | |
// printf("Ungültige Eingabe\n"); | |
return 0.0f; // Oder eine spezielle Rückgabewert für Fehlerbehandlung | |
} | |
if (x < 1) { | |
// Wenn x zwischen 0 und 1 liegt, benutze die Beziehung ln(x) = -ln(1/x) | |
x = 1 / x; | |
return -my_log(x); | |
} | |
float result = 0.0f; | |
float term = (x - 1) / (x + 1); | |
float term_squared = term * term; | |
float numerator = term; | |
int n = 1; | |
while (n <= 100) { | |
result += numerator / n; | |
numerator *= term_squared; | |
n += 2; | |
} | |
return 2 * result; | |
} | |
float roundToTenth(float num) { | |
float rounded = (int)(num * 10 + 0.5) / 10.0; | |
return rounded; | |
} | |
void setup() { | |
Serial_begin(115200); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
for (int i=0; i< NUMSAMPLES; i++) {// take N samples in a row, with a slight delay | |
samples[i] = analogRead(TEMP_PIN); | |
// delay(10); | |
} | |
// average all the samples out | |
average = 0; | |
for (int i=0; i< NUMSAMPLES; i++) { | |
average += samples[i]; | |
} | |
average /= NUMSAMPLES; | |
// Serial_print_s("Average analog reading "); | |
// Serial_print_f(average); | |
// Serial_println(); | |
// convert the value to resistance | |
if(NTCTOVCC)average = MAX_ANALOG_VALUE - average; | |
average = MAX_ANALOG_VALUE / average - 1; | |
average = SERIESRESISTOR / average; | |
// Serial_print_s("Thermistor resistance "); | |
// Serial_print_f(average); | |
// Serial_println(); | |
float steinhart; | |
steinhart = average / THERMISTORNOMINAL; // (R/Ro) | |
steinhart = my_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 | |
steinhart = roundToTenth(steinhart); //round to 0.1 | |
// Serial_print_s("Temperature "); | |
Serial_print_f(steinhart); | |
// Serial_print_s(" C"); | |
// Serial_println(); | |
Serial_println(); | |
delay(100); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment