Digital Temperature Sensor Breakout - AS6212 (Qwiic) Hookup Guide

Pages
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:

language:python
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", \
        file=sys.stderr

return

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:

language:python
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:

language:python
# set the number of consecutive faults before triggering alarm.
# valid options: 1,2,3 or 4
myTempSensor.set_consecutive_faults(1)

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

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

# set the Conversion Cycle Time (how quickly the sensor gets a new reading)
myTempSensor.set_conversion_cycletime(myTempSensor.AS6212_CONVERSION_CYCLE_TIME_250MS)

# 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.

language:python
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)
    time.sleep(1)

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:

lanugage:python
myTempSensor.set_sleep_mode(1)
print("Sleep mode ON")
time.sleep(1)

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:

language:python
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))
    time.sleep(1)