B4R and ESP32 MCP9808 digital temperature sensor example

MCP9808 digital temperature sensor example

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

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

More details about the sensor at http://www.microchip.com/wwwproducts/en/MCP9808

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

mcp9808 breakout
mcp9808 breakout

 

Connection

lolin32 and mcp9808
lolin32 and mcp9808

Code

You will need to install the adafruit MCP9808 library which is available at https://github.com/adafruit/Adafruit_MCP9808_Library/archive/master.zip .

 

 

<pre>#Region Project Attributes
	#AutoFlushLogs: True
	#CheckArrayBounds: True
	#StackBufferSize: 300
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'Public variables can be accessed from all modules.
	Public Serial1 As Serial
	Public timer1 As Timer
	Public mcptemp As Float
End Sub

Private Sub AppStart
	Serial1.Initialize(115200)
	Log("AppStart")
	RunNative("setup",Null)
	timer1.Initialize("timer1_Tick",1000)
	timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
	RunNative("loop",Null)
	Log("Temperature is: ", mcptemp, "°C")
End Sub

#if C
#include &lt;Wire.h&gt;
#include "Adafruit_MCP9808.h"
Adafruit_MCP9808 tempSensor = Adafruit_MCP9808();
void setup(B4R::Object* o){
tempSensor.begin();
}
  
void loop (B4R::Object* o) {
 
   b4r_main::_mcptemp =tempSensor.readTempC();
}

#End if
</pre>

 

Output

You should see this logged out

Temperature is: 21.2500°C
Temperature is: 21.2500°C
Temperature is: 21.2500°C
Temperature is: 21.5000°C
Temperature is: 24.3125°C
Temperature is: 25.1875°C
Temperature is: 25.5000°C
Temperature is: 25.6875°C
Temperature is: 25.8125°C
Temperature is: 25.9375°C
Temperature is: 26.0625°C
Temperature is: 26.1250°C
Temperature is: 26.1875°C
Temperature is: 26.2500°C
Temperature is: 26.4375°C
Temperature is: 26.1875°C
Temperature is: 25.1875°C
Temperature is: 24.8125°C

 

Links

High Accuracy Temperature Sensor MCP9808 I2C Breakout Board Module 2.7V-5V Logic Voltage

SHARE
Previous articleA B4R ESP32 HDC1008 temperature sensor example
Next articleESP32 and I2C LCD example