ESP8266 and HMC5983 magnetometer example

The Honeywell HMC5983 is a temperature compensated three-axis integrated circuit magnetometer. This surface-mount, multi-chip module is designed for low-field magnetic sensing for applications such as automotive and personal navigation, vehicle detection, and pointing.

The HMC5983 includes our state-of-the-art, high-resolution HMC118X series magnetoresistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy. The I²C or SPI serial bus allows for easy interface. The HMC5983 is a 3.0×3.0x0.9mm surface mount 16-pin leadless chip carrier (LCC).

The HMC5983 utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies. Honeywell’s anisotropic, directional sensors excel in linearity, low hysteresis, null output and scale factor stability over temperature, and with very low cross-axis sensitivity. These sensors’ solid-state construction is designed to measure both the direction and the magnitude of magnetic fields, from milli-gauss to 8 gauss. Honeywell’s Magnetic Sensors are among the most sensitive and reliable low-field sensors in the industry.

 

Connection

Here is a layout, its an easy device to connect

The HM5883 is the same as the HMC5983

Parts List

Here are the parts I used

Part name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
HMC5983 GY-282 HMC5983 High-precision High-sensitivity Temperature Compensation Triaxial Compass IIC SPI Module
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire Dupont cablefor Arduino

 

Code

You will need to install the following library – https://github.com/arduino/HMC5983

#include <HMC5983.h>
#include <Wire.h>
 
HMC5983 compass;
 
void setup () {
Serial.begin(9600);
compass.begin(); // use "true" if you need some debug information
}
 
void loop() {
float c = -999;
c = compass.read();
if (c == -999) {
Serial.println("Reading error, discarded");
} else {
Serial.println(c);
}
delay(500);
}

 

Output

Open the serial monitor – this is what you should expect to see. I moved the sensor around

13.84
22.05
23.39
38.34
5.99
9.68
17.69
24.28
16.61

 

Links

 

ESP8266 and MMA8452 accelerometer example

In this example we will connect a MMA8452Q accelerometer  to an ESP8266 – as usual it’s a Wemos Mini

Lets look at the sensor

The MMA8452Q is a smart, low-power, three-axis, capacitive, micromachined accelerometer with 12 bits of resolution. This accelerometer is packed with embedded functions with flexible user programmable options, configurable to two interrupt pins. Embedded interrupt functions allow for overall power savings relieving the host processor from continuously polling data.

The MMA8452Q has user selectable full scales of ±2 g/±4 g/±8 g with high-pass filtered data as well as non-filtered data available real-time. The device can be configured to generate inertial wakeup interrupt signals from any combination of the configurable embedded functions allowing the MMA8452Q to monitor events and remain in a low-power mode during periods of inactivity

 

Connection

Here is a layout, its an easy device to connect

esp8266 and mma8452
esp8266 and mma8452

Parts List

Here are the parts I used

Part name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
MMA8452 sensor GY-45 MMA8452 Module Digital Triaxial Accelerometer Precision Tilt 3-Axis
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire Dupont cablefor Arduino

 

Code

This example does not require any libraries

// 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.
// MMA8452Q
// This code is designed to work with the MMA8452Q_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Accelorometer?sku=MMA8452Q_I2CS#tabs-0-product_tabset-2
 
#include <Wire.h>
 
// MMA8452Q I2C address is 0x1C(28)
#define Addr 0x1C
 
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 control register
  Wire.write(0x2A);
  // StandBy mode
  Wire.write((byte)0x00);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register
  Wire.write(0x2A);
  // Active mode
  Wire.write(0x01);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register
  Wire.write(0x0E);
  // Set range to +/- 2g
  Wire.write((byte)0x00);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(300);
}
 
void loop()
{
  unsigned int data[7];
 
  // Request 7 bytes of data
  Wire.requestFrom(Addr, 7);
 
  // Read 7 bytes of data
  // staus, xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
  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 to 12-bits
  int xAccl = ((data[1] * 256) + data[2]) / 16;
  if (xAccl > 2047)
  {
    xAccl -= 4096;
  }
 
  int yAccl = ((data[3] * 256) + data[4]) / 16;
  if (yAccl > 2047)
  {
    yAccl -= 4096;
  }
 
  int zAccl = ((data[5] * 256) + data[6]) / 16;
  if (zAccl > 2047)
  {
    zAccl -= 4096;
  }
 
  // Output data to 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);
  delay(500);
}

Output

Open the serial monitor – this is what you should expect to see

Acceleration in X-Axis : 2047
Acceleration in Y-Axis : 1470
Acceleration in Z-Axis : 2047
Acceleration in X-Axis : 856
Acceleration in Y-Axis : -15
Acceleration in Z-Axis : -763
Acceleration in X-Axis : 181
Acceleration in Y-Axis : 1165
Acceleration in Z-Axis : 912
Acceleration in X-Axis : 297
Acceleration in Y-Axis : -589
Acceleration in Z-Axis : -554

 

Links

https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf

ESP8266 and PIR example in Micropython

In this example we look at a PIR example in Micropython for an ESP8266. Once again we use uPyCraft and again we use Wemos shields.

Lets remind ourselves about the PIR

A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors. A PIR-based motion detector is used to sense movement of people, animals, or other objects. They are commonly used in burglar alarms and automatically-activated lighting systems. They are commonly called simply “PIR”, or sometimes “PID”, for “passive infrared detector”.

If you want to read more basics – https://en.wikipedia.org/wiki/Passive_infrared_sensor

The shield uses D3 so that will be unavailable for any other components or shields you may want to use – The PIR is an AS312

 

Requirements

Lets take a look a the shields and boards that are required for this example

 

Image Summary
The Wemos mini – ESP8266 based board, it comes with various headers. This is the beauty of it you can create stackable projects with the board and pin compatible shields
This is simply a base, you plug the Wemos Mini into one side and you can plug a shield or shields into the other side

 

Parts List

I connect the Wemos Mini to the dual base and then put the DS18B20 shield along side this, you can connect the Wemos DS18B20 shield directly to the Wemos Mini if you want.

Name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
Wemos Base Tripler Base V1.0.0 Shield for WeMos D1 Mini
Wemos PIR shield PIR shield

Code

Create a new file called ldr1.py and import it into uPyCraft

A fairly simple idea here, we read in the state of the pin that the PIR uses

from machine import Pin
import time
 
ldr = Pin(0, Pin.IN)     # create input pin on GPIO2
 
while True:
	print(ldr.value())       # get value, 0 or 1
	time.sleep(1)

and a slight adaptation to create a more readable output

 

from machine import Pin
import time
 
ldr = Pin(0, Pin.IN)     # create input pin on GPIO2
 
while True:
	if ldr.value():
            print('OBJECT DETECTED')
        else:
            print('ALL CLEAR')
	time.sleep(1)

 

Output

You should see something like this

exec(open(‘ldr2.py’).read(),globals())
ALL CLEAR
ALL CLEAR
ALL CLEAR
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED
ALL CLEAR
ALL CLEAR
ALL CLEAR
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED

 

Links

https://datasheet.lcsc.com/szlcsc/Nanyang-Senba-Optical-Elec-AS312_C90465.pdf

ESP8266 and DS18B20 sensor and MIcropython example

In this example we look at a DS18b20 example in Micropython for an ESP8266. Once again we use uPyCraft and again we use Wemos shields.

Lets remind ourselves about the DS18B20

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems

Requirements

Lets take a look a the shields and boards that are required for this example

 

Image Summary
The Wemos mini – ESP8266 based board, it comes with various headers. This is the beauty of it you can create stackable projects with the board and pin compatible shields
This is not an official Wemos shield – this uses a DS18B20 and is connected to D2. This does mean that you can use another I2C device
This is simply a base, you plug the Wemos Mini into one side and you can plug a shield or shields into the other side

 

Parts List

I connect the Wemos Mini to the dual base and then put the DS18B20 shield along side this, you can connect the Wemos DS18B20 shield directly to the Wemos Mini if you want.

Name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
Wemos Base Tripler Base V1.0.0 Shield for WeMos D1 Mini
Wemos DS18B20 Temperature Sensor Shield DS18B20 Temperature Sensor Shield Wemos D1 Mini D1 Mini Pro ESP NodeMCU

Code

Create a new file called ds.py and import it into uPyCraft

from machine import Pin
import time, ds18x20
import onewire
ow = onewire.OneWire(Pin(4)) # create a OneWire bus on GPIO12
 
ds = ds18x20.DS18X20(ow)
 
roms = ds.scan()
ds.convert_temp()
time.sleep_ms(750)
 
for rom in roms:
    print(ds.read_temp(rom))

 

Output

You should see something like this

Ready to download this file,please wait!
..
download ok
exec(open(‘ds.py’).read(),globals())
26.4375
>>>

Links