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:
i2c : I2C

The I2C bus the MCP9808 is connected to.

address : int

The I2C device address. Defaults to 0x18

Raises:

RuntimeError – if the sensor is not found

Quickstart: Importing and using the device

Here is an example of using the MCP9808 class. First you will need to import the libraries to use the sensor

from machine import Pin, I2C
from micropython_mcp9808 import mcp9808

Once this is done you can define your machine.I2C object and define your sensor object

i2c = 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_0

0b00

mcp9808.HYSTERESIS_1_5

0b01

mcp9808.HYSTERESIS_3

0b10

mcp9808.HYSTERESIS_6

0b11

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.CONTINUOUS

0b00

mcp9808.SHUTDOWN

0b1

property temperature

Temperature in Celsius

property temperature_critical : float

Critical temperature in Celsius

property temperature_lower : float

Lower temperature in Celsius

property temperature_resolution : str

Sensor temperature_resolution

Mode

Value

mcp9808.RESOLUTION_0_5_C

0b00 0.5°C

mcp9808.RESOLUTION_0_625_C

0b01 0.25°C

mcp9808.RESOLUTION_0_125_C

0b10 0.125°C

mcp9808.RESOLUTION_0_0625_C

0b11 0.0625°C

property temperature_upper : float

Upper temperature in Celsius