PIR sensor and DmxMaster library

Hi i am having problems getting a PIR sensor to work along side the DmxMaster library (http://www.tinkerkit.com/dmxmaster-library/#dmxmaster). When running the sketch with the lightsOn() and lightsOff() functions commented out the sensor calibrates the detects motion and then prints the end of motion, (in the serial monitor). However when i run the sketch with these functions active the sensor instantly detects movement and never detects when the movement has ended, almost as if it isn’t running the

  if(digitalRead(pirPin) == LOW){   

Does anyone have any ideas as to why this is happening?

#include <DmxMaster.h>

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 3;    //the digital pin connected to the PIR sensor's output

  void setup(){
  pinMode(13, OUTPUT);
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");

void loop(){

     if(digitalRead(pirPin) == HIGH){
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.print("motion detected at ");
         Serial.println(" sec"); 
         takeLowTime = true;

     if(digitalRead(pirPin) == LOW){       
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
  void lightsOn(){
  void lightsOff(){
         takeLowTime = true;

This makes no sense. It would be as meaningful to have

         elephant = Colt45;

What relationship is there between time and true/false?


What pins is DmxMaster diddling with?

Ok so changing the PIR Pin from 3 to 7 has solved the problem, but can somebody please explain why on earth that makes a difference?

DmxMaster uses pin 13 as default Thanks

DmxMaster uses pin 13 as default

A link to the library, and any hardware, might allow us to explain what you are seeing. No links == no hope.

I have linked the library in my first post.......... Thanks

The DmxMaster library uses timer 2, which is also used for PWM on some pins. Using PWM pins for input is generally not a good idea when the timer(s) are being used for other purposes.

Brilliant thanks :-) just wondering how you knew the library uses timer 2? Cheers

joshbailz: Brilliant thanks :-) just wondering how you knew the library uses timer 2? Cheers

This was a clue:

/* TIMER2 has a different register mapping on the ATmega8.
* The modern chips (168, 328P, 1280) use identical mappings.