code.py
1 # SPDX-FileCopyrightText: 2021 Brent Rubell for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 'temperature.py'. 7 8 ================================================= 9 Writes TMP36 data to the REPL 10 """ 11 12 import time 13 import analogio 14 import board 15 from simpleio import map_range 16 17 temp_sensor = analogio.AnalogIn(board.A0) 18 19 20 def get_voltage(_temp_sensor): 21 """gets the TMP36's voltage.""" 22 voltage_val = map_range(_temp_sensor.value, 0, 65535, 0, 3.3) 23 return voltage_val 24 25 26 while True: 27 temp = get_voltage(temp_sensor) 28 # convert to celsius 29 temp = (temp - 0.5) * 100 30 print(" Temperature =", temp) 31 time.sleep(1)