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

 

AM2320 temperature and humidity sensor and ESP8266 example

Temperature and humidity combined sensor AM2320 digital temperature and humidity sensor is a digital signal output has been calibrated. Using special temperature and humidity acquisition technology, ensure that the product has a very high reliability and excellent long-term stability. Sensor consists of a capacitive moisture element and an integrated high-precision temperature measurement devices, and connected with a high-performance microprocessor .

AM2320 communication using a single bus, two communication modes standard I2C. Standard single-bus interface, the system integration becomes easy and quick. Ultra-small size, low power consumption, signal transmission distance up to 20 meters, making all kinds of applications and even the most demanding applications the best choice. I2C communication using standard communication sequence, the user can directly linked to the I2C communication bus without additional wiring, simple to use. Two communication modes are used as humidity, temperature, and other digital information directly CRC checksum temperature-compensated output, users do not need to calculate the secondary digital output, and no need for temperature compensation of the humidity, temperature and humidity can be accurately information. Two communication modes are free to switch, the user can freely choose, easy to use, wide range of applications.

 

Specifications

• Operating Voltage: 3.1 VDC to 5.5 VDC
• Operating Temperature Range: -40 ° C to + 80 ° C
• Humidity Range: 0 to 99.9% RH
• Accuracy ( 25 ° C environment)
Temperature: ± 0.5 ° C
Humidity: ± 3%
• RH (10 … 90% RH)
Resolution: Temperature: 0.1 ° C
Resolution: Humidity: 0.1% RH
• Attenuation values
Temperature: <0.1 ℃ / Year
Humidity: <1% RH / Year
• Response time: Temperature: 5s
• Response Time: Humidity: 5s 1 / e (63%)
• Output signal: single bus / IIC signal
• Housing material: PC plastic

 

Layout

a simple I2C sensor with a 3.1 to 5.5v range its straightforward to connect this device to a Wemos Mini

 

Code

You will need to install the folllowing library from https://github.com/EngDial/AM2320

This is the default example

 

#include <Wire.h>
#include <AM2320.h>
 
AM2320 th;
 
void setup() {
Serial.begin(9600);
Wire.begin();
}
 
void loop() {
Serial.println("Chip = AM2320");
switch(th.Read()) {
case 2:
Serial.println(" CRC failed");
break;
case 1:
Serial.println(" Sensor offline");
break;
case 0:
Serial.print(" Humidity = ");
Serial.print(th.Humidity);
Serial.println("%");
Serial.print(" Temperature = ");
Serial.print(th.cTemp);
Serial.println("*C");
Serial.println();
break;
}
delay(2000);
}

 

 

 

Output

Open the serial monitor

Chip = AM2320
Humidity = 47.10%
Temperature = 24.80*C

Chip = AM2320
Humidity = 48.70%
Temperature = 25.10*C

Chip = AM2320
Humidity = 53.60%
Temperature = 25.40*C

Chip = AM2320
Humidity = 55.80%
Temperature = 25.80*C

Chip = AM2320
Humidity = 59.80%
Temperature = 26.20*C

 

Links

AM2320 Digital Temperature and Humidity Sensor Replace AM2302 SHT10

https://akizukidenshi.com/download/ds/aosong/AM2320.pdf

Wemos mini and OLED shield bitcoin ticker

In this example we will display the price in USD of 1 bitcoin, the data is taken from coindesk’s API and displayed on an oled display.

This is similar to our previous examples, you do not need to use a Wemos mini or the OLED shield below but this makes the project easier to build

Here is the oled shield for the Wemos mini

Here is the wemos mini

Code


#include <ESP8266WiFi.h>
#include <Wire.h>
#include <SFE_MicroOLED.h>
#include <ArduinoJson.h>

const char* ssid = "iainhendry";
const char* pass = "iain061271";
#define PIN_RESET 255 //
#define DC_JUMPER 0 // I2C Addres: 0 - 0x3C, 1 - 0x3D
String id;
String value;
String json;

MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C Example

WiFiClient client;

// delay between updates
const unsigned long postingInterval = 60L * 1000L;
unsigned long lastConnectionTime = 0;

void setup()
{
delay(100);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

Serial.print("Connecting to ");

oled.begin();
oled.clear(ALL);
oled.setCursor(0,0);
oled.display();
oled.clear(PAGE);
oled.clear(ALL);
oled.print("Bitcoin price");
oled.setCursor(0,1);
oled.print("loading..");
oled.display(); // Display what's in the buffer (splashscreen)
delay(50);

}

int check_connect = 0;

void httpRequest()
{
client.stop();

// if there's a successful connection:
if (client.connect("api.coindesk.com", 80))
{
Serial.println("connecting...");
client.println("GET /v1/bpi/currentprice.json HTTP/1.1");
client.println("Host: api.coindesk.com");
client.println("User-Agent: ESP8266/1.1");
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
}
else
{
// if you couldn't make a connection:
Serial.println("connection failed");
}
}

void loop()
{
int cnt;

if (cnt++ == 10000)
{
cnt = 0;
if (check_connect++ == 50)
{
check_connect = 0;
if (WiFi.status() != WL_CONNECTED)
{
}
}
}

if (millis() - lastConnectionTime > postingInterval)
{
httpRequest();
unsigned int i = 0; //timeout counter
int n = 1; // char counter
char json[500] ="{";

while (!client.find("\"USD\":{")){}

while (i<20000)
{
if (client.available())
{
char c = client.read();
json[n]=c;
if (c=='}') break;
n++;
i=0;
}
i++;
}

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);

String newjson = root["code"];
String value = root["rate"];
id = newjson.substring(9,12);

// value = newjson.substring(41,51);
oled.display();
oled.clear(PAGE); // Clear the display's internal memory
oled.clear(ALL); // Clear the library's display buffer
oled.setCursor(0,1);
oled.print(value);
oled.display();

id="";
value="";
}
}

testing

You should see something like this on the oled display

esp8266 bitcoin ticker

esp8266 bitcoin ticker

Links

Here are some of the products I used

Smart Electronics D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos

0.66″ inch For Wemos Oled 64X48 IIC I2C LCD OLED LED Dispaly Shield for Arduino Compatible For WeMos D1 Mini SSD1306 OLED Shield

by hotcoin.net