Skip to content

Examples

Example 1: Basic Readings

Now that we've got our library installed and our hardware all hooked up, let's look at some examples.

This first example just does some basic measurements. To find Example 1, go to File > Examples > SparkFun Temperature Sensor - STTS22H > example1-basic:

Serial monitor output

Alternatively, you can copy and paste the code below to a shiny new Arduino file:

Example 1 Arduino Code
/*
example1-basic.ino

This example shows basic data retrieval from the SparkFun Temperature Sensor - STTS22H.

Output Data Rates: 

STTS22H_POWER_DOWN
STTS22H_ONE_SHOT  
STTS22H_1Hz       
STTS22H_25Hz      
STTS22H_50Hz      
STTS22H_100Hz     
STTS22H_200Hz     

Written by: 
Elias Santistevan @ SparkFun Electronics December, 2022

Products: 
   SparkFun Temperature Sensor - STTS2H              https://www.sparkfun.com/products/21262
   SparkFun Micro Temperature Sensor - STTS2H        https://www.sparkfun.com/products/21051

Repository:
     https://github.com/sparkfun/SparkFun_STTS22H_Arduino_Library

SparkFun code, firmware, and software is released under the MIT
License(http://opensource.org/licenses/MIT).

*/

#include <Wire.h>
#include "SparkFun_STTS22H.h"

SparkFun_STTS22H mySTTS; 

float temp; 

void setup()
{

    Wire.begin();

    Serial.begin(115200);

    if( !mySTTS.begin() )
    {
        Serial.println("Did not begin.");
        while(1);
    }

    Serial.println("Ready");

    // Other output data rates can be found in the description
    // above. To change the ODR or mode, the device must first be
    // powered down.
    mySTTS.setDataRate(STTS22H_POWER_DOWN);
    delay(10);
    mySTTS.setDataRate(STTS22H_1Hz);

    // Enables incrementing register behavior for the IC.
    // It is not enabled by default as the datsheet states and
    // is vital for reading the two temperature registers.
    mySTTS.enableAutoIncrement();

    delay(100);
}

void loop()
{

    // Only use data ready for one-shot mode or 1Hz output. 
    if( mySTTS.dataReady() ) 
    {

        mySTTS.getTemperatureF(&temp);

        // Temperature in different units can be retrieved
        // using the following functions.

        //mySTTS.getTemperatureC(&temp);
        //mySTTS.getTemperatureK(&temp);

        Serial.print("Temp: "); 
        Serial.print(temp);
        Serial.println("F"); 

    } 

    // delay = 1/ODR 
    delay(1000);

}

Once you've got your code uploaded, open up a Serial Monitor and check out your output.

Serial monitor output

Example 2: Interrupts

Once the library is installed, go ahead and open up File->Examples->SparkFun Temperature Sensor - STTS22H > example2-interrupt:

Serial monitor output

Make sure to select your board (SparkFun RedBoard) and COM port before hitting upload to begin experimenting with the air quality sensor. Alternatively, you can copy and paste the code below into a nice new Arduino sketch:

Example 2 Arduino Code
/*
example2_basic.ino

This example desmonstrates how to set temperature thresholds to trigger an interrupt. 

Output Data Rates: 

STTS22H_POWER_DOWN
STTS22H_ONE_SHOT  
STTS22H_1Hz       
STTS22H_25Hz      
STTS22H_50Hz      
STTS22H_100Hz     
STTS22H_200Hz     

Written by: 
Elias Santistevan @ SparkFun Electronics December, 2022

Products: 
   SparkFun Temperature Sensor - STTS2H              https://www.sparkfun.com/products/21262
   SparkFun Micro Temperature Sensor - STTS2H        https://www.sparkfun.com/products/21051

Repository:
     https://github.com/sparkfun/SparkFun_STTS22H_Arduino_Library

SparkFun code, firmware, and software is released under the MIT
License(http://opensource.org/licenses/MIT).

*/

#include <Wire.h>
#include "SparkFun_STTS22H.h"

SparkFun_STTS22H mySTTS; 

float temp; 

// These values are in Farenheit
float interruptHighValue = 90.5;
float interruptLowValue = 42.0;

int tempInterrupt = 1; 

void setup()
{

    Wire.begin();

    Serial.begin(115200);

    pinMode(tempInterrupt, INPUT);

    if( !mySTTS.begin() )
    {
        Serial.println("Did not begin.");
        while(1);
    }

    Serial.println("Ready");

    // Other output data rates can be found in the description
    // above. To change the ODR or mode, the device must first be
    // powered down.
    mySTTS.setDataRate(STTS22H_POWER_DOWN);
    delay(10);
    mySTTS.setDataRate(STTS22H_25Hz);

    // Enables incrementing register behavior for the IC.
    // It is not enabled by default as the datsheet states and
    // is vital for reading the two temperature registers.
    mySTTS.enableAutoIncrement();


    // Set interrupts for both lower and higher thresholds.
    // Note: These functions accept Farenheit as their arguments.
    // Other functions for different units just below. 
    mySTTS.setInterruptLowF(interruptLowValue);
    mySTTS.setInterruptHighF(interruptHighValue);

    //mySTTS.setInterruptLowC(interruptLowValue);
    //mySTTS.setInterruptHighC(interruptHighValue);

    //mySTTS.setInterruptLowK(interruptLowValue);
    //mySTTS.setInterruptHighK(interruptHighValue);

    delay(100);
}

void loop()
{

    // Checking if data ready is not necessary when output is set higher 
    // than 1Hz. 
    mySTTS.getTemperatureF(&temp);

    // Temperature in different units can be retrieved
    // using the following functions.

    //mySTTS.getTemperatureC(&temp);
    //mySTTS.getTemperatureK(&temp);

    Serial.print("Temp: "); 
    Serial.print(temp);
    Serial.println("F"); 

    if( digitalRead(tempInterrupt) == LOW )
    {
        Serial.println("Temperature threshold"); 
        while(1);
    }

    // delay = 1/ODR 
    delay(40);

}

Note that depending on which processor board you are using, you may need to alter the Interrupt Pin. Since we're using a RedBoard here, our Interrupt Pin is 2 (ensInt = 2). Also, in this example, we've used an IC hook with a pigtail to connect the Interrupt Pin to the RedBoard pin 2, but you can also solder headers to the STTS22H Temperature Sensor so you can use the interrupt pin. Your hardware hookup should look something like the following:

Use the Qwiic connectors to connect the boards to each other and use a jumper to connect the INT pin to Pin 2 of the RedBoard

Once you've got your code uploaded, open up a Serial Monitor and check out your output.

If you have a look at the code, you'll notice that we've set our upper threshhold to 90.5 degrees F, and our lower threshhold to 42 degrees F. I held the sensor in front of a heater to hit the upper threshhold:

Serial monitor output

The lower threshhold was reached by sticking the sensor in a plastic bag and then putting that plastic bag into ice water:

Serial monitor output

Example 3: One Shot

The One-Shot operating mode of the STTS22H allows for the temperature measurement to be made and then the device puts itself in a power-down condition. In one-shot mode, the sensor current consumption falls to 1.75 µA, though the full breakout board will draw a bit higher due to the LED &etc.

Go ahead and open up File->Examples->SparkFun Temperature Sensor - STTS22H ->example3-one_shot. Make sure to select your board (SparkFun RedBoard) and COM port before hitting upload to begin experimenting with the air quality sensor.

Menu Location as listed above

Alternatively, you can copy and paste the code below into a nice new Arduino sketch:

Example 3 Arduino Code
/*
example3-one_shot.ino

This example shows basic data retrieval using the "one-shot" feature i.e. - get the temp
now feature. 

Output Data Rates: 

STTS22H_POWER_DOWN
STTS22H_ONE_SHOT   < -------- This one. 
STTS22H_1Hz       
STTS22H_25Hz      
STTS22H_50Hz      
STTS22H_100Hz     
STTS22H_200Hz     

Written by: 
Elias Santistevan @ SparkFun Electronics December, 2022

Products: 
   SparkFun Temperature Sensor - STTS2H              https://www.sparkfun.com/products/21262
   SparkFun Micro Temperature Sensor - STTS2H        https://www.sparkfun.com/products/21051

Repository:
     https://github.com/sparkfun/SparkFun_STTS22H_Arduino_Library

SparkFun code, firmware, and software is released under the MIT
License(http://opensource.org/licenses/MIT).

*/

#include <Wire.h>
#include "SparkFun_STTS22H.h"

SparkFun_STTS22H mySTTS; 

float temp; 

void setup()
{

    Wire.begin();

    Serial.begin(115200);

    if( !mySTTS.begin() )
    {
        Serial.println("Did not begin.");
        while(1);
    }

    Serial.println("Ready");

    // Other output data rates can be found in the description
    // above. To change the ODR or mode, the device must first be
    // powered down.
    mySTTS.setDataRate(STTS22H_POWER_DOWN);
    delay(10);
    // Force new reading, temp sensor will power down after conversion. 
    mySTTS.setDataRate(STTS22H_ONE_SHOT); 

    // Enables incrementing register behavior for the IC.
    // It is not enabled by default as the datsheet states and
    // is vital for reading the two temperature registers.
    mySTTS.enableAutoIncrement();

    delay(100);
}

void loop()
{

    // Temp sensor will power down automatically after single read.
    if( mySTTS.dataReady() ) 
    {

        mySTTS.getTemperatureF(&temp);

        // Temperature in different units can be retrieved
        // using the following functions.

        //mySTTS.getTemperatureC(&temp);
        //mySTTS.getTemperatureK(&temp);

        Serial.print("Temp: "); 
        Serial.print(temp);
        Serial.println("F"); 

        // Wait 10 seconds for until we initiate another read.
        delay(10000);

        // Enable another reading.
        mySTTS.setDataRate(STTS22H_ONE_SHOT); 

    } 

    // Demonstrative delay. 
    delay(100);

}

Once you've got your code uploaded, open up a Serial Monitor and check out your output. You should see something like the following:

Serial monitor output

This really isn't all that exciting until you measure the current consumption!