PIR Motion Module

This documentation includes hardware info, installation guidelines and sample code for your hardware.

Overall Info

Turta PIR Motion Detect Module recognizes the movement of heat-emitting things, like humans. It carries AM312 passive infrared sensor.



Module Connections

  • INT: Motion detect out.

  • POWER: +3.3V and GND.


AM312's technical specifications are as follows:

  • Detection range: Approx. 3m.

  • Delay time: 2.3 Seconds.

  • Two-way differential high impedance sensor input and temperature compensation.

  • Built-in filter, screen the interference by other frequency.

  • Operating temperature range: -20 to 85 °C


To get maximum performance from your hardware, please make sure you'll meet the following requirements.Hardware

PIR Motion Detect Module is an add-in board for Turta Modular System. You'll need a compatible host device for your module. The following devices support PIR Motion Detect Module:

  • Turta IoT Node - ESP32


For ESP32 based systems, we're currently supporting Arduino core. Please make sure you've installed the following:

  • Arduino IDE.

  • ESP32 Hardware libraries for the Arduino IDE.

  • Python for the compilation of Arduino projects.

  • Drivers for your host device, e.g., USB Virtual Serial Port driver.


You'll need to install a library to make Arduino IDE support the sensor. This library includes drivers for the AS312 sensor and sample code for you to jumpstart your project.


Please download the library archive from https://github.com/Turta-io/Modular

Then, copy its contents to your Arduino Libraries folder.

You can find the Arduino Libraries folder inside your Arduino sketch directory. It is generally located inside the My Documents folder.


The library comes with included samples. There is no need to download additional files for the examples.

Source Code

The hardware, its drivers and the sample code are open source. You can use them under the terms of the MIT license. Please feel free to customize and prototype them for your projects.

Arduino Library

Turta_PIRMotionDetect_Module library is responsible for reading the sensor interrupts.

To use the library on Arduino IDE, add the following #include statement to the top of your sketch.

#include <Turta_PIRMotionDetect_Module.h>

Then, create an instance of the Turta_PIRMotionDetect_Module class.

Turta_PIRMotionDetect_Module pir

Now you're ready to access the library by calling the pir instance.


To initialize the sensor, call the begin method.


This method configures the interrupt pin to read sensor state.

Basic Members

Read Motion State

Returns the motion state from the sensor.

bool readMotionState()
  • Parameters

    • None

  • Returns

    • Bool: Motion state.


You can open the example from Arduino IDE > File > Examples > Examples from Custom Libraries > Turta PIR Motion Detect Module. There is one example of this sensor.

Motion Detect

This application demonstrates motion detect function of the module. It uses Arduino IDE's Serial Monitor and IoT Node's onboard LED to display motion state.

To run the example:

  1. Open the IoT_Node_PIRMotionDetect sketch from the examples menu.

  2. Select Turta IoT Node from the Tools > Board menu.

  3. Select your device's COM port from Tools > Port menu.

  4. Open Serial Monitor from Tools > Serial Monitor.

  5. Select 115.200 baud from the Serial Monitor's status bar.

  6. Upload the code to your device.

Sample Code

#include <Turta_PIRMotionDetect_Module.h>
#define LED 13
// Global variables.
bool motionDetected = false;
bool oldMotionState = false;
// Create PIR Motion Detect instance.
Turta_PIRMotionDetect_Module pir;
void setup() {
// Initialize onboard LED.
pinMode(LED, OUTPUT);
// Initialize PIR.
// Configure serial port.
void loop() {
// Read PIR state.
motionDetected = pir.readMotionState();
// On motion detected / no motion.
if (oldMotionState != motionDetected) {
digitalWrite(LED, motionDetected ? HIGH : LOW);
Serial.println(motionDetected ? "Motion detected." : "No motion.");
// Store last motion state.
oldMotionState = motionDetected;
// Delay 10ms.


After the application is uploaded to the device, it writes motion state to the Serial Monitor if the state is changed. The example output should be like this:

Motion detected.
No motion.

The application runs forever until you clear it from memory.


If you're experiencing difficulties while working with your device, please try the following steps.

Problem: The sensor generated an interrupt, but there was no movement. Cause: However there is a built-in temperature compensation inside the sensor, quick heat changes may trigger incorrect interrupts. Solution: Try to prevent quick temperature changes in the sensor's surrounding.