Beaglebone and MCP9808 digital temperature sensor in C example


In this article we look at an MCP9808 connected to a Beaglebone first of all lets look at the sensor.

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.


±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

More details about the sensor at

This typically comes in a breakout such as the one in the breakout below


Parts Required


Name Link
Beaglebone BeagleBone Black TI AM3358 Cortex-A8 development BB-Black Rev.C
MCP9808 MCP9808 I2C Breakout Board Module
Connecting wire Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire


beaglebone and mcp9808
beaglebone and mcp9808

Code Example

This is a controleverything example – they have code examples for various platforms. This is the C example from


#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-2";
	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);


Save this as mcp9808.c, I used the Cloud 9 IDE

First of all compile the c program.

$>gcc MCP9808.c -o MCP9808

Run the c program.




After running you should see something like this

Temperature in Celsius is : 23.81 C
Temperature in Fahrenheit is : 74.86 F


Please enter your comment!
Please enter your name here


This site uses Akismet to reduce spam. Learn how your comment data is processed.