Go Down

Topic: Trouble with PIR sensor/ servo circuit  (Read 323 times) previous topic - next topic


Aug 16, 2017, 11:46 pm Last Edit: Aug 17, 2017, 12:03 am by tommov
Hey, I'm very new to arduino so please forgive any rookie mistakes/ code formatting. Basically I have a PIR motion sensor, a servo motor, a push button, a relay and an LED.

When motion is detected, I want the servo to move 180 degrees then stop. The LED will then blink. After 5 button pushes, the LED will stop blinking and the servo will return to its initial position. The relay is there to disable the motion sensor after it has triggered the motor, and re-enable it afterwards.

I have my relay and servo connected to 5V on the arduino, with the button, LED and PIR sensor (and one pine of the relay) connected to digital pins (listed in the code). The servo moves fine an the LED works. I'm pretty sure I've got the wiring (at least between components) correct.

So far I've just got the servo moving 180 degrees and not doing much else. It doesn't even seem to be triggered by the sensor, it moves a few seconds after I upload the code.  :( .
I've attached and posted my code below.
Any help will be appreciated, thanks in advance!

Code: [Select]
#include <Servo.h>

Servo myservo;  

const int pirinputPin = 2;  
const int  buttonPin = 6;
const int ledpin = 13;
int pos = 0;                 // variable to store the servo position
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int LastButtonState = 0;     // previous state of the button
int pirState = LOW;          // we start, assuming no motion detected
int relaypin = 10;           // PIR connected to normally closed port (activates PIR sensor when not actuated)
int relaystate = LOW;

void setup() {
pinMode(13, OUTPUT);
pinMode(pirinputPin, INPUT);    
pinMode(relaypin, OUTPUT);
digitalWrite (relaypin, LOW );
pinMode(buttonPin, INPUT);


void loop() {
// read PIR input value
if (digitalRead(pirinputPin) == HIGH) {        
 // there was previously no motion detected
 if (pirState == LOW) {  
 Serial.println("Motion detected");
 pirState = HIGH;
 // PIR sensor disconnected (normally closed)
 digitalWrite (relaypin, HIGH);
 relaystate = HIGH;

if (pirState = HIGH && pos <=180); {  
// goes from 0 degrees to 180 degrees
pos = pos+1;


if (pos == 180) {
pirState= LOW;
// led blinks  
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin, LOW);  

else digitalWrite(ledpin, LOW);

// compare the buttonState to its previous state
if (buttonState != LastButtonState) {
   // if the state has changed, increment the counter
   if (buttonState == HIGH); {
     // if the current state is HIGH then the button went from off to on:
   // Delay a little bit to avoid bouncing
// save the current state as the last state, for next time through the loop
LastButtonState = buttonState;


if (buttonPushCounter ==5 && pos >0) {
// goes from 0 degrees to 180 degrees in steps of 1 degree
 pos = pos-1;
if (pos == 0 && relaystate == HIGH) {
 digitalWrite (relaypin, LOW);
 delay (10000);
 relaystate = LOW;


Aug 16, 2017, 11:55 pm Last Edit: Aug 17, 2017, 12:05 am by J-M-L
Hello and welcome

Don't attach the code, post it inline with code tags (read how to use the forum), you'll get more comments (can't read from a smartphone)

Also describe what is connected where and what's your power source

Most PIR needs at least 30 seconds to calibrate after powering it up, add a delay in your setup, something like

Code: [Select]

const int calibrationTime = 30; 

  // in setup and possibly at every wake up
  //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");

(Taken from this example)

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Go Up