MCP9808¶
mcp9808¶
MicroPython Driver for the Microchip MCP9808 Temperature Sensor
Author(s): Jose D. Montoya
- class micropython_mcp9808.mcp9808.AlertStatus(high_alert, low_alert, critical_alert)¶
Create new instance of AlertStatus(high_alert, low_alert, critical_alert)
- critical_alert¶
Alias for field number 2
- high_alert¶
Alias for field number 0
- low_alert¶
Alias for field number 1
-
class micropython_mcp9808.mcp9808.MCP9808(i2c, address: int =
0x18)[source]¶ Driver for the MCP9808 Sensor connected over I2C.
- Parameters:¶
- Raises:¶
RuntimeError – if the sensor is not found
Quickstart: Importing and using the device
Here is an example of using the
MCP9808class. First you will need to import the libraries to use the sensorfrom machine import Pin, I2C from micropython_mcp9808 import mcp9808Once this is done you can define your
machine.I2Cobject and define your sensor objecti2c = I2C(1, sda=Pin(2), scl=Pin(3)) mcp = mcp9808.MCP9808(i2c)Now you have access to the attributes
temp = mcp.temperature- property alert_status¶
The current triggered status of the high and low temperature alerts as a AlertStatus named tuple with attributes for the triggered status of each alert.
import time from machine import Pin, I2C from micropython_mcp9808 import mcp9808 i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040 mcp = mcp9808.MCP9808(i2c) mcp.temperature_lower = 20 mcp.temperature_upper = 23 mcp.temperature_critical = 30 print("High limit: {}".format(mcp.temperature_upper)) print("Low limit: {}".format(mcp.temperature_lower)) print("Critical limit: {}".format(mcp.temperature_critical)) while True: print("Temperature: {:.2f}C".format(mcp.temperature)) alert_status = tmp.alert_status if alert_status.high_alert: print("Temperature above high set limit!") if alert_status.low_alert: print("Temperature below low set limit!") if alert_status.critical_alert: print("Temperature above critical set limit!") time.sleep(1)
- property hysteresis : str¶
Sensor hysteresis
Mode
Value
mcp9808.HYSTERESIS_00b00mcp9808.HYSTERESIS_1_50b01mcp9808.HYSTERESIS_30b10mcp9808.HYSTERESIS_60b11
- property power_mode : str¶
Sensor power_mode In shutdown, all power-consuming activities are disabled, though all registers can be written to or read. This bit cannot be set to ‘1’ when either of the Lock bits is set (bit 6 and bit 7). However, it can be cleared to ‘0’ for continuous conversion while locked
Mode
Value
mcp9808.CONTINUOUS0b00mcp9808.SHUTDOWN0b1
- property temperature¶
Temperature in Celsius