Wemos Mini and LIS3MDL magnetic field sensor example

In this article we look at another sensor – this time its the LIS3MDL which is a 3-axis MEMS magnetic field sensor, digital output, I2C, SPI, low power mode, high performance

This is the sensor that I used

The LIS3MDL has user-selectable full scales of ±4/±8/±12/±16 gauss.
The self-test capability allows the user to check the functioning of the sensor in the final application.
The device may be configured to generate interrupt signals for magnetic field detection.
The LIS3MDL includes an I2C serial bus interface that supports standard and fast mode (100 kHz and 400 kHz) and SPI serial standard interface.
The LIS3MDL is available in a small thin plastic land grid array package (LGA) and is guaranteed to operate over an extended temperature range of -40 °C to +85 °C.

Features

  • Wide supply voltage, 1.9 V to 3.6 V
  • Independent IO supply (1.8 V)
  • ±4/±8/±12/±16 gauss selectable magnetic full scales
  • Continuous and single-conversion modes
  • 16-bit data output
  • Interrupt generator
  • Self-test
  • I2C/SPI digital output interface
  • Power-down mode / low-power mode

As an added bonus here is the schematic for one of these modules

Parts Required

 

Name Link
Wemos Mini WeMos D1 mini
LIS3MDL Taidacent LIS3MDL High Precision 3 Axis Magnetometer Sensor Compass Module Tilt Compensated Compass Replacement HMC5883L
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
Base shield Double Socket Dual Base Shield for Wemos D1 Mini

Schematic/Connection

 

Wemos mini Sensor
3.3v Vcc
Gnd Gnd
SDA (D2) SDA
SCL (D1) SCL

 

Code Example

This uses the library from https://github.com/pololu/lis3mdl-arduino

 

/*
The sensor outputs provided by the library are the raw 16-bit values
obtained by concatenating the 8-bit high and low magnetometer data registers.
They can be converted to units of gauss using the
conversion factors specified in the datasheet for your particular
device and full scale setting (gain).
 
Example: An LIS3MDL gives a magnetometer X axis reading of 1292 with its
default full scale setting of +/- 4 gauss. The GN specification
in the LIS3MDL datasheet (page 8) states a conversion factor of 6842
LSB/gauss (where LSB means least significant bit) at this FS setting, so the raw
reading of 1292 corresponds to 1292 / 6842 = 0.1888 gauss.
*/
 
#include <Wire.h>
#include <LIS3MDL.h>
 
LIS3MDL mag;
 
char report[80];
 
void setup()
{
  Serial.begin(9600);
  Wire.begin();
 
  if (!mag.init())
  {
    Serial.println("Failed to detect and initialize magnetometer!");
    while (1);
  }
 
  mag.enableDefault();
}
 
void loop()
{
  mag.read();
 
  snprintf(report, sizeof(report), "M: %6d %6d %6d",
    mag.m.x, mag.m.y, mag.m.z);
  Serial.println(report);
 
  delay(100);
}

 

 

Output

Open the serial monitor , move the sensor around and you should readings like this

M: -1639 3336 7493
M: -1839 2769 7615
M: -1374 2834 7749
M: -356 2968 7769
M: -104 2425 7931
M: 53 1223 7841
M: -90 157 7370
M: -488 -1027 6358
M: -1011 -1540 5230
M: -1255 -1699 4822
M: -1417 -1714 4831
M: -1209 -1363 5964
M: -682 -301 7271

 

Links

https://www.st.com/resource/en/datasheet/lis3mdl.pdf

 

 

 

RPI-1031 tilt sensor and Wemos Mini example

In this article we look at a tilt sensor – this time its the RPI-1031 and we will connect it to a Wemos

The RPI-1031 tilt sensor is capable of sensing a change in orientation in four different directions: forward, back, left or right</p

This tilt sensor can be used to detect multiple orientations. Inside the sensor are infrared emitters which are either reflected or not, depending on the orientation of the sensor. By reading the output of the various pins you can easily determine the orientation of the sensor and your project.

Being a tilt sensor this is just a metal ball that rolls around inside the case. The ball is always up against one side even when it is flat.

The sensor needs power and ground, you can then connect the 2 digital pins. These 2 digital pins will output LOW/LOW, LOW/HIGH, HIGH/LOW and HIGH/HIGH depending on the wall it is touching.

Parts Required

 

Name Link
Wemos Mini D1 mini – ESP8266
RPI-1031 RPI-1031 Angle Sensor 4DOF Attitude HM Module 4 Direction For Arduino
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
sensor shield Expansion IO Board Sensor Shield

Schematic/Connection

Wemos Sensor
3v3 Vcc
Gnd Gnd
D5 S1
D6 S2

 

 

Code Example

The code for this sensor is really basic, it just checks the 2 pins, to see what side is being touched – The simple function simply takes the 2 digital outputs and returns 0,1,2 or 3 depending on the side.

int tilt_s1 = 2;
int tilt_s2 = 3;
void setup(){
pinMode(tilt_s1, INPUT);
pinMode(tilt_s2, INPUT);
Serial.begin(9600);
}
void loop(){
int position = getTiltPosition();
Serial.println(position);
delay(200); //only here to slow down the serial output
}
int getTiltPosition(){
int s1 = digitalRead(tilt_s1);
int s2 = digitalRead(tilt_s2);
return (s1 << 1) | s2; //bitwise math to combine the values
}

 

Output

Open the serial monitor and you should see something like this, I was moving the sensor around.

Not the most intuitive readings admittedly and it seems to work

3
3
3
2
0
0

 

Links

https://github.com/sparkfun/Tilt-a-Whirl_Breakout

 

 

 

 

 

SHTC1 digital humidity sensor and ESP8266 example

In this article we look at another digital humidity sensor – this time its the SHTC1 and we will connect it to a Wemos Mini

The SHTC1 is a digital humidity sensor designed especially for high-volume consumer electronics applications. This humidity sensor is strictly designed to overcome conventional limits for size, power consumption, and price-performance ratio, in order to fulfill the current and future requirements of the consumer electronics market.

Sensirion’s CMOSens® technology offers a complete sensor system on a single chip, consisting of a capacitive humidity sensor, a band-gap temperature sensor, analog and digital signal processing, A/D converter, calibration data memory, and a digital communication interface supporting I2C fast mode. The ultra-small, 2 × 2 × 0.75 mm3 DFN package enables applications to be placed in even the most limited of spaces. The sensor covers a humidity measurement range of 0 to 100 %RH and a temperature measurement range of –30°C to 100°C with a typical accuracy of ±3 %RH and ±0.3°C.

The operating voltage of 1.8 V and an energy budget below 1 µJ per measurement make the SHTC1 suitable for mobile or wireless applications running on the lowest power budgets. With the industry-proven quality and reliability of Sensirion’s humidity sensors and constant accuracy over a large measurement range, the SHTC1 humidity sensor offers an unprecedented price-performance ratio. Tape and reel packaging together with suitability for standard SMD assembly processes make the SHTC1 predestined for high-volume applications.

Features

Interface I²C
Supply voltage 1.8 V
Power consumption 2µW (at 1 reading per second in low power mode)
Measuring range (RH) 0 – 100% relative humidity
Measuring range (T) -30 to +100°C (-22 to +212°F)
Response time (RH) 8s (tau63%)

Parts Required

 

Name Link
ESP8266 (Wemos Mini) D1 mini – ESP8266
SHTC1 SHTC1 digital temperature and humidity sensor
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
sensor shield Expansion IO Board Sensor Shield

Schematic/Connection

Wemos Sensor
3.3v Vcc
Gnd Gnd
SDA (D2) SDA
SCL (D1) SCL

 

Code Example

This uses the library from https://github.com/Sensirion/arduino-sht

#include <Wire.h>
#include "SHTSensor.h"
SHTSensor sht;
// To use a specific sensor instead of probing the bus use this command:
// SHTSensor sht(SHTSensor::SHT3X);
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
delay(1000); // let serial console settle
if (sht.init()) {
Serial.print("init(): success\n");
} else {
Serial.print("init(): failed\n");
}
sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM); // only supported by SHT3x
}
void loop() {
// put your main code here, to run repeatedly:
if (sht.readSample()) {
Serial.print("SHT:\n");
Serial.print(" RH: ");
Serial.print(sht.getHumidity(), 2);
Serial.print("\n");
Serial.print(" T: ");
Serial.print(sht.getTemperature(), 2);
Serial.print("\n");
} else {
Serial.print("Error in readSample()\n");
}
delay(1000);
}

 

Output

Open the serial monitor and you should see something like this

init(): success
SHT:
RH: 43.97
T: 20.05
SHT:
RH: 43.99
T: 20.01
SHT:
RH: 44.01
T: 20.00

 

Links

 

 

 

 

 

ESP8266 and LPS22HB absolute pressure sensor example

In this article we look at another absolute pressure sensor – this time its the LPS22HB

Once again lets look at this sensor from the manufacturers perspective

Description

The LPS22HB is an ultra-compact piezoresistive absolute pressure sensor which functions as a digital output barometer. The device comprises a sensing element and an IC interface which communicates through I2C or SPI from the sensing element to the application.

The sensing element, which detects absolute pressure, consists of a suspended membrane manufactured using a dedicated process developed by ST.
The LPS22HB is available in a full-mold, holed LGA package (HLGA). It is guaranteed to operate over a temperature range extending from -40 °C to +85 °C. The package is holed to allow external pressure to reach the sensing element.

Features

  • 260 to 1260 hPa absolute pressure range
  • Current consumption down to 3 μA
  • High overpressure capability: 20x full-scale
  • Embedded temperature compensation
  • 24-bit pressure data output
  • 16-bit temperature data output
  • ODR from 1 Hz to 75 Hz
  • SPI and I²C interfaces
  • Embedded FIFO
  • Interrupt functions: Data Ready, FIFO flags, pressure thresholds
  • Supply voltage: 1.7 to 3.6 V
  • High shock survivability: 22,000 g

 

Parts Required

Around $5 for the module

Name Link
Wemos Mini D1 mini – ESP8266 by WeMos
LPS22HB Semoic Lps22Hb Pressure Resistance Pressure Sensor Module for High Intensity Industrial Control
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

 

esp8266 and LPS22HB
esp8266 and LPS22HB

 

Code Example

This uses the library from https://github.com/adrien3d/IO_LPS22HB

This is the default example which works fine

/***************************************************************************
  This is a library for the LPS22HB Absolute Digital Barometer
 
  Designed to work with all kinds of LPS22HB Breakout Boards
 
  These sensors use I2C, 2 pins are required to interface, as this :
	VDD to 3.3V DC
	SCL to A5
	SDA to A4
	GND to common groud 
 
  Written by Adrien Chapelet for IoThings
 ***************************************************************************/
 
#include <Wire.h>
 
#include "IO_LPS22HB.h"
 
IO_LPS22HB lps22hb;
 
void setup()
{
	Serial.begin(9600);
	Serial.println("IoThings LPS22HB Arduino Test");
 
	lps22hb.begin(0x5D);
 
	byte who_am_i = lps22hb.whoAmI();
	Serial.print("Who Am I? 0x");
	Serial.print(who_am_i, HEX);
	Serial.println(" (expected: 0xB1)");
	if (who_am_i != LPS22HB_WHO_AM_I_VALUE) {
		Serial.println("Error while retrieving WHO_AM_I byte...");
		while (true) {
		      // loop forever
		}
	}
}
 
void loop()
{
	Serial.print("P=");
	Serial.print(lps22hb.readPressure());
	Serial.print(" mbar, T=");
	Serial.print(lps22hb.readTemperature());
	Serial.println("C");
	delay(300);
}

Output

Open the serial monitor and you should see something like this – I put my finger on the sensor, hence the value is rising

P=983.26 mbar, T=27.88C
P=983.22 mbar, T=28.27C
P=983.24 mbar, T=28.60C
P=983.24 mbar, T=28.93C
P=983.27 mbar, T=29.23C
P=983.24 mbar, T=29.50C
P=983.24 mbar, T=29.71C
P=983.25 mbar, T=29.89C
P=983.26 mbar, T=30.05C
P=983.25 mbar, T=30.21C
P=983.23 mbar, T=30.34C

I wanted to check the pressure out so I visited the following website – https://www.worldweatheronline.com . There are several others.

They stated that the pressure was – Pressure: 992 mb

So not to bad

Links

https://www.st.com/resource/en/datasheet/lps22hb.pdf