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

 

Wemos Mini and various shields ebook

As you may know looking at this web site we are big fans of the Wemos mini and the various shields that you can buy for this hardware. We have decided to write an ebook and publish it on the Kindle store and of course any money recieved through commision will go towards buying new modules, sensors and shields. We also want to design our own shields which this would help fund.

There will be descriptions of each chapter, code examples (which is available via a github link in the book) and expected output. Here are the contents of the ebook so you can see what we have added

Contents 2
About the ESP8266 4
The Wemos Mini and shields 5
Setting up the Arduino IDE 6
Basic examples 7
Flash an LED example 7
Basic Wifi example 8
I2Scanner example 9
Basic Webserver example 11
Read and write to the eeprom 16
ESP specific APIs 18
A look at SHA-1 20
The ticker library 21
Sampling analog data 22
Control a Wemos using an application on your PC 23
A look at SPIFFS 28
Setup your ESP8266 as an Action point 32
DHT Shield example 33
The 1 button shield 36
WS2812B RGB Shield 37
OLED Shield 40
Barometric Pressure Shield 43
TFT 2.4 Touch Shield 46
BMP180 Shield 55
DS18B20 Shield 57
PIR Shield 60
Micro SD Card Shield 62
IR Controller Shield 64
Ambient light Shield (BH1750) 67
DHT Pro Shield 69
RGB LED Shield 71
SHT30 Shield 74
TFT 1.4 Shield 76
Matrix LED Shield 84
Buzzer Shield 87
Relay Shield 90
Other Shields 91
DHT22 readings on an OLED display simple project 93
OLED shield bitcoin ticker example 96
PIR shield warning example 100
BMP180 versus SHt30 temperature comparison 101
Save sensor values to an SD card 102
Conclusion 105

We receive a percentage of the profits from Amazon which we reinvest into this site with regards domain name renewal, web site hosting and purchasing any parts we use – we don’t get any samples or freebies from any companies (but live in hope).

So please support the site by purchasing the ebook from the following links

 

Site Link
Amazon US Buy here
Amazon UK Buy here
Amazon DE Buy here
Amazon FR Buy here
Amazon ES Buy here
Amazon IT Buy here
Amazon NL Buy here
Amazon JP Buy here
Amazon BR Buy here
Amazon CA Buy here
Amazon MX Buy here
Amazon AU Buy here
Amazon IN Buy here