Home Raspberry PI Raspberry Pi 3 and MCP9808 example in C

Raspberry Pi 3 and MCP9808 example in C

by iainhendry

The MCP9808 digital temperature sensor converts temperatures between -20°C and +100°C to a digital word with ±0.5°C (max.) 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 Event and Critical output boundaries. When the temperature changes beyond the specified boundary limits, the MCP9808 outputs an Event signal. The user has the option of setting the event output signal polarity as an active-low or active-high comparator output for thermostat operation, or as temperature event interrupt output for microprocessor-based systems. .

The event output can also be configured as a Critical temperature output. This sensor has an industry standard 2-wire, SMBus and Standard I2C™Compatible compatible (100kHz/400kHz bus clock) serial interface, allowing up to eight sensors to be controlled in a single serial bus. These features make the MCP9808 ideal for sophisticated multi-zone temperature- monitoring applications.

Here is a typical module that you can purchase


      • Accuracy:
        • ±0.25°C (typical) from -40°C to +125°C
        • ±0.5°C (maximum) from -20°C to +100°C
      • User Selectable Measurement Resolution:
        • 0.5°C, 0.25°C, 0.125°C, 0.0625°C
      • User Programmable Temperature Limits:
        • Temperature Window Limit
        • Critical Temperature Limit
      • User Programmable Temperature Alert Output
      • Operating Voltage Range: 2.7V to 5.5V
      • Operating Current: 200 µA (typical)
      • Shutdown Current: 0.1 µA (typical)
      • 2-wire Interface: I2C/SMBus Compatible


A very simple device to connect to a Raspberry Pi

pi and mcp9808

pi and mcp9808



save the file below as MCP9808.c

[codesyntax lang=”cpp”]

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MCP9808
// This code is designed to work with the MCP9808_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Temperature?sku=MCP9808_I2CS#tabs-0-product_tabset-2

#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>

void main()
// Create I2C bus
int file;
char *bus = “/dev/i2c-1”;
if ((file = open(bus, O_RDWR)) < 0)
printf(“Failed to open the bus. \n”);
// Get I2C device, MCP9808 I2C address is 0x18(24)
ioctl(file, I2C_SLAVE, 0x18);

// Select configuration register(0x01)
// Continuous conversion mode, Power-up default(0x00, 0x00)
char config[3] = {0};
config[0] = 0x01;
config[1] = 0x00;
config[2] = 0x00;
write(file, config, 3);
// Select resolution rgister(0x08)
// Resolution = +0.0625 / C(0x03)
config[0] = 0x08;
config[1] = 0x03;
write(file, config, 2);

// Read 2 bytes of data from register(0x05)
// temp msb, temp lsb
char reg[1] = {0x05};
write(file, reg, 1);
char data[2] = {0};
if(read(file, data, 2) != 2)
printf(“Error : Input/Output error \n”);
// Convert the data to 13-bits
int temp = ((data[0] & 0x1F) * 256 + data[1]);
if(temp > 4095)
temp -= 8192;
float cTemp = temp * 0.0625;
float fTemp = cTemp * 1.8 + 32;

// Output data to screen
printf(“Temperature in Celsius is : %.2f C \n”, cTemp);
printf(“Temperature in Fahrenheit is : %.2f F \n”, fTemp);


compile this like the following

gcc MCP9808.c -o MCP9808 -lwiringPi - lm


run this like this

sudo ./MCP9808





MCP9808 High Accuracy I2C IIC Temperature Sensor Breakout Board

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More