Simple test

Ensure your device works with this simple test.

examples/mcp9808_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import adafruit_mcp9808
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9
10# To initialise using the default address:
11mcp = adafruit_mcp9808.MCP9808(i2c)
12
13# To initialise using a specified address:
14# Necessary when, for example, connecting A0 to VDD to make address=0x19
15# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19)
16
17while True:
18    tempC = mcp.temperature
19    tempF = tempC * 9 / 5 + 32
20    print("Temperature: {} C {} F ".format(tempC, tempF))
21    time.sleep(2)

Temperature Limit test

Show the MCP9808 to setup different temperature values

examples/mcp9808_temperature_limits.py
 1# SPDX-FileCopyrightText: 2021 Jose David M.
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Show the MCP9808 to setup different temperature values
 6"""
 7
 8import time
 9import board
10import adafruit_mcp9808
11
12i2c = board.I2C()  # uses board.SCL and board.SDA
13mcp = adafruit_mcp9808.MCP9808(i2c)
14
15# Change the values according to the desired values
16print("Setting Temperature Limits")
17mcp.upper_temperature = 23
18mcp.lower_temperature = 10
19mcp.critical_temperature = 100
20
21# To verify the limits we need to read the temperature value
22print(mcp.temperature)
23time.sleep(0.3)  # This is the time temperature conversion at maximum resolution
24
25# Showing temperature Limits
26while True:
27    if mcp.below_lt:
28        print("too cold!")
29    if mcp.above_ut:
30        print("getting hot!")
31    if mcp.above_ct:
32        print("Above critical temp!")