Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

:: Reading from a SMT16030 temperature sensor ::

General

The SMT16030 is a digital temperature sensor - it can be read with digital inputs - an ADC is not required.

Connect the Output pin of the sensor to a digital input on your Arduino, +Vcc to 5V, and GND to GND.

Signal

The sensor generates a square wave signal. The amplitude is the same as the input volage (5V), the frequency varies between 1kHz and 4kHz. The data is encoded in the signal's https://en.wikipedia.org/wiki/Duty_cycle? - the fraction of the output signal that is HIGH.

The duty cycle can then be converted to degrees Celsius by calculating temperature = (dutyCycle) -0.32)/0.0047.

To get the duty cycle, the sensor's output has to be sampled. Because the maximum frequency is 4kHz, the sampling rate should be at least 8kHz for no loss of information. Since a digitalRead only takes one clock cycle, this is no problem for Arduino.

The fewer samples you take, the larger the error will be: You can see that the formula above returns temperatures between -68°C and 145°C - a range of 213K. Now if you only use 100 samples for your calculation, the error of your measurement is at least +/- 2.13K! With 4000 samples you can reduce it to +/- 0.05K.

Code

This sketch prints the HIGH count, sample count and temperature to the serial interface every 500ms. I'm using it to plot temperature over time with Processing. The HIGH count and sample rate in the output is useful for drawing average curves (over 2 seconds, over 4 seconds, ...)

unsigned long now;
unsigned long lastSample;
unsigned int sampleSize;
unsigned int highCount;
unsigned long lastOutput;
float temperature;
float hcf, ssf;

int sensorPin = 2;

void setup()
{
  Serial.begin(9600); 
  Serial.println("HELLO!");
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, LOW);
  lastSample = 0;
  sampleSize = 0;
  highCount = 0;
  lastOutput = 0;
  temperature = 0;
}

void loop() 
{
  now = micros();
  if (now - lastOutput > 500000) {
    hcf = highCount;
    ssf = sampleSize;
    temperature = ((hcf / ssf) -0.32)/0.0047;
    Serial.print(highCount);
    Serial.print(" / "); 
    Serial.print(sampleSize);
    Serial.print(" temp: "); 
    Serial.println(temperature);
    lastOutput = now;
    sampleSize = 0;
    highCount = 0;
  }
  if (now - lastSample > 100) {
    sampleSize++;
    highCount = highCount + digitalRead(sensorPin);
    lastSample = now;
  } else {
    delayMicroseconds(10);
  }
}

References