1#!/usr/bin/env python
2#-----------------------------------------------------------------------------
3# Example_01_BasicReadings.py
4#
5# Simple Example for the Qwiic AS6212 Device
6#------------------------------------------------------------------------
7#
8# Written by Pete Lewis, SparkFun Electronics, Aug 2021
9#
10# Thanks to Alex Wende and Lori Croster @ SparkFun Electronics
11# for code examples from TMP102 Python Package, May 2021
12# (https://github.com/sparkfun/Qwiic_TMP102_Py)
13#
14# Thanks to Brandon Williams. This library was based off his
15# original library created 07/15/2020 and can be found here:
16# https://github.com/will2055/AS6212-Arduino-Library/
17#
18# Thanks to Madison Chodikov @ SparkFun Electronics
19# for code examples from TMP117 Arduino Library
20# (https://github.com/sparkfun/SparkFun_TMP117_Arduino_Library)
21#
22# This python library supports the SparkFun Electroncis qwiic
23# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
24# board computers.
25#
26# This python library supports the SparkFun Electroncis qwiic
27# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
28# board computers.
29#
30# More information on qwiic is at https://www.sparkfun.com/qwiic
31#
32# Do you like this library? Help support SparkFun. Buy a board!
33#
34#==================================================================================
35# Copyright (c) 2021 SparkFun Electronics
36#
37# Permission is hereby granted, free of charge, to any person obtaining a copy
38# of this software and associated documentation files (the "Software"), to deal
39# in the Software without restriction, including without limitation the rights
40# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
41# copies of the Software, and to permit persons to whom the Software is
42# furnished to do so, subject to the following conditions:
43#
44# The above copyright notice and this permission notice shall be included in all
45# copies or substantial portions of the Software.
46#
47# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
50# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
51# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
52# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53# SOFTWARE.
54#==================================================================================
55# Example 1
56#
57
58from __future__ import print_function
59import qwiic_as6212
60import time
61import sys
62
63def runExample():
64
65 print("\nSparkFun Qwiic AS6212 Sensor Example 1\n")
66 myTempSensor = qwiic_as6212.QwiicAs6212Sensor()
67
68 if myTempSensor.is_connected == False:
69 print("The Qwiic AS6212 Sensor device isn't connected to the system. Please check your connection", \
70 file=sys.stderr)
71 return
72
73 myTempSensor.begin()
74 time.sleep(1)
75
76 print("Initialized.")
77
78 # Initialize configuration settings
79 # These settings are saved in the sensor, even if it loses power
80
81 # set the number of consecutive faults before triggering alarm.
82 # valid options: 1,2,3 or 4
83 myTempSensor.set_consecutive_faults(1)
84
85 # set the polarity of the Alert. (0:Active LOW, 1:Active HIGH).
86 myTempSensor.set_alert_polarity(myTempSensor.AS6212_ALERT_ACTIVE_LOW)
87
88 # set the sensor in Comparator Mode (0) or Interrupt Mode (1).
89 myTempSensor.set_interrupt_mode(myTempSensor.AS6212_MODE_COMPARATOR)
90
91 # set the Conversion Cycle Time (how quickly the sensor gets a new reading)
92 myTempSensor.set_conversion_cycletime(myTempSensor.AS6212_CONVERSION_CYCLE_TIME_250MS)
93
94 # set T_HIGH, the upper limit to trigger the alert on
95 myTempSensor.set_high_temp_f(78.0) # set T_HIGH in F
96 # myTempSensor.set_high_temp_c(25.56) # set T_HIGH in C
97
98 # set T_LOW, the lower limit to shut turn off the alert
99 myTempSensor.set_low_temp_f(75.0) # set T_LOW in F
100 # myTempSensor.set_low_temp_c(23.89) # set T_LOW in C
101
102 print("TLOW F: ", myTempSensor.read_low_temp_f())
103 print("THIGH F: ", myTempSensor.read_high_temp_f())
104
105 while True:
106 myTempSensor.set_sleep_mode(0) # turn sleep mode off (0)
107 time.sleep(0.250) # allow time to wake up and complete first conversion
108
109 temperature = myTempSensor.read_temp_f()
110
111 # Check for alert
112 alertRegisterState = myTempSensor.get_alert_status() # read the Alert from register
113
114 # Place sensor in sleep mode to save power.
115 # Current consumption typically ~0.1uA.
116 myTempSensor.set_sleep_mode(1) # turn sleep mode on (1)
117
118 print("Temperature: ", temperature, "\tAlert Register: ", alertRegisterState)
119 time.sleep(1)
120
121if __name__ == '__main__':
122 try:
123 runExample()
124 except (KeyboardInterrupt, SystemExit) as exErr:
125 print("\nEnding Example 1")
126 sys.exit(0)