Decoding an SMT172 temperature sensor

The project is using an SMT172 TO220 sensor to measure temperature.
Somewhere in my Pc I have a downloaded an SMT172 library but I don’t know where it resides and haven’t manage to incorporate it into the Arduino library in order to #include it into my sketch. What to do when being “hot”? Cool down… Yes…. I wrote a sketch according to sensor data but the result is some 10-12 degrees Celcius too high.
The sketch looks like:

//#include <SMT172.h>
#define temperature_pin 9
void setup() {
  // put your setup code here, to run once:
  pinMode(temperature_pin, INPUT_PULLUP);
  Serial.begin(9600);

}
float measure_8(void)
{
  int i;
  int high_count, low_count = 0;
  float DC_SUM = 0;
  float f_high, f_low;
  unsigned long start_us;

//get in sync
//if input is low, wait for high
  while (digitalRead(temperature_pin) == LOW)
  { }

//when input is high, wait for the negative flank giving a low
  while (digitalRead(temperature_pin) == HIGH)
  { }

//now a low flank appeared, pin is low
  for (i = 0; i < 8; i++)//sample across 8 periods
  {
    start_us = micros();
    while (digitalRead(temperature_pin) == LOW) {}
    low_count = (micros() - start_us);

    start_us = micros();
    while (digitalRead(temperature_pin) == HIGH) {}
    high_count = (micros() - start_us);

    f_high = high_count;
    f_low= low_count;
    DC_SUM += (f_high / (f_high + f_low));
    //    Serial.print(high_count); Serial.print(" "); Serial.print(low_count); Serial.print(" "); Serial.println(DC_SUM);
  }//next i
  Serial.print(f_high); Serial.print(" "); Serial.print(f_low); Serial.print(" "); Serial.print(DC_SUM);Serial.print(" ");
  return (DC_SUM / 8.0);
}
void loop()
{
  // put your main code here, to run repeatedly:
  float Celcius;
  float raw;

  raw = measure_8();// make one reading

  // where      DC = Valid Duty Cycle         T = Temperature in °C
  //  DC - 0.32 = 0.0047 * T
  // (DC - 0.32) / 0.0047 = T
  // T = (DC - 0.32) / 0.0047

  Celcius =   (raw - 0.32) / 0.0047;
  Serial.print(Celcius); Serial.println(" Celcius");
  delay(1000);
}

Anybody that can se the fault except from using the SMT172 library?

I think you should use a real library that uses real hardware timers, using micros() does not have sufficent resolution.

You are perfectly right. Using a proper library will most likely increase the accurazy. Using micros() is the maximum of my knowledge.
Opening the link You gave me I still don't know how to proceed. Too much that I don't understand.

Well, then there is no better time to learn than right now. Libraries are a key feature of Arduino and you needn’t be attempting recreating things from scratch that have already been created for you.

It’s not at all difficult and you probably know how to do each step individually, you just have to know the proper order and locations where to find and place things.

Please read over the instructions in the link below and post a question when and if you get stuck, include which operating system and version you’re running.

Thanks! I wonder if posts have been lost in this topic. An answer I read earlier as well as a reply from me is lost. Strange.
I answer again. I cleaned up user/documents/arduino from all SMT172 folders and tried again. -This time I managed to download and activate a good SMT172 library. Using the demo sketch made my project work nicely and precisely.

@WattsThat
Watching the temperature using Serial Plotter I sometimes get single spikes showing the double value of temperature. I started a new topic about this and got the advice to update the recently downloaded routines. Anything You can tell me here? I know, I'm asking the same question in 2 topics now, in case You don't read this other second topic..

Today I really cleaned up a number of folders, removing old SMT172 folders and files and got back to the download of SMT172_master and incorporated that lib into my Arduino lib. Using the demo file, making some cut downs of printouts and creating a fault finding version my project it has been running without spikes and sparks for several hours now. The value is also very stable. Here is the working code of now:

/*
  Demo sketch for the SMT172 library.
  This sketch is intended for the ATmega328P (Uno, Nano, Pro Mini etc.)
  There is no timer2 on the ATmega32U4 (Pro Micro)
  This sketch will output to serial.
  Connect the output of the SMT172 to pin 8 (Input Capture Pin of timer 1)
  Timer 2 is set up in phase correct PWM and output a duty cycle of
  10.98% ~-45.06 C on pin 3 and a duty cycle of 92.94% ~139.58 C on pin 11
  Connect pin 8 to pin 3 or pin 11 to check the working if no SMT172 is available

*/

#include<arduino.h>
#include <SMT172.h>

uint32_t LastSensorUpdate;

//The setup function is called once at startup of the sketch
void setup() {
  Serial.begin(115200);
  pinMode(8, INPUT);
  pinMode(12, OUTPUT);
  Serial.println(F("Demo sketch SMT172"));

  //	The following code fragment sets up phase-correct PWM on pins 3 and 11 (Timer 2).
  //	The waveform generation mode bits WGM are set to to 001 for phase-correct PWM.
  //	The other bits are the same as for fast PWM.

  pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
  // TCCR2B = _BV(CS22); // Output frequency: 16 MHz / 64 / 255 / 2 =  490.196 Hz
  TCCR2B = _BV(CS21); // Output frequency: 16 MHz /  8 / 255 / 2 = 3921.569 Hz
  OCR2A = 237; // Output A duty cycle: 237 / 255 = 92.94%	= 129.58 C	on pin 11
  OCR2B = 28;	 // Output B duty cycle:  28 / 255 = 10.98%	= -45.06 C	on pin 3
}

// The loop function is called in an endless loop
void loop()
{
  float tmp_temp;
  // read the sensor every 250 millisecond
  if ((unsigned long) (millis() - LastSensorUpdate) >= 100) {
    LastSensorUpdate = millis();

    SMT172::startTemperature(0.002);

repeat:
    switch (SMT172::getStatus()) {
      case 0: goto repeat; // O Dijkstra, be merciful onto me, for I have sinned against you :)
      case 1:
        //		    Serial.print(F("Measuring time   [ms]: "));
        //				Serial.println(SMT172::getTime() * 1e3, 2); // convert to milliseconds
        //				Serial.print(F("Sensor frequency [Hz]: "));
        //				Serial.println(SMT172::getFrequency(), 2);
        //				Serial.print(F("Duty cycle        [%]: "));
        //				Serial.println(SMT172::getDutyCycle() * 100, 2);
        Serial.print(F("Temperature       [C]: "));
        Serial.println(SMT172::getTemperature(), 2);
        tmp_temp = SMT172::getTemperature();
        if (tmp_temp > 30.0 ) Serial.println(tmp_temp);
        
        //				Serial.print(F("Error            [mK]: "));
        //				Serial.println(SMT172::getError() * 1000, 2);
        //				Serial.println();
        break;
      case 2: Serial.println(F("**** Sensor disconnected ****"));
        Serial.println();
    }
  }
}

That switch statement hurts my eyes and obscures the flow (and why a case for two results?) How about this:

void loop()
{
  ...
  do {
    int result = SMT::getStatus();
    } while ( ! result );
    If ( result == 1 ){
    ...
    }
    If ( result == 2 ) {
    ...
    }
}

I agree with You. You gave med the first tip . It showed good values except for ghostly spikes. Another member, in an other thread, tipped me of another path, another source of download and that's what You see here. It is the first, simple example file in that library.
I am satisfied with the output and now an I2C LCD will be added to complete my milling motor temperature monitouring thermometer.