Digital Temperature Sensor Breakout - AS6212 (Qwiic) Hookup Guide

Contributors: QCPete, El Duderino
Favorited Favorite 0

Python Examples

The Qwiic AS6212 Python package includes two quick examples to get you started. Let's take a quick look at both of them. Just like the Arduino library, the examples start by creating the temperature sensor object and checking the Digital Temperature Sensor Breakout - AS6212 (Qwiic) is connected to the bus at the default address:

myTempSensor = qwiic_as6212.QwiicAs6212Sensor()

if myTempSensor.is_connected == False:
    print("The Qwiic AS6212 Sensor device isn't connected to the system. Please check your connection", \


If you see this printout, the most common causes are the AS6212 is set to an alternate I2C address and the code has not been adjusted properly or the device is not making a good connection with the I2C pins. If the AS6212 is set to an alternate address, make sure to add the address to this line:

myTempSensor = qwiic_as6212.QwiicAs6212Sensor(NEW ADDRESS HERE)

Example 1 - Basic Readings

Don't be fooled by the name of Example 1, think of it more as a kitchen sink demo of getting temperature data, setting high and low temperature thresholds, the alert behavior, alert polarity, consecutive fault count and conversion cycle time. After initializing the sensor on the bus, the code sets all of the values listed above:

# set the number of consecutive faults before triggering alarm.
# valid options: 1,2,3 or 4

# set the polarity of the Alert. (0:Active LOW, 1:Active HIGH).

# set the sensor in Comparator Mode (0) or Interrupt Mode (1).

# set the Conversion Cycle Time (how quickly the sensor gets a new reading)

# set T_HIGH, the upper limit to trigger the alert on
myTempSensor.set_high_temp_f(78.0)  # set T_HIGH in F
# myTempSensor.set_high_temp_c(25.56) # set T_HIGH in C

# set T_LOW, the lower limit to shut turn off the alert
myTempSensor.set_low_temp_f(75.0)   # set T_LOW in F
# myTempSensor.set_low_temp_c(23.89)    # set T_LOW in C

The main loop puts the AS6212 in and out of sleep mode. When awake, the code retrieves temperature data in °F and then puts it back into sleep mode. Each second the code prints out the recorded temperature data and the Alert Register state.

while True:
    myTempSensor.set_sleep_mode(0) # turn sleep  mode off (0)
    time.sleep(0.250) # allow time to wake up and complete first conversion

    temperature = myTempSensor.read_temp_f()

    # Check for alert
    alertRegisterState = myTempSensor.get_alert_status()        # read the Alert from register

    # Place sensor in sleep mode to save power.
    # Current consumption typically ~0.1uA.
    myTempSensor.set_sleep_mode(1) # turn sleep  mode on (1)

    print("Temperature: ", temperature, "\tAlert Register: ", alertRegisterState)

This method works fine for standard applications but using the AS6212's single shot readings is even better to conserve power as we can call for temperature data on demand. The next example covers using single shot.

Example 2 - Single Shot

The second example shows how to set up and use single shot readings on the AS6212. The example sets up similarly to the previous example but does not set any of the alert or threshold settings. Instead, the code initializes the AS6212 and then puts the device into sleep mode:

print("Sleep mode ON")

The AS6212 must be in sleep mode for single shot measurements to work. After the device has been initialized and put in sleep mode, the main loop triggers single shot conversions and prints out the reported temp data every second:

while True:
    myTempSensor.trigger_single_shot_conversion() # trigger SS

    #wait for conversion to complete (~51ms)
    conversionTime = 0
    while myTempSensor.get_single_shot_status() == 1:
        conversionTime += 1
        time.sleep(0.001) # 1ms

    tempF = myTempSensor.read_temp_f()

    print("Temperature: %.2fF \t Conversion time: %ims" % (tempF, conversionTime))