ESP8266 and MS5611 barometric pressure sensor example

In this example we look at the MS5611 barometric pressure sensor

This barometric pressure sensor is optimized for altimeters and variometers with an altitude resolution of 10 cm. The sensor module includes a high linearity pressure sensor and an ultra-low power 24 bit ΔΣ ADC with internal factory calibrated coefficients. It provides a precise digital 24 Bit pressure and temperature value and different operation modes that allow the user to optimize for conversion speed and current consumption. A high resolution temperature output allows the implementation of an altimeter/thermometer function without any additional sensor.

 

features

 

  • High resolution module, 10 cm
  • Fast conversion down to 1 ms
  • Low power, 1 µA (standby < 0.15 µA)
  • QFN package 5.0 x 3.0 x 1.0 mm3
  • Supply voltage 1.8 to 3.6 V
  • Integrated digital pressure sensor (24 bit ΔΣ ADC)
  • Operating range: 10 to 1200 mbar, -40 to +85 °C
  • I2C and SPI interface up to 20 MHz
  • No external components (Internal oscillator)
  • Excellent long term stability

 

 

Connection

Wemos Module connection
3.3v Vcc
Gnd Gnd
SCL SCL
SDA SDA

 

Code

This example is adapted from the https://github.com/jarzebski/Arduino-MS5611 library

#include <Wire.h>
#include <MS5611.h>
 
MS5611 ms5611;
 
double referencePressure;
 
void setup() 
{
  Serial.begin(9600);
 
  // Initialize MS5611 sensor
  Serial.println("Initialize MS5611 Sensor");
 
  while(!ms5611.begin())
  {
    Serial.println("Could not find a valid MS5611 sensor, check wiring!");
    delay(500);
  }
 
  // Get reference pressure for relative altitude
  referencePressure = ms5611.readPressure();
 
  // Check settings
  checkSettings();
}
 
void checkSettings()
{
  Serial.print("Oversampling: ");
  Serial.println(ms5611.getOversampling());
}
 
void loop()
{
  // Read raw values
  uint32_t rawTemp = ms5611.readRawTemperature();
  uint32_t rawPressure = ms5611.readRawPressure();
 
  // Read true temperature & Pressure
  double realTemperature = ms5611.readTemperature();
  long realPressure = ms5611.readPressure();
 
  // Calculate altitude
  float absoluteAltitude = ms5611.getAltitude(realPressure);
  float relativeAltitude = ms5611.getAltitude(realPressure, referencePressure);
 
  Serial.println("--");
 
  Serial.print(" rawTemp = ");
  Serial.print(rawTemp);
  Serial.print(", realTemp = ");
  Serial.print(realTemperature);
  Serial.println(" *C");
 
  Serial.print(" rawPressure = ");
  Serial.print(rawPressure);
  Serial.print(", realPressure = ");
  Serial.print(realPressure);
  Serial.println(" Pa");
 
  Serial.print(" absoluteAltitude = ");
  Serial.print(absoluteAltitude);
  Serial.print(" m, relativeAltitude = ");
  Serial.print(relativeAltitude);    
  Serial.println(" m");
 
  delay(1000);
}

 

Output

Open the serial monitor and you will see something like this

Temp = 25.85 *C
Pressure = 101704 Pa
absoluteAltitude = -31.51 m, relativeAltitude = -0.75 m

Temp = 27.66 *C
Pressure = 101710 Pa
absoluteAltitude = -32.00 m, relativeAltitude = -1.24 m

Temp = 28.35 *C
Pressure = 101703 Pa
absoluteAltitude = -31.42 m, relativeAltitude = -0.66 m

Temp = 27.80 *C
Pressure = 101693 Pa
absoluteAltitude = -30.59 m, relativeAltitude = 0.17 m

Temp = 27.47 *C
Pressure = 101687 Pa
absoluteAltitude = -30.09 m, relativeAltitude = 0.66 m

 

Link

GY-63 MS5611-01BA03 Precision MS5611 Atmospheric Pressure Sensor Module Height Sensor Module

TMP175 digital temperature sensor and ESP8266 example

Another sensor or module to cross our path, this time we will look at the TMP175 digital temperature sensors

We will connect one of these up to one of our trusty Wemos Mini boards

The TMP175 devices is a digital temperature sensors ideal for NTC and PTC thermistor replacement. The device offers a typical accuracy of ±1°C without requiring calibration or external component signal conditioning. IC temperature sensors are highly linear and do not require complex calculations or look-up tables to derive the temperature. The on-chip 12-bit ADC offers resolutions down to 0.0625°C.

The TMP175  feature SMBus, Two-Wire, and I2C interface compatibility. The TMP175 device allows up to 27 devices on one bus. . The TMP175  feature an SMBus Alert function.

The TMP175 are ideal for extended temperature measurement in a variety of communication, computer, consumer, environmental, industrial, and instrumentation applications.

Features

  • TMP175: 27 Addresses
  • Digital Output: SMBus™, Two-Wire™, and I2C
    Interface Compatibility
  • Resolution: 9 to 12 Bits, User-Selectable
  • Accuracy:
    • ±1°C (Typical) from –40°C to 125°C
    • ±2°C (Maximum) from –40°C to 125°C
  • Low Quiescent Current: 50-µA, 0.1-µA Standby
  • Wide Supply Range: 2.7 V to 5.5 V
  • Small 8-Pin MSOP and 8-Pin SOIC Packages

 

Connection

You can use 5v but I just decided on 3v3

Module Connection Wemos (ESP8266) Connection
VCC 3v3
GND Gnd
SDA SDA
SCL SCL

 

Code

#include <Wire.h> 
 
byte TempHi;              // Variable hold data high byte
byte TempLo;              // Variable hold data low byte
boolean P_N;              // Bit flag for Positive and Negative
unsigned int Decimal;     // Variable hold decimal value
 
void Cal_Temp();
/*******************************************************************************
                      Setup
*******************************************************************************/ 
void setup() 
{ 
  Serial.begin(9600);
  Wire.begin();             // join i2c bus (address optional for master) 
  delay(1000);
} 
 
/*******************************************************************************
                      Main Loop
*******************************************************************************/  
void loop() 
{
  const int I2C_address = 0x37;  // I2C write address 
 
  delay(100);
  Wire.beginTransmission(I2C_address);
  Wire.write(1);             // Setup configuration register
  Wire.write(0x60);          // 12-bit
  Wire.endTransmission(); 
 
  Wire.beginTransmission(I2C_address);
  Wire.write(0);             // Setup Pointer Register to 0
  Wire.endTransmission(); 
 
  while (1)
  {
    delay(1000);
 
    // Read temperature value
    Wire.requestFrom(I2C_address, 2);
    while(Wire.available())          // Checkf for data from slave
    {                                
      TempHi = Wire.read();       // Read temperature high byte
      TempLo = Wire.read();       // Read temperature low byte
    } 
    Cal_Temp ();
 
    // Display temperature
    Serial.print("The temperature is ");
    if (P_N == 0)
      Serial.print("-");
    Serial.print(TempHi,DEC);
    Serial.print(".");
    Serial.print(Decimal,DEC);
    Serial.println(" degree C");
  }  
}
 
void Cal_Temp()
{
  if (TempHi&0x80)          // If bit7 of the TempHi is HIGH then the temperature is negative
    P_N = 0;
  else                      // Else the temperature is positive
    P_N = 1;
 
  TempHi = TempHi & 0x7F;   // Remove sign
  TempLo = TempLo & 0xF0;   // Filter out last nibble
  TempLo = TempLo >>4;      // Shift right 4 times
  Decimal = TempLo;
  Decimal = Decimal * 625;  // Each bit = 0.0625 degree C
 
}

 

Output

Open up the trusty serial monitor and you will see something like this all going well

The temperature is 27.0 degree C
The temperature is 27.0 degree C
The temperature is 28.0 degree C
The temperature is 28.5000 degree C
The temperature is 29.0 degree C
The temperature is 28.5000 degree C
The temperature is 28.0 degree C

 

Link

1pcs CJMCU-175 TMP175 27 Address Digital Temperature Sensor

ESP8266 and BMA250 acceleration sensor example

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 that you can buy

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

 

Wemos Module
3.3v Vcc
Gnd Gnd
D2 SDA
D1 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

ESP8266 and bme280 temperature sensor example

The BME280 is a great new chip which was originally designed for the next generation of smartphones. It is made up of a very accurate pressure sensor and an associated temperature sensor which helps calibrate the pressure readings.

And just for fun they threw in a pretty solid humidity sensor in there as well! So with an I2C connection you have access to enough weather data to make some pretty good predictions for your local area.

Or you can just use the pressure sensor with it’s abililty to discern the difference in 7.5cm in altitude.

The chip contains smarts to smooth out measurements

Connection:

vin—————-3v3
GND————–GND
SCL—————-D1
SDA—————-D2

Just like this

esp8266 and bme280

esp8266 and bme280

 

Code

No libraries required

#include<Wire.h>
 
// BME280 I2C address is 0x76(108)
#define Addr 0x76
 
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise Serial communication, set baud rate = 9600
Serial.begin(9600);
}
 
void loop()
{
unsigned int b1[24];
unsigned int data[8];
unsigned int dig_H1 = 0;
for(int i = 0; i < 24; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((136+i));
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 24 bytes of data
if(Wire.available() == 1)
{
b1[i] = Wire.read();
}
}
 
// Convert the data
// temp coefficients
unsigned int dig_T1 = (b1[0] & 0xff) + ((b1[1] & 0xff) * 256);
int dig_T2 = b1[2] + (b1[3] * 256);
int dig_T3 = b1[4] + (b1[5] * 256);
 
// pressure coefficients
unsigned int dig_P1 = (b1[6] & 0xff) + ((b1[7] & 0xff ) * 256);
int dig_P2 = b1[8] + (b1[9] * 256);
int dig_P3 = b1[10] + (b1[11] * 256);
int dig_P4 = b1[12] + (b1[13] * 256);
int dig_P5 = b1[14] + (b1[15] * 256);
int dig_P6 = b1[16] + (b1[17] * 256);
int dig_P7 = b1[18] + (b1[19] * 256);
int dig_P8 = b1[20] + (b1[21] * 256);
int dig_P9 = b1[22] + (b1[23] * 256);
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(161);
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 1 byte of data
if(Wire.available() == 1)
{
dig_H1 = Wire.read();
}
 
for(int i = 0; i < 7; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((225+i));
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 7 bytes of data
if(Wire.available() == 1)
{
b1[i] = Wire.read();
}
}
 
// Convert the data
// humidity coefficients
int dig_H2 = b1[0] + (b1[1] * 256);
unsigned int dig_H3 = b1[2] & 0xFF ;
int dig_H4 = (b1[3] * 16) + (b1[4] & 0xF);
int dig_H5 = (b1[4] / 16) + (b1[5] * 16);
int dig_H6 = b1[6];
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select control humidity register
Wire.write(0xF2);
// Humidity over sampling rate = 1
Wire.write(0x01);
// Stop I2C Transmission
Wire.endTransmission();
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select control measurement register
Wire.write(0xF4);
// Normal mode, temp and pressure over sampling rate = 1
Wire.write(0x27);
// Stop I2C Transmission
Wire.endTransmission();
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select config register
Wire.write(0xF5);
// Stand_by time = 1000ms
Wire.write(0xA0);
// Stop I2C Transmission
Wire.endTransmission();
 
for(int i = 0; i < 8; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((247+i));
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 8 bytes of data
if(Wire.available() == 1)
{
data[i] = Wire.read();
}
}
 
// Convert pressure and temperature data to 19-bits
long adc_p = (((long)(data[0] & 0xFF) * 65536) + ((long)(data[1] & 0xFF) * 256) + (long)(data[2] & 0xF0)) / 16;
long adc_t = (((long)(data[3] & 0xFF) * 65536) + ((long)(data[4] & 0xFF) * 256) + (long)(data[5] & 0xF0)) / 16;
// Convert the humidity data
long adc_h = ((long)(data[6] & 0xFF) * 256 + (long)(data[7] & 0xFF));
 
// Temperature offset calculations
double var1 = (((double)adc_t) / 16384.0 - ((double)dig_T1) / 1024.0) * ((double)dig_T2);
double var2 = ((((double)adc_t) / 131072.0 - ((double)dig_T1) / 8192.0) *
(((double)adc_t)/131072.0 - ((double)dig_T1)/8192.0)) * ((double)dig_T3);
double t_fine = (long)(var1 + var2);
double cTemp = (var1 + var2) / 5120.0;
double fTemp = cTemp * 1.8 + 32;
 
// Pressure offset calculations
var1 = ((double)t_fine / 2.0) - 64000.0;
var2 = var1 * var1 * ((double)dig_P6) / 32768.0;
var2 = var2 + var1 * ((double)dig_P5) * 2.0;
var2 = (var2 / 4.0) + (((double)dig_P4) * 65536.0);
var1 = (((double) dig_P3) * var1 * var1 / 524288.0 + ((double) dig_P2) * var1) / 524288.0;
var1 = (1.0 + var1 / 32768.0) * ((double)dig_P1);
double p = 1048576.0 - (double)adc_p;
p = (p - (var2 / 4096.0)) * 6250.0 / var1;
var1 = ((double) dig_P9) * p * p / 2147483648.0;
var2 = p * ((double) dig_P8) / 32768.0;
double pressure = (p + (var1 + var2 + ((double)dig_P7)) / 16.0) / 100;
 
// Humidity offset calculations
double var_H = (((double)t_fine) - 76800.0);
var_H = (adc_h - (dig_H4 * 64.0 + dig_H5 / 16384.0 * var_H)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * var_H * (1.0 + dig_H3 / 67108864.0 * var_H)));
double humidity = var_H * (1.0 - dig_H1 * var_H / 524288.0);
if(humidity > 100.0)
{
humidity = 100.0;
}
else if(humidity < 0.0)
{
humidity = 0.0;
}
 
// Output data to serial monitor
Serial.print("Temperature in Celsius : ");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit : ");
Serial.print(fTemp);
Serial.println(" F");
Serial.print("Pressure : ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Relative Humidity : ");
Serial.print(humidity);
Serial.println(" RH");
delay(1000);
}

 

Output

In the serial monitor you should see something along these lines

Temperature in Celsius : 22.10 C
Temperature in Fahrenheit : 71.77 F
Pressure : 1162.21 hPa
Relative Humidity : 0.00 RH
Temperature in Celsius : 27.18 C
Temperature in Fahrenheit : 80.93 F
Pressure : 1110.28 hPa
Relative Humidity : 0.00 RH
Temperature in Celsius : 28.71 C
Temperature in Fahrenheit : 83.67 F
Pressure : 1080.85 hPa
Relative Humidity : 0.00 RH
Temperature in Celsius : 29.91 C
Temperature in Fahrenheit : 85.84 F
Pressure : 1053.26 hPa
Relative Humidity : 0.00 RH

 

Links