Categories

B4r and Arduino SHT21 humidity and temperature sensor example

The digital SHT2x humidity sensor series is used in high volumes in a wide variety of applications and has today become the de facto industry standard. The SHT2x series consists of a low-cost version with the SHT20 humidity sensor, a standard version with the SHT21 humidity sensor, and a high-end version with the SHT25 humidity sensor. Its an I2C device so again is very simple to connect to any arduino

Features

Size 3 x 3 x 1.1 mm
Output I²C digital, PWM, SDM
Supply voltage range 2.1 to 3.6 V
Energy consumption 3.2µW (at 8 bit, 1 measurement / s)
RH operating range 0 – 100% RH
T operating range -40 to +125°C (-40 to +257°F)
RH response time 8 sec (tau63%)

This is the breakout for the sensor that I bought.

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

 

Schematic

 

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

 

You need to install the following library – https://github.com/markbeee/SHT21

#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 sht21humi As Float
Public sht21temp 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: ", sht21temp, "°C")
Log("Humidity is: ", sht21humi, " %")
End Sub
#if C
#include <Wire.h>
#include "SHT21.h"
SHT21 SHT21;
void setup(B4R::Object* o){
SHT21.begin();
}
void loop (B4R::Object* o) {
b4r_main::_sht21humi =SHT21.getHumidity();
b4r_main::_sht21temp =SHT21.getTemperature();
}
#End if

 

Output

In the log view

Temperature is: 20.9541°C
Humidity is: 47.1311 %
Temperature is: 25.0403°C
Humidity is: 48.8706 %
Temperature is: 26.7027°C
Humidity is: 51.6553 %
Temperature is: 27.8289°C
Humidity is: 50.8161 %
Temperature is: 28.6547°C
Humidity is: 50.0150 %
Temperature is: 29.2982°C
Humidity is: 50.1371 %
Temperature is: 29.7915°C
Humidity is: 49.0003 %
Temperature is: 30.2420°C
Humidity is: 49.0079 %
Temperature is: 30.1562°C
Humidity is: 59.5365 %

 

Links

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

Humidity Sensor – SHT21 Breakout Board

B4R Arduino LM35 temperature sensor example

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

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

 

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 A1, you can see this below

 

 

Code

Sub Process_Globals
Public Serial1 As Serial
Private SensorPin As Pin 'Output pin connected from the TMP36 sensor
Private SensorPinNumber As Byte = 0x01 'Pin number used is A1 (Analog)
Private MeasureTimer As Timer 'Timer for the sensor measurement
Private MeasureTimerInterval As ULong = 2 'Timerinterval in seconds
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart - Pin ", SensorPinNumber, " read every ", MeasureTimerInterval, " seconds")
'Init the pin with LM35 connected
SensorPin.Initialize(SensorPinNumber, SensorPin.MODE_OUTPUT)
'Init the timer
MeasureTimer.Initialize("MeasureTimer_Tick", MeasureTimerInterval * 1000)
'Start the timer
MeasureTimer.Enabled = True
End Sub
'Handle the timer ticks
Private Sub MeasureTimer_Tick
'Read the current state of the pin
Dim rawvoltage As UInt = SensorPin.AnalogRead
Log("Raw voltage: ", rawvoltage)
'Convert rawvoltage
'Voltage = (RawValue / 1023.0) * 5000; // 5000 To get millivots.
'tempC = Voltage * 0.1;
'tempF = (tempC * 1.8) + 32; // conver To F
Dim Voltage As Double = (rawvoltage/1023.0) * 5000
Dim tempC As Double = Voltage * 0.1
Log("Temperate (c) = ", tempC)
Dim tempF As Double = (tempC * 1.8) + 32
Log("Temperate (f) = ", tempF)
End Sub

 

Results

You should see the following

AppStart – Pin 1 read every 2 seconds
Temperate (c) = 21.5054
Temperate (f) = 70.7097
Raw voltage: 44
Temperate (c) = 21.5054
Temperate (f) = 70.7097
Raw voltage: 44
Temperate (c) = 21.5054
Temperate (f) = 70.7097
Raw voltage: 48
Temperate (c) = 23.4604
Temperate (f) = 74.2287
Raw voltage: 51
Temperate (c) = 24.9267
Temperate (f) = 76.8680
Raw voltage: 53
Temperate (c) = 25.9042
Temperate (f) = 78.6276
Raw voltage: 54
Temperate (c) = 26.3930
Temperate (f) = 79.5073

 

Links

LM35 datasheet

5PCS LM35D Temperature Sensor TO92 Packing

B4R Arduino and DS1624 temperature sensor

The DS1624 consists of two separate functional units: a 256-byte nonvolatile E2 memory and a direct-to-digital temperature sensor.

The nonvolatile memory is made up of 256 bytes of E2 memory. This memory can be used to store any type of information the user wishes. These memory locations are accessed through the 2-wire serial bus.

The direct-to-digital temperature sensor allows the DS1624 to measure the ambient temperature and report the temperature in a 12-bit word with 0.0625°C resolution. The temperature sensor and its related registers are accessed through the 2-wire serial interface. Figure 1 in the full data sheet shows a block diagram of the DS1624.

Features

Reduces Component Count with Integrated Temperature Sensor and Nonvolatile E2 Memory
Measures Temperatures from -55°C to +125°C in 0.0625°C Increments
±0.5°C Accuracy from 0°C to 70°C
256 Bytes of E2 Memory for Storing Information Such as Frequency Compensation Coefficients
No External Components
Easy-to-Use 2-Wire Serial Interface
Temperature is Read as a 12-Bit Value (2-Byte Transfer)
Available in 8-Pin SO and DIP Packages

 

Connection

Module Arduino
VDD 5v
Gnd Gnd
SDA SDA – A4
SCL SCL – A5

Code

Install https://github.com/federico-galli/Arduino-i2c-temperature-sensor-DS1624 in the Arduino IDE first

 

#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 dstemp 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: ", dstemp, "°C")
End Sub
#if C
#include <Wire.h>
#include "DS1624.h"
DS1624 temperature(0x48);
void setup(B4R::Object* o){
temperature.start();
}
void loop (B4R::Object* o) {
b4r_main::_dstemp =temperature.getTemp();
}
#End if

 

Testing

 

Temperature is: 30.3125°C
Temperature is: 30.4375°C
Temperature is: 30.3750°C
Temperature is: 30.2187°C
Temperature is: 30.1250°C
Temperature is: 30.0625°C
Temperature is: 29.9687°C
Temperature is: 29.9375°C
Temperature is: 29.3750°C
Temperature is: 29.6562°C
Temperature is: 30.6562°C
Temperature is: 30.5000°C

 

 

Link

https://datasheets.maximintegrated.com/en/ds/DS1624.pdf

CJMCU-1624 DS1624 temperature sensor, high precision digital thermometer with memory function

B4R and Arduino 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

 

Connection

 

arduino and mcp9808

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

 

 

#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 <Wire.h>
#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

 

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

B4R and Arduino HDC1008 temperature sensor example

The HDC1000 is a digital humidity sensor with integrated temperature sensor that provides excellent measurement accuracy at very low power. The device measures humidity based on a novel capacitive sensor. The humidity and temperature sensors are factory calibrated.

The sensing element of the HDC1000 is placed on the bottom part of the device, which makes the HDC1000 more robust against dirt, dust, and other environmental contaminants. The HDC1000 is functional within the full –40°C to +125°C temperature range.

Key Features

  • Relative Humidity (RH) Operating Range 0% to 100%
  • 14 Bit Measurement Resolution
  • Relative Humidity Accuracy ±3%
  • Temperature Accuracy ±0.2°C
  • Supply Voltage 3 V to 5 V
  • I2C Interface

Connection information

The easiest way is to purchase some sort of breakout, here is how you connect the module to your Arduino

arduino and hdc1008

arduino and hdc1008

 

Code

Import the Adafruit HDC 1008 libraries into the ARduino as you would normally do – these are available from https://github.com/adafruit/Adafruit_HDC1000_Library

This example uses some inline c which makes the job of re-using existing libraries easy

#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 hdctemp AS Float
	Public hdchumi 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: ", hdctemp, "°C")
	LOG("Humidity is: ", hdchumi, " %")
END SUB
 
#if C
#include <Wire.h>
#include "Adafruit_HDC1000.h"
Adafruit_HDC1000 hdc = Adafruit_HDC1000();
void setup(B4R::Object* o){
hdc.begin();
}
 
void LOOP (B4R::Object* o) {
 
   b4r_main::_hdchumi =hdc.readHumidity();
   b4r_main::_hdctemp =hdc.readTemperature();
}
 
#End IF

 

Look at the log window after you compile and run, you should see this

 

Temperature is: 25.2185°C
Humidity is: 30.8899 %
Temperature is: 25.1581°C
Humidity is: 30.4077 %
Temperature is: 25.0977°C
Humidity is: 29.8279 %
Temperature is: 25.0574°C
Humidity is: 29.3396 %
Temperature is: 24.9969°C
Humidity is: 28.9368 %
Temperature is: 24.9466°C
Humidity is: 28.4546 %
Temperature is: 24.8761°C
Humidity is: 28.0701 %
Temperature is: 24.8459°C
Humidity is: 27.6794 %
Temperature is: 24.8157°C
Humidity is: 27.2949 %
Temperature is: 24.7653°C
Humidity is: 26.9043 %

 

 

Links

Adafruit HDC1008 Temperature & Humidity Sensor Breakout Board