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 GA1A12S202 light sensor example

In this example we will connect a GA1A12S202 Log-Scale Analog Light Sensor to an Arduino.

The features of this sensor are as follows

Output voltage increases with light on the sensor
Logarithmic response not only gives more sensitivity in low light, its also almost impossible to “max-out” the sensor
Dynamic range of 3 to 55,000 Lux
Use indoors and outdoors without needing to recalibrate!

Again these are typically best used in breakout/module form. Here is a picture of the module

You connect the sensor as follows

Vcc – 5v
Gnd – Gnd
Out – A1 (or you can use another analog in pin but you would need to change the code example)

Layout

 

arduino and GA1A12S202

arduino and GA1A12S202

 

Code

 

Sub Process_Globals
Public Serial1 As Serial
Private SensorPin As Pin 'Output pin connected
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 GA1a12s02 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
Dim loglux As Double = (rawvoltage*5.0)/1024.0
Dim luxvalue As Double = Power(10,loglux)
Log("Lux: ", luxvalue)
End Sub

 

Results

You should see something like the following

AppStart – Pin 1 read every 2 seconds
Raw voltage: 382
Lux: 73.3220
Raw voltage: 382
Lux: 73.3220
Raw voltage: 383
Lux: 74.1510
Raw voltage: 108
Lux: 3.3678
Raw voltage: 274
Lux: 21.7716
Raw voltage: 346
Lux: 48.9162
Raw voltage: 360
Lux: 57.2549
Raw voltage: 366
Lux: 61.2505
Raw voltage: 367
Lux: 61.9430

 

Links

Adafruit GA1A12S202 Log-scale Analog Light Sensor [ADA1384]

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 Arduino MLX90614 infrared thermometer example

The MLX90614 is a non-contact infrared thermometer with a measurement range from -70 to +380 degree Celsius. Just connect the four leads to your Arduino and you will have a accurate thermometer with a resolution of 0.01 and a accuracy of 0.5 degrees, or for that matter you can use any microcontroller that can communicate with it through it’s I2C interface.

Being an I2C device you simply need to connect to the SDA, SCL and choose a suitable GND and Vin. I used 3.3v to be safe, although the breakout states 3 to 5v.

This version I chose comes with a breakout board with all of the components needed for operation. Here is a picture of that breakout board

Features:

Small size, low cost
Mounted on a breakout board with two types of pins
10k Pull up resistors for the I2C interface with optional solder jumpers
Factory calibrated in wide temperature range:
-40 … + 125 ° C for sensor temperature and
-70 … + 380 ° C for object temperature.
High accuracy of 0.5 ° C over wide temperaturerange (0 … + 50 ° C for both Ta and To) High (medical) accuracy calibration
Measurement resolution of 0.02 ° C
Single and dual zone versions
SMBus compatible digital interface
Customizable PWM output for continuous reading
Sleep mode for reduced power consumption

 

Layout

 

arduino and mlx90614

arduino and mlx90614

 

 

Code

 

The sketch below is fairly straightforward, most of the work is done in the Adafruit MLX96014 library

#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 mlxambtempc As Float
Public mlxobjtempc As Float
Public mlxambtempf As Float
Public mlxobjtempf 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("Ambient temperature (c) is: ", mlxambtempc, "°C")
Log("Ambient temperature (f) is: ", mlxambtempf, "°F")
Log("Object temperature (c) is: ", mlxobjtempc, "°C")
Log("Object temperature (f) is: ", mlxobjtempf, "°F")
End Sub
#if C
#include <Wire.h>
#include "Adafruit_MLX90614.h"
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup(B4R::Object* o){
mlx.begin();
}
void loop (B4R::Object* o) {
b4r_main::_mlxambtempc =mlx.readAmbientTempC();
b4r_main::_mlxobjtempc =mlx.readObjectTempC();
b4r_main::_mlxambtempf =mlx.readAmbientTempF();
b4r_main::_mlxobjtempf =mlx.readObjectTempF();
}
#End if

 

Output

Here is the output

Ambient temperature (c) is: 22.0700°C
Ambient temperature (f) is: 71.7260°F
Object temperature (c) is: 28.2500°C
Object temperature (f) is: 82.8500°F
Ambient temperature (c) is: 22.0700°C
Ambient temperature (f) is: 71.7260°F
Object temperature (c) is: 26.1700°C
Object temperature (f) is: 79.1060°F
Ambient temperature (c) is: 22.0700°C
Ambient temperature (f) is: 71.7260°F
Object temperature (c) is: 26.8500°C
Object temperature (f) is: 80.3300°F
Ambient temperature (c) is: 22.0700°C
Ambient temperature (f) is: 71.7260°F
Object temperature (c) is: 22.9500°C
Object temperature (f) is: 73.3100°F

 

Links

Here is a link to the datasheet and also the breakout I purchased, come in at about $9 a piece.

MLX90614 datasheet
MLX90614 Contactless IR Infrared Thermometer Sensor Module IIC for Arduino