Example2_One-Shot_Temperature_Reading¶
examples/Example2_One-Shot_Temperature_Reading.py¶
1#!/usr/bin/env python
2#-----------------------------------------------------------------------------
3# Example2_One-Shot_Temperature_Reading.py
4# Example for the TMP102 I2C Temperature Sensor
5# Alex Wende @ SparkFun Electronics
6# April 29th 2016
7
8# This sketch connects to the TMP102 temperature sensor and enables the
9# one-shot temperature measurement mode using the one_shot() function.
10# The function returns 0 until the temperature measurement is ready to
11# read (takes around 25ms). After the measurment is read, the TMP102 is
12# placed back into sleep mode before the loop is repeated. This can be
13# useful to save power or increase the continuous conversion rate from
14# 8Hz up to a maximum of 40Hz.
15
16# This code is beerware; if you see me (or any other SparkFun employee) at
17# the local, and you've found our code helpful, please buy us a round!
18
19# Distributed as-is; no warranty is given.
20#-----------------------------------------------------------------------------
21
22from __future__ import print_function
23import qwiic_tmp102
24import time
25import sys
26
27# Connections
28# VCC = 3.3V
29# GND = GND
30# SDA = A4
31# SCL = A5
32
33# Sensor address can be changed with an external jumper to:
34# ADD0 - Address
35# VCC - 0x49
36# SDA - 0x4A
37# SCL - 0x4B
38
39
40def runExample():
41
42 print("\nSparkFun Qwiic TMP102 Sensor Example 2\n")
43 myTmpSensor = qwiic_tmp102.QwiicTmp102Sensor()
44
45 if myTmpSensor.is_connected == False:
46 print("The Qwiic TMP102 Sensor device isn't connected to the system. Please check your connection", \
47 file=sys.stderr)
48 return
49
50 myTmpSensor.begin()
51
52 print("Initialized.")
53
54 while True:
55 myTmpSensor.one_shot(1)
56 while(myTmpSensor.one_shot() == 0):
57 time.sleep(1)
58 print ("TempC = ", myTmpSensor.read_temp_c())
59 myTmpSensor.sleep()
60 time.sleep(1)
61
62
63if __name__ == '__main__':
64 try:
65 runExample()
66 except (KeyboardInterrupt, SystemExit) as exErr:
67 print("\nEnding Example 1")
68 sys.exit(0)
69
70
71
72