Controlling a Stepper with a PIR with Stepper.h and Arduino Motor Sheild

Hi there,

I am currently trying to get a PIR to control a stepper motor so that when there is motion, the motor steps a given number of steps. I have found code that operates each device separately, but when I combine the two sets of code, the PIR no longer seems to detect movement.

Here is the PIR code on its own that allows the state of the PIR to be written to the serial port. In this case, the PIR responds correctly to movement:

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

/////////////////////////////
//SETUP
void setup(){
   
  Serial.begin(9600);
  
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
}

////////////////////////////
//LOOP
void loop(){
      
 while(digitalRead(pirPin) == LOW)
            {
            Serial.println(digitalRead(pirPin));
            delay(500);
            }
            
 while(digitalRead(pirPin) == HIGH)
            {
            Serial.println(digitalRead(pirPin));
            delay(500);
            }
 }

Then, before even writing any commands to control the motor, on adding the preparatory setup code for the motor control, the sketch stops working. That is to say that the PIR no longer seems to detect movement and stays on LOW consistently.

// Include the Stepper Library
#include <Stepper.h>

// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;


// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 400;

// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);

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

/////////////////////////////
//SETUP
void setup(){
  
   // Set the RPM of the motor
  myStepper.setSpeed(30);

  // Turn on pulse width modulation
  pinMode(pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);


  // Turn off the brakes
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);
  
  Serial.begin(9600);
  
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
}

////////////////////////////
//LOOP
void loop(){
      
 while(digitalRead(pirPin) == LOW)
            {
            Serial.println(digitalRead(pirPin));
            delay(500);
            }
            
 while(digitalRead(pirPin) == HIGH)
            {
            Serial.println(digitalRead(pirPin));
            delay(500);
            }
 }

Does anybody have any ideas as to why the above code seems to break the PIR sensing? Nothing in the wiring has changed, however I can post a photo of the wiring setup if it helps.

Thanks for your help in advance…

Ok, this problem is now solved. It is not a programming problem, there was not enough current on the board to power both the PIR and the Motor. It was fixed by providing an external power supply.

Thanks...