Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Where do I find the pir.h library?

Hi, Where do I find the pir.h library?

We do not have an Arduino library for PIR sensors. If you are using the #2731 PIR motion sensor we carry (or one that functions identically to it), you can find an Arduino tutorial in the “Resoruces” tab of its product page.

Brandon

Hi Brandon, I am using 2 Welleman VMA 314 sensors


How do people implement a PIR then: coding directly into the sketch?
Cheers, Pascal

I am a bigginer: what is wrong here? How should I correct the problem?

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware -hardware C:\Users\Oma\Documents\ArduinoData\packages -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\tools-builder -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -tools C:\Users\Oma\Documents\ArduinoData\packages -built-in-libraries C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\libraries -libraries C:\Users\Oma\Arduino\libraries -fqbn=pololu-a-star:avr:a-star32U4 -ide-version=10813 -build-path C:\Users\Oma\AppData\Local\Temp\arduino_build_907344 -warnings=none -build-cache C:\Users\Oma\AppData\Local\Temp\arduino_cache_172570 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -verbose C:\Users\Oma\Arduino\2_PIR_Servo_Direction\2_PIR_Servo_Direction.ino
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\arduino-builder -compile -logger=machine -hardware C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware -hardware C:\Users\Oma\Documents\ArduinoData\packages -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\tools-builder -tools C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -tools C:\Users\Oma\Documents\ArduinoData\packages -built-in-libraries C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\libraries -libraries C:\Users\Oma\Arduino\libraries -fqbn=pololu-a-star:avr:a-star32U4 -ide-version=10813 -build-path C:\Users\Oma\AppData\Local\Temp\arduino_build_907344 -warnings=none -build-cache C:\Users\Oma\AppData\Local\Temp\arduino_cache_172570 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\tools\avr -verbose C:\Users\Oma\Arduino\2_PIR_Servo_Direction\2_PIR_Servo_Direction.ino
Using board 'a-star32U4' from platform in folder: C:\Users\Oma\Documents\ArduinoData\packages\pololu-a-star\hardware\avr\4.0.2
Using core 'arduino' from platform in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\arduino\avr
Detecting libraries used...
"C:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_A_STAR_32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x1ffb -DUSB_PID=0x2300 "-DUSB_MANUFACTURER=\"Pololu Corporation\"" "-DUSB_PRODUCT=\"Pololu A-Star 32U4\"" "-IC:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\variants\\leonardo" "C:\\Users\\Oma\\AppData\\Local\\Temp\\arduino_build_907344\\sketch\\2_PIR_Servo_Direction.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
Alternatives for Servo.h: [Servo@1.1.6 Servo@1.1.7]
ResolveLibrary(Servo.h)
  -> candidates: [Servo@1.1.6 Servo@1.1.7]
"C:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_A_STAR_32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x1ffb -DUSB_PID=0x2300 "-DUSB_MANUFACTURER=\"Pololu Corporation\"" "-DUSB_PRODUCT=\"Pololu A-Star 32U4\"" "-IC:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Oma\\Arduino\\libraries\\Servo\\src" "C:\\Users\\Oma\\AppData\\Local\\Temp\\arduino_build_907344\\sketch\\2_PIR_Servo_Direction.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
Alternatives for PIR.h: [PIR]
ResolveLibrary(PIR.h)
  -> candidates: [PIR]
"C:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_A_STAR_32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x1ffb -DUSB_PID=0x2300 "-DUSB_MANUFACTURER=\"Pololu Corporation\"" "-DUSB_PRODUCT=\"Pololu A-Star 32U4\"" "-IC:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files\\WindowsApps\\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Oma\\Arduino\\libraries\\Servo\\src" "-IC:\\Users\\Oma\\Arduino\\libraries\\PIR" "C:\\Users\\Oma\\AppData\\Local\\Temp\\arduino_build_907344\\sketch\\2_PIR_Servo_Direction.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
Alternatives for ../Generic/DigitalInput.h: []
ResolveLibrary(../Generic/DigitalInput.h)
  -> candidates: []
In file included from C:\Users\Oma\Arduino\2_PIR_Servo_Direction\2_PIR_Servo_Direction.ino:4:0:
Multiple libraries were found for "Servo.h"
C:\Users\Oma\Arduino\libraries\PIR/PIR.h:2:10: fatal error: ../Generic/DigitalInput.h: No such file or directory
 Used: C:\Users\Oma\Arduino\libraries\Servo
 #include "../Generic/DigitalInput.h"
 Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\libraries\Servo
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Using library Servo at version 1.1.7 in folder: C:\Users\Oma\Arduino\libraries\Servo 
Using library PIR in folder: C:\Users\Oma\Arduino\libraries\PIR (legacy)
exit status 1
Error compiling for board Pololu A-Star 32U4.

I combined your threads since they seemed related.

Usually, reading digital sensors like that are a simple digitalRead() function in Arduino, so a library is not necessary. It looks like the downloads page on the product page you linked to has a user manual for your PIR sensor that includes a wiring diagram showing how to connect it to an Arduino as well as some example Arduino code that toggles the onboard LED when the sensor detects something. I suggest starting there with a single sensor before writing your own sketches.

If you still have problems when trying to upload the example code from that user manual to the A-Star, could you post the latest error message you get when you try that, as well as your entire Arduino sketch? Also, if you are going to continue trying to use the PIR library you found, can you post a link to it?

Brandon

Hi Brandon,

It took me some time, but I am almost there.
I adapted an Arduino tutorial: https://arduinogetstarted.com/tutorials/arduino-motion-sensor-servo-motor.
The full sketch is here after.
This program somehow works, but it not 100% proof (instability?). For some reason the servo moves sometimes by itself. If I increase the PIR time on the PIR board, then nothing moves.
The idea is to keep the servo in neutral position (90 degrees) when there is no motion. When a motion occurs at the left PIR then the servo moves to 0 degree, with the right PIR to 180 degrees. When the motion stops, the servo returns to 90 d.
How could I improve the sketch?
Cheers!

#include <Servo.h>

// constants won't change
const int MOTION_SENSOR_PIN7 = 7; // Arduino pin connected to motion sensor's pin
const int MOTION_SENSOR_PIN9 = 9;
const int SERVO_PIN         = 11; // Arduino pin connected to servo motor's pin

Servo servo; // create servo object to control a servo

// variables will change:
int angle = 90;          // the current angle of servo motor
int lastMotionState7;    // the previous state of motion sensor
int currentMotionState7; // the current state of motion sensor
int lastMotionState9;
int currentMotionState9;

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN7, INPUT); // set arduino pin to input mode
  pinMode(MOTION_SENSOR_PIN9, INPUT);
  servo.attach(SERVO_PIN);           // attaches the servo on pin 11 to the servo object

  servo.write(angle);
  currentMotionState7 = digitalRead(MOTION_SENSOR_PIN7);
  currentMotionState9 = digitalRead(MOTION_SENSOR_PIN9);
}

void loop() {
  lastMotionState7    = currentMotionState7;             // save the last state
  lastMotionState9    = currentMotionState9; 
  currentMotionState7 = digitalRead(MOTION_SENSOR_PIN7); // read new state
  currentMotionState9 = digitalRead(MOTION_SENSOR_PIN9);
  
  if (currentMotionState7 == LOW && lastMotionState7 == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    servo.write(0);
  }
  else
  if (currentMotionState9 == LOW && lastMotionState9 == HIGH) {
    Serial.println("Motion detected!");
    servo.write(90);
  }
  else
  if (currentMotionState7 == HIGH && lastMotionState7 == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    servo.write(180);
  }
  else
  if (currentMotionState9 == HIGH && lastMotionState9 == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    servo.write(90);
  }
}

Hi,
My project is completed fulfilling my original goal :grinning:
“The idea is to keep the servo in neutral position (90 degrees) when there is no motion. When a motion occurs at the left PIR then the servo moves to 0 degree, with the right PIR to 180 degrees. When the motion stops, the servo returns to 90 d.”
Here is the code, which is quite simplified from the above one. I removed unnecessary items and added delays (was the cause of the instability).
Thank you for your support on my first trip :+1:
Cheers, Pascal

/*
* Modified from Arduino's tutorial by Pascal Halluli - 16 Dec. 2020
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-motion-sensor-servo-motor.
*The idea is to keep the servo in neutral position (90 degrees) when there is no motion. When a *motion occurs at the left PIR then the servo moves to 0 degree, with the right PIR to 180 *degrees. When the motion stops, the servo returns to 90 d.
 */

#include <Servo.h>

// constants
const int PIR_PIN7 = 7;
const int PIR_PIN9 = 9;
const int SERVO_PIN = 11;

Servo servo;

// variables
int angle = 90;
int MotionPIR7;
int MotionPIR9;

void setup() {
  Serial.begin(9600);
  pinMode(PIR_PIN7, INPUT);
  pinMode(PIR_PIN9, INPUT);
  servo.attach(SERVO_PIN);

  servo.write(angle);
  MotionPIR7 = digitalRead(PIR_PIN7);
  MotionPIR9 = digitalRead(PIR_PIN9);
}

void loop() {
  MotionPIR7 = digitalRead(PIR_PIN7);
  MotionPIR9 = digitalRead(PIR_PIN9);

  if (MotionPIR7 == HIGH && MotionPIR9 == LOW){
  servo.write(0);
  delay(1000);
  }
  else
  
  if (MotionPIR9 == HIGH && MotionPIR7 == LOW){
  servo.write(180);
  delay(1000);
  }
  else
  
  if (MotionPIR9 == LOW && MotionPIR7 == LOW) {
  servo.write(90);
  delay(1000);
  }
}

That’s great! I am glad you were able to get it working how you want; thank you for letting us know.

Brandon