MCP9808 I2C

Github Repo C Header C source JS source
mongoose-os-libs/mcp9808-i2c mgos_mcp9808.h    

A Mongoose library for Microchip's MCP9808 integrated circuit.

Sensor details

Microchip Technology Inc.’s MCP9808 digital temperature sensor converts temperatures between -20°C and +100°C to a digital word with ±0.25°C/±0.5°C (typical/maximum) accuracy.

The MCP9808 comes with user-programmable registers that provide flexibility for temperature sensing applications. The registers allow user-selectable settings such as Shutdown or Low-Power modes and the specification of temperature Alert window limits and critical output limits. When the temperature changes beyond the specified boundary limits, the MCP9808 outputs an Alert signal. The user has the option of setting the Alert output signal polarity as an active-low or activehigh comparator output for thermostat operation, or as a temperature Alert interrupt output for microprocessor based systems. The Alert output can also be configured as a critical temperature output only.

This sensor has an industry standard 400 kHz, 2-wire, SMBus/I2C compatible serial interface, allowing up to eight or sixteen sensors to be controlled with a single serial bus.

These features make the MCP9808 ideal for sophisticated, multi-zone, temperature-monitoring applications.

See datasheet for implementation details.

A great place to buy a ready made and tested unit is at Adafruit.

Example application

An example program using a timer to read data from the sensor every 5 seconds:

#include "mgos.h"
#include "mgos_i2c.h"
#include "mgos_mcp9808.h"

static struct mgos_mcp9808 *s_mcp9808;

static void timer_cb(void *user_data) {
  float temperature;


  LOG(LL_INFO, ("mcp9808 temperature=%.2f", temperature));

  (void) user_data;

enum mgos_app_init_result mgos_app_init(void) {
  struct mgos_i2c *i2c;

  if (!i2c) {
    LOG(LL_ERROR, ("I2C bus missing, set i2c.enable=true in mos.yml"));
  } else {
    s_mcp9808=mgos_mcp9808_create(i2c, 0x40); // Default I2C address
    if (s_mcp9808) {
      mgos_set_timer(5000, true, timer_cb, NULL);
    } else {
      LOG(LL_ERROR, ("Could not initialize sensor"));


This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.


uint32_t read;                 // calls to _read()
  uint32_t read_success;         // successful _read()
  uint32_t read_success_cached;  // calls to _read() which were cached
  // Note: read_errors := read - read_success - read_success_cached
  double   read_success_usecs;   // time spent in successful uncached _read()

value of mg_time() upon last call to _read()


struct mgos_mcp9808 *mgos_mcp9808_create(struct mgos_i2c *i2c, uint8_t i2caddr);

Initialize a MCP9808 on the I2C bus i2c at address specified in i2caddr parameter (default MCP9808 is on address 0x18). The sensor will be polled for validity, upon success a new struct mgos_mcp9808 is allocated and returned. If the device could not be found, NULL is returned.


void mgos_mcp9808_destroy(struct mgos_mcp9808 **sensor);

Destroy the data structure associated with a MCP9808 device. The reference to the pointer of the struct mgos_mcp9808 has to be provided, and upon successful destruction, its associated memory will be freed and the pointer set to NULL.


bool mgos_mcp9808_read(struct mgos_mcp9808 *sensor);

The sensor will be polled for its temperature and humidity data. If the poll has occured in the last MGOS_MCP9808_READ_DELAY seconds, the cached data is used (so as not to repeatedly poll the bus upon subsequent calls).


float mgos_mcp9808_getTemperature(struct mgos_mcp9808 *sensor);

The sensor will be polled for its temperature and humidity data. If the poll has occured in the last MGOS_MCP9808_READ_DELAY seconds, the cached data is used (so as not to repeatedly poll the bus upon subsequent calls).

The return value is the temperature of the sensor in Celsius, or NAN if no data was found.


bool mgos_mcp9808_getStats(struct mgos_mcp9808 *sensor, struct mgos_mcp9808_stats *stats);

Returns the running statistics on the sensor interaction, the user provides a pointer to a struct mgos_mcp9808_stats object, which is filled in by this call.

Upon success, true is returned. Otherwise, false is returned, in which case the contents of stats is undetermined.


void mgos_mcp9808_enable(struct mgos_mcp9808 *sensor, bool enable);

The sensor will be enabled (true) or disabled and put into deep sleep (false) based on the enable argument.


bool mgos_mcp9808_i2c_init(void);

Initialization function for MGOS -- currently a noop.

edit this doc