Categories

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

Code

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

#include <Wire.h>
#include "Adafruit_MCP9808.h"
 
// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
 
void setup() {
  Serial.begin(9600);
 
  if (!tempsensor.begin()) 
  {
    Serial.println("Couldn't find MCP9808!");
    while (1);
  }
}
 
void loop() {
  // Read and print out the temperature, then convert to *F
  float c = tempsensor.readTempC();
  float f = c * 9.0 / 5.0 + 32;
  Serial.print("Temp: "); 
  Serial.print(c); 
  Serial.print(" C\t"); 
  Serial.print(f); 
  Serial.println(" F");
  delay(250);
 
  tempsensor.shutdown_wake(1);
  delay(2000);
  tempsensor.shutdown_wake(0);
 
 
}

 

Results

Open the serial monitor and you should see something like this

Temp: 21.69 C 71.04 F
Temp: 21.62 C 70.93 F
Temp: 24.50 C 76.10 F
Temp: 25.06 C 77.11 F
Temp: 25.37 C 77.68 F
Temp: 25.56 C 78.01 F
Temp: 24.25 C 75.65 F
Temp: 23.87 C 74.97 F

 

Links
Adafruit MCP9808 High Accuracy I2C Temperature Sensor Breakout Board [ADA1782]

Share

LM35 temperature sensor

In this example we will connect an LM35 temperature sensor to our Arduino

The LM35 series are precision integrated-circuit temperature sensors, whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. The LM35 thus has an advantage over linear temperature sensors calibrated in Kelvin, as the user is not required to subtract a large constant voltage from its output to obtain convenient Centigrade scaling. The LM35 does not require any external calibration or trimming to provide typical accuracies of ±1/4°C at room temperature and ±3/4°C over a full -55 to +150°C  temperature range

Here is a picture of the pins, its important to get these correct or you can damage the sensor

LM35 temperature sensor pinout

LM35 temperature sensor pinout

I purchased a small module, which looks like this. This was clearly labelled and helps avoid any mistakes with wiring, I also prefer using cables to modules rather than breadboards

lm35 module

lm35 module

Schematics and Parts

You will need

Arduino Uno
LM35 sensor or module
Hook up wire (dupont cables)

Very simple to connect Vcc is 5v, Gnd is any Gnd and out goes to Arduino A0, you can see this below

Arduino and lm35 schematic

Arduino and lm35 schematic

Code

//initializes/defines the output pin of the LM35 temperature sensor
int outputpin= 0;
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
}
 
//main loop
void loop()
{
int rawvoltage= analogRead(outputpin);
float millivolts= (rawvoltage/1024.0) * 5000;
float celsius= millivolts/10;
Serial.print(celsius);
Serial.print(" degrees Celsius, ");
 
Serial.print((celsius * 9)/5 + 32);
Serial.println(" degrees Fahrenheit");
delay(1000);
 
}

Results

Here are the results via the serial monitor

arduino and lm35 serial output

arduino and lm35 serial output

 

Links

LM35 datasheet

 
5PCS LM35D Temperature Sensor TO92 Packing

Share

SHT21 humidity and temperature sensor example

The SHT21 is a low cost humidity and temperature sensor. Its an I2C device so again is very simple to connect to any arduino

This is the breakout for the sensor that I bought.

SHT21

SHT21

Here is a simple schematic, again be careful as Vcc is 3.3v with this device

Schematic

UNO and SHT21

UNO and SHT21

Lets look at the required libraries and a basic code example, there is not much to this to be honest most of the work is done in the library

Code

There are a couple of libraries to help you out

https://github.com/misenso/SHT2x-Arduino-Library
https://github.com/elechouse/SHT21_Arduino

I used the latter one

 

#include <Wire.h>
#include <SHT2x.h>
 
 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
}
 
void loop()
{
  Serial.print("Humidity: ");
  Serial.println(SHT2x.GetHumidity());
  Serial.print("Temperature(C): ");
  Serial.println(SHT2x.GetTemperature());
  Serial.println();
  delay(1000);
}

Links

these come in at about $7 for a useful breakout board with this sensor

Humidity Sensor – SHT21 Breakout Board

Share

DHT11 sensor example

In this example we will show a basic example of connecting a DHT11 Temperature and Humidity Sensor to your Arduino. This is a very nice little, low cost sensor which requires only a pull up resistor to VDD. This can easily be constructed on a mini bread board and then joined up to your Arduino. using some connecting cables

The DHT sensors are made of two parts, a capacitive humidity sensor and a thermistor. There is a chip inside that does some a/d conversion and outputs a digital signal with the temperature and humidity.

I wont go into the technical details of the DHT11, there is a link at the bottom to a PDF datasheet which explains all you need to know.

Schematic

Arduino and DHT11 schematic

Arduino and DHT11 schematic

Connections

Here is the breadboard layout

Arduino and DHT11 breadboard

Arduino and DHT11 breadboard

Code

You will need the DHT11 library from the DHT library. You could roll your own code but this saves a whole lot of time.

Download, extract and copy this to your Arduino libraries folder location. This is a simple example which outputs the humidity and temperature, place your fingers on the DHT11 and you will see the readings fluctuate

#include <dht11.h>

dht11 DHT;
//Pin 4 of Arduino to Data of DHT11
#define DHT11_PIN 4

void setup()
{
Serial.begin(9600);
int check;
Serial.print(“DHT11 STATUS – \t”);
check = DHT.read(DHT11_PIN);
//check status
switch (check)
{
case DHTLIB_OK:
Serial.print(“OK\n”);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print(“Checksum error\n”);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print(“Timeout error\n”);
break;
default:
Serial.print(“Unknown error\n”);
break;
}
}

void loop()
{

//humidity and temperature output
Serial.print(“Humidity is “);
Serial.print(DHT.humidity,1);
Serial.print(“\n”);
Serial.println(“Temperature is “);
Serial.println(DHT.temperature,1);

delay(1000);
}

Output

Upload the sketch above and open the Serial Monitor window

arduino and dht11 serial output

arduino and dht11 serial output

 

Parts List

Label	Part Type
DHT1	DHT11 Humitidy and Temperature Sensor	
Part1	Arduino Uno (Rev3)
R1	4.7k Ω

 

Links

DHT11 Humidity and Temperature Sensor from Amazon UK

DHT11 Humidity and Temperature Sensor from Amazon US

DHT11 Datasheet in PDF format

Share