Interfacing mcp9808 temperature sensor

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Zacks
Posts: 6
Joined: Wed Sep 08, 2021 8:16 am

Interfacing mcp9808 temperature sensor

Post by Zacks » Mon Sep 13, 2021 7:14 am

I am trying to interface the MCP9808 temperature sensor with OpenMV Cam, and I found this library from here https://github.com/kfricke/micropython-mcp9808 However there was no example code on how to use the library to read the temperature. This maybe simple but since I am still new to miccropython it may take time to try to figure it out. Help from anyone who may have used used this before or can help with an example on how to use the library to read temperature will be much appreciated

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: Interfacing mcp9808 temperature sensor

Post by mcauser » Mon Sep 20, 2021 12:09 pm

Firstly you need to download mcp9808.py and copy to your device.
Next connect the sensor to your I2C pins.
Then a simple example would be:

Code: Select all

from machine import Pin, I2C
import mcp9808

# adjust pins and frequency to suit your device
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)

# create an instance of the sensor, giving it a reference to the I2C bus
mcp = mcp9808.MCP9808(i2c)

# get the temperature (float)
temp_celsius = mcp.get_temp()
print(temp_celsius)

Post Reply