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

[codesyntax lang=”python”]

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)

[/codesyntax]

and a slight adaptation to create a more readable output

 

[codesyntax lang=”python”]

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)

[/codesyntax]

 

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here