Categories

Arduino and GY-21P module example

The GY-21P is an interesting module in that it combines a BMP280 sensor and an SI7021 sensor. The on-board BMP280+SI7021 sensor measures atmospheric pressure from 30kPa to 110kPa as well as relative humidity and temperature.

BMP280
Pressure range: 300-1100 hPa (9000 meters above sea level at -500m)
Relative accuracy (at 950 – 1050 hPa at 25 ° C): ± 0.12 hPa, equiv. to ± 1 m
Absolute accuracy (at (950 – 1050 hPa, 0 – +40 ° C): ± 0.12 hPa, equiv. To ± 1 m
Mains voltage: 1.8V – 3.6V
Power consumption: 2.7µA at 1Hz readout rate
Temperature range: -40 to + 85 ° C

SI7021
HVAC/R
Thermostats/humidistats
Respiratory therapy
White goods
Indoor weather stations
Micro-environments/data centers
Automotive climate control and defogging
Asset and goods tracking
Mobile phones and tablets
Size: 1.3*1cm/0.51*0.39″

Features:

Operation Voltage: 3.3V
I2C & SPI Communications Interface
Temp Range: -40C to 85C
Humidity Range: 0 – 100% RH, =-3% from 20-80%
Pressure Range: 30,000Pa to 110,000Pa, relative accuracy of 12Pa, absolute accuracy of 100Pa
Altitude Range: 0 to 30,000 ft (9.2 km), relative accuracy of 3.3 ft (1 m) at sea level, 6.6 (2 m) at 30,000 ft.

Code

I use a variety of Adafruit libraries, took the default examples and made the following out of them

https://github.com/adafruit/Adafruit_Sensor

https://github.com/adafruit/Adafruit_BMP280_Library

https://github.com/adafruit/Adafruit_Si7021

 

 

include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Si7021.h"
 
Adafruit_BMP280 bme; // I2C
Adafruit_Si7021 sensor = Adafruit_Si7021();
 
void setup() 
{
Serial.begin(9600);
Serial.println("BMP280 and SI7021 (GY-21p) test");
 
if (!bme.begin()) 
{ 
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
 
if (!sensor.begin()) 
{
Serial.println("Did not find Si7021 sensor!");
while (true);
}
}
 
void loop() 
{
Serial.println("BMP280 results");
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure());
Serial.println(" Pa");
Serial.print("Approx altitude = ");
Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
Serial.println(" m");
Serial.println();
 
Serial.println("SI7021 results");
Serial.print("Humidity: "); 
Serial.println(sensor.readHumidity(), 2);
Serial.print("Temperature: "); 
Serial.println(sensor.readTemperature(), 2);
Serial.println();
delay(2000);
}

 

Output

OPen the serial monitor

BMP280 results
Temperature = 27.26 *C
Pressure = 99042.63 Pa
Approx altitude = 191.78 m

SI7021 results
Humidity: 31.04
Temperature: 27.30

BMP280 results
Temperature = 28.43 *C
Pressure = 99044.82 Pa
Approx altitude = 191.60 m

SI7021 results
Humidity: 32.59
Temperature: 28.56

BMP280 results
Temperature = 28.17 *C
Pressure = 99037.41 Pa
Approx altitude = 192.22 m

SI7021 results
Humidity: 32.88
Temperature: 28.46

You can see the temperature output is close between the sensors

 

Links

Atmospheric Humidity Temperature Sensor Breakout Barometric Pressure BMP280 SI7021 for Arduino

Share

DHT12 temperature sensor and Arduino example

The DHt12 is an upgraded version of the classic DHT11 humidity temperature sensor, it is fully downward compatible, more precise and adds an I2C interface.

Features:

compact size
low power consumption
low voltage operation
Standard I2C and 1-wire interface.

Sensing range
Temperature: -20 ~ +60 C
Humidity: 20-95 RH
Humidity:
Resolution: 0.1%RH
Repeat: -+ 1%RH
Precision 25C @ -+5RH
Temperature:
Resolution: 0.1C
Repeat: -+0.2C
Precision: 25C @ -+0.5C
Power: DC 2.7-5.5V
Normal current 1mA
Standby current 60uA
Sample cycle: > 2 seconds

Pin interface: 1. VDD 2. SDA 3. GND 4. SCL (connect to GND when use as 1-wire)

 

 

Layout

This shows how to connect the DHT12 to an Arduino Uno

arduino and dht12

arduino and dht12

 

 

Code

This is from https://github.com/xreef/DHT12_sensor_library


 

#include "Arduino.h"
 
#include <DHT12.h>
 
// Set dht12 i2c comunication on default Wire pin
DHT12 dht12;
 
void setup()
{
Serial.begin(112560);
// Start sensor handshake
dht12.begin();
}
int timeSinceLastRead = 0;
 
void loop()
{
// Report every 2 seconds.
if(timeSinceLastRead > 2000) {
// Reading temperature or humidity takes about 250 milliseconds!
// Read temperature as Celsius (the default)
float t12 = dht12.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht12.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht12.readHumidity();
 
bool dht12Read = true;
// Check if any reads failed and exit early (to try again).
if (isnan(h12) || isnan(t12) || isnan(f12)) {
Serial.println("Failed to read from DHT12 sensor!");
 
dht12Read = false;
}
 
if (dht12Read){
// Compute heat index in Fahrenheit (the default)
float hif12 = dht12.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht12.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
float dpf12 = dht12.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
float dpc12 = dht12.dewPoint(t12, h12, false);
 
Serial.print("DHT12=> Humidity: ");
Serial.print(h12);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t12);
Serial.print(" *C ");
Serial.print(f12);
Serial.print(" *F\t");
Serial.print(" Heat index: ");
Serial.print(hic12);
Serial.print(" *C ");
Serial.print(hif12);
Serial.print(" *F");
Serial.print(" Dew point: ");
Serial.print(dpc12);
Serial.print(" *C ");
Serial.print(dpf12);
Serial.println(" *F");
}
timeSinceLastRead = 0;
}
delay(100);
timeSinceLastRead += 100;
 
}

 

 

 

Output

DHT12=> Humidity: 33.70 % Temperature: 20.20 *C 68.36 *F Heat index: 19.16 *C 66.48 *F Dew point: 3.60 *C 38.48 *F
DHT12=> Humidity: 39.70 % Temperature: 20.60 *C 69.08 *F Heat index: 19.75 *C 67.55 *F Dew point: 6.34 *C 43.41 *F
DHT12=> Humidity: 44.60 % Temperature: 21.20 *C 70.16 *F Heat index: 20.54 *C 68.97 *F Dew point: 8.64 *C 47.55 *F
DHT12=> Humidity: 47.80 % Temperature: 21.80 *C 71.24 *F Heat index: 21.28 *C 70.31 *F Dew point: 10.25 *C 50.45 *F
DHT12=> Humidity: 48.90 % Temperature: 22.40 *C 72.32 *F Heat index: 21.97 *C 71.55 *F Dew point: 11.16 *C 52.08 *F
DHT12=> Humidity: 53.90 % Temperature: 22.90 *C 73.22 *F Heat index: 22.65 *C 72.78 *F Dew point: 13.14 *C 55.66 *F
DHT12=> Humidity: 55.10 % Temperature: 23.60 *C 74.48 *F Heat index: 23.45 *C 74.22 *F Dew point: 14.15 *C 57.46 *F
DHT12=> Humidity: 54.90 % Temperature: 23.90 *C 75.02 *F Heat index: 23.78 *C 74.80 *F Dew point: 14.37 *C 57.86 *F
DHT12=> Humidity: 45.30 % Temperature: 24.40 *C 75.92 *F Heat index: 24.08 *C 75.34 *F Dew point: 11.80 *C 53.24 *F
DHT12=> Humidity: 40.60 % Temperature: 24.60 *C 76.28 *F Heat index: 24.18 *C 75.52 *F Dew point: 10.28 *C 50.50 *F
DHT12=> Humidity: 37.10 % Temperature: 24.80 *C 76.64 *F Heat index: 24.30 *C 75.75 *F Dew point: 9.09 *C 48.35 *F

 

 

Link

Under $1 for one of these sensors

1pcs DHT-12 DHT12 sensor Digital output Temperature and Humidity Sensor high quality

Share

Arduino and BMA250 acceleration sensor

The BMA250E is an advanced, ultra-small, triaxial, low-g acceleration sensor with digital interfaces, aiming for low-power consumer electronics applications. Featuring 10 bit digital resolution, the BMA250E allows low-noise measurement of accelerations in 3 perpendicular axes.

A typical module

technical Information

Parameter Technical data
Digital resolution 10 bit
Resolution
(in ±2g range)
3.9 mg
Measurement ranges
(programmable)
±2 g, ±4 g, ±8 g, ±16 g
Sensitivity (calibrated) ±2 g: 256 LSB/g
±4 g: 128 LSB/g
±8 g: 64 LSB/g
±16 g: 32 LSB/g
Zero-g offset (typ., over life-time) ±80 mg
Noise density (typ.) 400 μg/√Hz
Bandwidths (programmable) 1000 Hz … 8 Hz
Digital inputs/outputs SPI & I²C, 2x digital interrupt pins
Supply voltage (VDD) 1.62 V … 3.6 V
I/0 supply voltage (VDDIO) 1.2 V … 3.6 V
Temperature range -40 … +85°C
Current consumption
– full operation
– low-power mode
130 μA (@ 2 kHz data rate)
6.5 μA (@ 40 Hz data rate)
LGA package 2 x 2 x 0.95 mm³
Interrupts – Data-ready (e. g. for processor synchronization)
– Any-motion (slope) detection (e. g. for wake-up)
– Tap sensing (e. g. for tap-sensitive UI control)
– Orientation change recognition (e. g. for portrait/landscape switching)
– Flat detection (e. g. for position sensitive switching)
– Low-g / high-g detection (e. g. for shock and free-fall detection)
– No-motion (e.g. for power saving)

Connection

 

Arduino Module
3.3v Vcc
Gnd Gnd
A4 SDA
A5 SCL

 

Code

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// BMA250
// This code is designed to work with the BMA250_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Accelorometer?sku=BMA250_I2CS#tabs-0-product_tabset-2
 
#include <Wire.h>
 
// BMA250 I2C address is 0x18(24)
#define Addr 0x18
 
void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise Serial Communication, set baud rate = 9600
  Serial.begin(9600);
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select range selection register
  Wire.write(0x0F);
  // Set range +/- 2g
  Wire.write(0x03);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select bandwidth register
  Wire.write(0x10);
  // Set bandwidth 7.81 Hz
  Wire.write(0x08);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(300);
}
 
void loop()
{
  unsigned int data[0];
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Data Registers (0x02 − 0x07)
  Wire.write(0x02);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Request 6 bytes 
  Wire.requestFrom(Addr, 6);
 
  // Read the six bytes 
  // xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
  if(Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }
  delay(300);
 
  // Convert the data to 10 bits
  float xAccl = ((data[1] * 256.0) + (data[0] & 0xC0)) / 64;
  if (xAccl > 511)
  {
    xAccl -= 1024;
  }
  float yAccl = ((data[3] * 256.0) + (data[2] & 0xC0)) / 64;
  if (yAccl > 511)
  {
    yAccl -= 1024;
  }
  float zAccl = ((data[5] * 256.0) + (data[4] & 0xC0)) / 64;
  if (zAccl > 511)
  {
    zAccl -= 1024;
  }
 
  // Output data to the serial monitor
  Serial.print("Acceleration in X-Axis :");
  Serial.println(xAccl);
  Serial.print("Acceleration in Y-Axis :");
  Serial.println(yAccl);
  Serial.print("Acceleration in Z-Axis :");
  Serial.println(zAccl) ; 
}

Link

Share

Arduino and MLX90393 magnetic field sensor example

The MLX90393 magnetic field sensor can be reprogrammed to different modes and with different settings at run-time. The sensor offers a 16-bit output proportional to the magnetic flux density sensed along the XYZ axes using the Melexis proprietary Triaxis® technology and also offers a temperature output signal. These digital values are available via I2C and SPI, where the MLX90393 is a slave on the bus.

By selecting which axes are to be measured, the raw data can be used as input for further post-processing, such as for joystick applications, rotary knobs, and more complex 3D position sensing applications. Unparallelled performance is achieved with this sensor, which is primarily targeting industrial and consumer applications.

mlx90393 module

mlx90393 module

Connection

Module Arduino
VDD 3v3
Gnd Gnd
SDA A4
SCL A5

Code

 

 

 

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MLX90393
// This code is designed to work with the MLX90393_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/products
#include<Wire.h>
 
// MLX90393 I2C Address is 0x0C(12)
#define Addr 0x0C
 
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select Write register command
Wire.write(0x60);
// Set AH = 0x00, BIST disabled
Wire.write(0x00);
// Set AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5
Wire.write(0x5C);
// Select address register, (0x00 << 2)
Wire.write(0x00);
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read status byte
if(Wire.available() == 1)
{
unsigned int c = Wire.read();
}
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select Write register command
Wire.write(0x60);
// Set AH = 0x02
Wire.write(0x02);
// Set AL = 0xB4, RES for magnetic measurement = 0
Wire.write(0xB4);
// Select address register, (0x02 << 2)
Wire.write(0x08);
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read status byte
if(Wire.available() == 1)
{
unsigned int c = Wire.read();
}
delay(300);
}
 
void loop()
{
unsigned int data[7];
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Start single meaurement mode, ZYX enabled
Wire.write(0x3E);
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read status byte
if(Wire.available() == 1)
{
unsigned int c = Wire.read();
}
delay(100);
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Send read measurement command, ZYX enabled
Wire.write(0x4E);
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 7 bytes of data
Wire.requestFrom(Addr, 7);
 
// Read 7 bytes of data
// status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
if(Wire.available() == 7);
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
data[4] = Wire.read();
data[5] = Wire.read();
data[6] = Wire.read();
}
 
// Convert the data
int xMag = data[1] * 256 + data[2];
int yMag = data[3] * 256 + data[4];
int zMag = data[5] * 256 + data[6];
 
// Output data to serial monitor
Serial.print("Magnetic Field in X-Axis : ");
Serial.println(xMag);
Serial.print("Magnetic Field in Y-Axis : ");
Serial.println(yMag);
Serial.print("Magnetic Field in Z-Axis : ");
Serial.println(zMag);
delay(500);
}

 

 

 

Output

Open the serial monitor and you should see something like this

Magnetic Field in X-Axis : 66
Magnetic Field in Y-Axis : 65458
Magnetic Field in Z-Axis : 8
Magnetic Field in X-Axis : 83
Magnetic Field in Y-Axis : 65477
Magnetic Field in Z-Axis : 65525
Magnetic Field in X-Axis : 76
Magnetic Field in Y-Axis : 65452
Magnetic Field in Z-Axis : 65506
Magnetic Field in X-Axis : 25
Magnetic Field in Y-Axis : 65374
Magnetic Field in Z-Axis : 65512
Magnetic Field in X-Axis : 80

 

Links

https://www.melexis.com/-/media/files/documents/datasheets/mlx90393-datasheet-melexis.pdf

CJMCU-90393, MLX90393 digital 3D Holzer sensor, displacement, angle, rotation, 3D position

Share