ESP8266 and BMA400 acceleration sensor example

In this article we look at another acceleration sensor – this time its the BMA400

Lets take a look at the sensor and its features

Description

The BMA400 is the first real ultra-low power acceleration sensor without compromising on performance. Featuring 12-bit digital resolution, continuous measurement and a defined selectable bandwidth combined with ultra-low power the BMA400 allows low-noise measurement of accelerations in three perpendicular axes. The BMA400 thus senses tilt, orientation, tab/double tab, and enables plug ’n’ play step counting with activity recognition especially suited for wearable devices, which need a long-lasting battery lifetime.

Thanks to the continuous measurement principle and always-defined bandwidth, the BMA400 is the ideal solution for smart home applications such as smart indoor climate systems and smart home security systems. In the latter, the BMA400 can distinguish between real alarm situations like broken glass and false signals coming from random vibrations. Thereby, the new acceleration sensor avoids false alarms.

Features

Parameter Technical data
Measurement range ±2 g, ±4 g, ±8 g, ±16 g
Digital resolution 12 bit
Output Data Rate (ODR) 12.5 Hz to 800 Hz
Low path filter bandwidth Selectable 0.48xODR or 0.24xODR
Current consumption (independent from ODR due to continuous measurement) Max. performance: 14.5 μA
Typical use case: 5.8 μA
Low power use case: 3.5μA
Noise density Max. performance: 180 μg/√Hz (Z: x 1.45)
Typical use case: 300 μg/√Hz (Z: x 1.45)
Low power: 415 μg/√Hz (Z: x 1.45)
Ultra low power / Auto-wake-up mode 800 nA @ 25 Hz ODR
Embedded features
  • Step counter (< 4 μA overall)
  • Activity recognition (walking, running, standing still)
  • Activity change
  • Orientation
  • Tab/Double tab (< 8 μA overall)
  • General interrupt 1 and 2 (programmable via thresholds, timer, logical AND/OR operations)
  • 1 kB FIFO
Offset ±80 mg
TCO ±1 mg/K
Interface SPI & I²C & 2 Interrupt pins
Supply voltage 1.71 V up to 3.6 V

Parts Required

 

Name Link
Wemos Mini D1 mini – ESP8266 by WeMos
BMA400 BMA400 Acceleration Sensor
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

 

esp8266 and BMA400
esp8266 and BMA400

 

Code Example

This uses the library from https://github.com/Seeed-Studio/Grove_3Axis_Digital_Accelerometer_BMA400

No need to change the default code example – it worked just fine

#include "BMA400.h"
 
float x = 0, y = 0, z = 0;
int16_t temp = 0;
 
void setup(void)
{
    Wire.begin();
 
    Serial.begin(115200);
    while(!Serial);
    Serial.println("BMA400 Raw Data");
 
    while(1)
    {
        if(bma400.isConnection())
        {
            bma400.initialize();
            Serial.println("BMA400 is connected");
            break;
        }
        else Serial.println("BMA400 is not connected");
 
        delay(2000);
    }
}
 
void loop(void)
{
    bma400.getAcceleration(&x, &y, &z);
    temp = bma400.getTemperature();
 
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Serial.print(",");
    Serial.print(z);
    Serial.print(",");
 
    Serial.print(temp);
 
    Serial.println();
 
    delay(50);
}

 

Output

Open the serial monitor and you should see something like this when you move the sensor about

189.45,876.95,373.05,32
197.27,884.77,376.95,32
197.27,896.48,382.81,23
183.59,888.67,357.42,31
0.00,0.00,0.00,23
185.55,888.67,380.86,31
0.00,0.00,0.00,23
195.31,904.30,378.91,31
201.17,892.58,380.86,31
234.37,865.23,369.14,31
197.27,867.19,396.48,31

 

Links

https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMA400-DS000.pdf

https://github.com/BoschSensortec/BMA400-API

https://www.bosch-sensortec.com/bst/products/all_products/bma400_1

 

 

ESP8266 and BMM150 digital geomagnetic sensor example

In this article we look at a digital geomagnetic sensor – this time its the BMM150 and as usual we will connect this to a Wemos Mini

Lets look at the manufacturers blurb about this sensor

Description

BMM150 is a low power and low noise 3-axis digital geomagnetic sensor to be used in compass applications. The 12-pin wafer level chip scale package (WLCSP) with a footprint of 1.56 x 1.56 mm² and 0.60 mm height provides highest design flexibility to the developer of mobile devices.

Applications like virtual reality or gaming on mobile devices such as mobile phones, tablet PCs or portable media players require 9-axis inertial sensing including magnetic heading information. Due to the stable performance over a large temperature range, the BMM150 is also especially suited for supporting drones in accurate heading.

BMM150 can be used with an inertial measurement unit (IMU) consisting of a 3-axis accelerometer and a 3-axis gyroscope like Bosch Sensortec’s BMI055.

 

Features

Parameter Technical data
Package CSWLP- (12 pin)
1.56×1.56×0.6 mm³
0.4 mm diagonal ball pitch
Temperature range -40°C … +85°C
Digital interfaces I²C and SPI
(2 interrupt pins)
Resolution 0.3μT
Supply voltage VDD: 1.62V to 3.6V
VDDIO: 1.2V to 3.6V
Zero-B offset ±50μT
Non-linearity <1% FS
Magnetic range typ. ±1300μT (x,y-axis)
±2500μT (z-axis)
Average current consumption 170 μA (low power preset)
500 μA (normal mode)
Interrupts New data, magnetic threshold high / low

Parts Required

You can pick up all these parts for about $5

Name Link
Wemos Mini D1 mini – ESP8266 by WeMos
BMM150 BMM150 Geomagnetic Sensor Breakout Board
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

Nothing complex here, just remember to use 3.3v

esp8266 and bmm150
esp8266 and bmm150

 

Code Example

This uses the library from https://github.com/Seeed-Studio/Grove_3_Axis_Compass_V2.0_BMM150

This is the default example which worked just fine

#include <Arduino.h>
#include <Wire.h>
// libraries
#include "bmm150.h"
#include "bmm150_defs.h"
 
BMM150 bmm = BMM150();
 
void setup()
{
  Serial.begin(9600);
 
  if(bmm.initialize() == BMM150_E_ID_NOT_CONFORM) 
  {
    Serial.println("Chip ID can not read!");
    while(1);
  } 
  else 
  {
    Serial.println("Initialize done!");
  }
 
}
 
void loop()
{
  bmm150_mag_data value;
  bmm.read_mag_data();
 
  value.x = bmm.raw_mag_data.raw_datax;
  value.y = bmm.raw_mag_data.raw_datay;
  value.z = bmm.raw_mag_data.raw_dataz;
 
  float xyHeading = atan2(value.x, value.y);
  float zxHeading = atan2(value.z, value.x);
  float heading = xyHeading;
 
  if(heading < 0)
  heading += 2*PI;
  if(heading > 2*PI)
  heading -= 2*PI;
  float headingDegrees = heading * 180/M_PI;
  float xyHeadingDegrees = xyHeading * 180 / M_PI;
  float zxHeadingDegrees = zxHeading * 180 / M_PI;
 
  Serial.print("Heading: ");
  Serial.println(headingDegrees);
 
  delay(100);
}

 

Output

Open the serial monitor and you should see something like this – I was moving the module around. If you tie this up with an LCD you could make a digital compass – in fact that sounds a good idea for another article

Heading: 60.49
Heading: 56.58
Heading: 53.62
Heading: 37.93
Heading: 37.98
Heading: 38.95
Heading: 39.45
Heading: 42.31
Heading: 36.98
Heading: 18.60
Heading: 10.92
Heading: 355.60
Heading: 348.37
Heading: 351.50
Heading: 352.01
Heading: 29.29
Heading: 64.44

 

Links

https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMM150-DS001.pdf

https://github.com/BoschSensortec/BMM150-Sensor-API

 

ESP8266 and a BH1745NUC Luminance and Colour Sensor

In this article we look at a BH1745NUC Luminance and Colour Sensor and connect it to a Wemos Mini

First of all lets take a look at the sensor – most of the info is on the manufacturers website

The BH1745NUC is digital color sensor IC with I²C bus interface. This IC senses Red, Green and Blue light (RGB) and converts them to digital values. The high sensitivity, wide dynamic range and excellent Ircut characteristics makes this IC the most suitable to obtain the illuminance and color temperature of ambient light for adjusting LCD backlight of TV, mobile phone and tablet PC. It is possible to detect very wide range light intensity. (0.005 – 40k lx)

Specifications:

VCC Voltage Range: 2.3V to 3.6V
Maximum Sensitivity: 0.005Lx/step
Current Consumption: 130μA (Typ)
Standby Mode Current: 0.8μA (Typ)
Operating Temperature Range: -40°C to +85°C

Features

The High Sensitivity and Wide Dynamic Range (0.005 – 40k lx)
Supports Low Transmittance (Dark) Window
Correspond to I²C Bus Interface
Low Current by Power Down Function
Rejecting 50Hz/60Hz Light Noise
Correspond to 1.8V Logic Interface
Programmable Interrupt Function
It is possible to select 2 type of I²C bus slave address (ADDR =’L’: “0111000”, ADDR =’H’: “0111001”)

Here is a typical module that I used

 

 

Parts Required

I connected a sensor shield to an Arduino and then the sensor via connecting wire

Name Link
Wemos Mini
BH1745NUC BH1745NUC Digital Color Sensor RGB Detecting Sensor Light Module
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Schematic/Connection

Be careful as I used a CJMCU-1745 – the sensor is rated at 2.3V to 3.6V. So use the 3.3v output

 

Code Example

This is a controleverything example – they have code examples for various platforms. It does not use a library and I always think these are good for learning especially when you pair this up with the datasheet and how they build the code. An example is the following line

// Select mode control register1
Wire.write(0x41);

#include <Wire.h>

// I2C address of the BH1745NUC
#define Addr 0x38

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 mode control register1
    Wire.write(0x41);
    // Set RGBC measurement time 160 msec
    Wire.write(0x00);
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select mode control register2
    Wire.write(0x42);
    // Set measurement mode is active, gain = 1x
    Wire.write(0x90);
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select mode control register3
    Wire.write(0x44);
    // Set default value
    Wire.write(0x02);
    // Stop I2C Transmission
    Wire.endTransmission();
    delay(300);
}

void loop()
{
    unsigned int data[8];
    for(int i = 0; i < 8; i++)
    {
        // Start I2C Transmission
        Wire.beginTransmission(Addr);
        // Select data register
        Wire.write((80+i));
        // Stop I2C Transmission
        Wire.endTransmission();
        
        // Request 1 byte of data from the device
        Wire.requestFrom(Addr, 1);
        
        // Read 8 bytes of data
        // Red lsb, Red msb, Green lsb, Green msb, Blue lsb, Blue msb
        // cData lsb, cData msb
        if(Wire.available() == 1)
        {
            data[i] = Wire.read();
        }
        delay(300);
    }

    // Convert the data
    int red = ((data[1] & 0xFF) * 256) + (data[0] & 0xFF);
    int green = ((data[3] & 0xFF) * 256) + (data[2] & 0xFF);
    int blue = ((data[5] & 0xFF) * 256) + (data[4] & 0xFF);
    int cData = ((data[7] & 0xFF) * 256) + (data[6] & 0xFF);
    
    // Output data to serial monitor
    Serial.print("Red Color luminance  : ");
    Serial.println(red);
    Serial.print("Green Color luminance : ");
    Serial.println(green);
    Serial.print("Blue Color luminance : ");
    Serial.println(blue);
    Serial.print("Clear Data Color luminance : ");
    Serial.println(cData);
}

[/codesyntax]

 

Output

Open the serial monitor, this is what I saw

Clear Data Color luminance : 2
Red Color luminance : 50
Green Color luminance : 52
Blue Color luminance : 14
Clear Data Color luminance : 16
Red Color luminance : 51
Green Color luminance : 46
Blue Color luminance : 15
Clear Data Color luminance : 17
Red Color luminance : 13
Green Color luminance : 0
Blue Color luminance : 0
Clear Data Color luminance : 0
Red Color luminance : 0
Green Color luminance : 0
Blue Color luminance : 9
Clear Data Color luminance : 24

Place different colored objects beside the sensor and check the values

Links

https://www.rohm.com/datasheet/BH1749NUC/bh1749nuc-e

ESP8266 and BMP388 barometric pressure sensor

In this article we look at another sensor – it is a BMP388 by Bosch Sensortec, so lets crack on and try this out with our Wemos board

As usual we will look at some of the manufacturers information from the website on the sensor

BMP388 Information

The BMP388 is a very small, low-power and low-noise 24 bit absolute barometric pressure sensor. It enables accurate altitude tracking and is specifically suited for drone applications. The best-in-class TCO of the BMP388 between 0-65°C for accurate altitude measurement over a wide temperature range greatly enhances the drone flying experience by making accurate steering easier.

It is compatible for use with other Bosch sensors, including BMI088 for better performance, robustness and stability.

The BMP388 sensor offers outstanding design flexibility, providing a single package solution that is easy to integrate into other existing and upcoming devices such as smart homes, industrial products and wearables.

It is more accurate than its predecessors, covering a wide measurement range from 300 hPa to 1250 hPa. BMP388 exhibits an attractive price-performance ratio coupled with low power consumption. It is available in a compact 10-pin 2.0 x 2.0 x 0.75 mm³ LGA package with metal lid.

  • Operating voltage: 3.3V/5V
  • Communication interface: I2C/SPI
  • Barometric pressure operation range: 300~1250hPa
  • Barometric pressure absolute accuracy: ±0.40hPa (@900~1100hPa, 25~40℃)
  • Barometric pressure relative accuracy: ±0.08hPa (@900~1100hPa, 25~40℃)
  • Temperature coefficient offset: ±0.75Pa/K (@700~1100hPa, -20~65℃))
  • Temperature absolute accuracy: ±0.5℃ (0~65℃)
  • Possible resolution: 0.016Pa (high precision mode)
  • Possible sampling rate: 200Hz
  • Operating voltage: -40~85℃

If you purchase a module they will have a 3.3v regulator on board, you will also have the option of I2C or SPI, here is the module that I located. You can see that it has Vin and 3v written on the board

Parts Required

 

Name Link
ESP8266 – wemos D1 mini by WeMos
BMP388 24-bit low noise BMP388 digital temperature atmospheric pressure sensor
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

I use the sensor in I2C mode – as you can see in the layout below

wemos and bmp388
wemos and bmp388

Code Example

I used the library from adafruit, you can download this from – https://github.com/adafruit/Adafruit_BMP3XX. 

This library can be installed via the library manager. This is the default code example and I have removed some of the SPI code and code comments since I was using the sensor in I2C mode

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
 
 
#define SEALEVELPRESSURE_HPA (1013.25)
 
Adafruit_BMP3XX bmp; // I2C
 
 
void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("BMP388 test");
 
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP3 sensor, check wiring!");
    while (1);
  }
 
  // Set up oversampling and filter initialization
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  //bmp.setOutputDataRate(BMP3_ODR_50_HZ);
}
 
void loop() {
  if (! bmp.performReading()) {
    Serial.println("Failed to perform reading :(");
    return;
  }
  Serial.print("Temperature = ");
  Serial.print(bmp.temperature);
  Serial.println(" *C");
 
  Serial.print("Pressure = ");
  Serial.print(bmp.pressure / 100.0);
  Serial.println(" hPa");
 
  Serial.print("Approx. Altitude = ");
  Serial.print(bmp.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");
 
  Serial.println();
  delay(2000);
}

 

Output

Open the serial monitor and you should see something like this, the value swill be slightly different depending on your location and current weather conditions

BMP388 test
Temperature = 23.64 *C
Pressure = 844.40 hPa
Approx. Altitude = 470.89 m