programming a pir sensor using arduino

I need help with the code for programming a pir sensor to sense and count 3 different objects. Once those 3 objects have been sensed, a servo will activate.

equipment: (1) servo motor, (1) pir sensor, arduino uno

currently, my code activates the servo after only 1 object passes through the pir sensor. Here is a copy of my current code:

#include <Servo.h>

Servo myservo; //creates a servo object
//a maximum of eight servo objects can be created

int pos = 0; //variable to store servo position

//amount of 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 = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin

void setup(){
myservo.attach(4); //attaches servo to pin 4
Serial.begin(9600); //begins serial communication
pinMode(pirPin, INPUT);
pinMode(pirPos, OUTPUT);
digitalWrite(pirPos, HIGH);

//give the sensor time to calibrate
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");

//while making this Instructable, I had some issues with the PIR's output
//going HIGH immediately after calibrating
//this waits until the PIR's output is low before ending setup
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}

void loop(){

if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo

for(pos = 0; pos < 180; pos += 1) //goes from 0 to 180 degrees
{ //in steps of one degree
myservo.write(pos); //tells servo to go to position in variable "pos"
delay(5); //waits for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) //goes from 180 to 0 degrees
{
myservo.write(pos); //to make the servo go faster, decrease the time in delays for
delay(5); //to make it go slower, increase the number.
}

if(lockLow){
//makes sure we wait for a transition to LOW before further output is made
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}

if(digitalRead(pirPin) == LOW){

if(takeLowTime){
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 can assume the motion has stopped
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");
delay(50);
}
}
}

You may find that PIR sensors are not appropriate for this task. The cheap ones tend to have a dead period after switching off, to prevent retriggering by the electrical noise caused by switching the load.

How much time elapses between the sensor switching the load off and the next object coming into view ?

Maybe post a link ti the PIR sensor you are using.

I need help with the code for programming a pir sensor

This forum is about programming the Arduino. Find the PIR sensor programming forum for help programming your sensor.

to sense and count 3 different objects.

What objects? Do you know what a PIR sensor senses?

Ditch that stupid code. You started from a crappy example.

Where are you counting the number of times the sensor has become HIGH? How can you expect to move the servo after the count reaches three if you don't count? If you don't determine that the sensor has changed state?

About 20 seconds will elapse between the sensor switching off and the next object coming into view. I just need help with modifying the code so that the sensor doesnt go off after the first object and instead activates once all three have passed through.

PaulS:
This forum is about programming the Arduino. Find the PIR sensor programming forum for help programming your sensor.
What objects? Do you know what a PIR sensor senses?

Ditch that stupid code. You started from a crappy example.

Where are you counting the number of times the sensor has become HIGH? How can you expect to move the servo after the count reaches three if you don't count? If you don't determine that the sensor has changed state?

Im not sure how to go about moving the servo after the count reaches three. As of now it moves right after the first object has passed through. The objects are just 3 coins.

I need help with the code for programming a pir sensor to sense and count 3 different objects. Once those 3 objects have been sensed, a servo will activate. The objects are just coins.

equipment: (1) servo motor, (1) pir sensor, arduino uno

currently, my code activates the servo after only 1 object passes through the pir sensor. Here is a copy of my current code:

#include <Servo.h>

Servo myservo; //creates a servo object
//a maximum of eight servo objects can be created

int pos = 0; //variable to store servo position

//amount of 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 = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin

void setup(){
myservo.attach(4); //attaches servo to pin 4
Serial.begin(9600); //begins serial communication
pinMode(pirPin, INPUT);
pinMode(pirPos, OUTPUT);
digitalWrite(pirPos, HIGH);

//give the sensor time to calibrate
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");

//while making this Instructable, I had some issues with the PIR's output
//going HIGH immediately after calibrating
//this waits until the PIR's output is low before ending setup
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}

void loop(){

if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo

for(pos = 0; pos < 180; pos += 1) //goes from 0 to 180 degrees
{ //in steps of one degree
myservo.write(pos); //tells servo to go to position in variable "pos"
delay(5); //waits for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) //goes from 180 to 0 degrees
{
myservo.write(pos); //to make the servo go faster, decrease the time in delays for
delay(5); //to make it go slower, increase the number.
}

if(lockLow){
//makes sure we wait for a transition to LOW before further output is made
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}

if(digitalRead(pirPin) == LOW){

if(takeLowTime){
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 can assume the motion has stopped
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");
delay(50);
}
}
}

I need help with the code for programming a pir sensor to sense and count 3 different objects. Once those 3 objects have been sensed, a servo will activate. The objects are just coins

equipment: (1) servo motor, (1) pir sensor, arduino uno

currently, my code activates the servo after only 1 object passes through the pir sensor. Here is a copy of my current code:

#include <Servo.h>

Servo myservo; //creates a servo object
//a maximum of eight servo objects can be created

int pos = 0; //variable to store servo position

//amount of 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 = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin

void setup(){
myservo.attach(4); //attaches servo to pin 4
Serial.begin(9600); //begins serial communication
pinMode(pirPin, INPUT);
pinMode(pirPos, OUTPUT);
digitalWrite(pirPos, HIGH);

//give the sensor time to calibrate
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");

//while making this Instructable, I had some issues with the PIR's output
//going HIGH immediately after calibrating
//this waits until the PIR's output is low before ending setup
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}

void loop(){

if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo

for(pos = 0; pos < 180; pos += 1) //goes from 0 to 180 degrees
{ //in steps of one degree
myservo.write(pos); //tells servo to go to position in variable "pos"
delay(5); //waits for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) //goes from 180 to 0 degrees
{
myservo.write(pos); //to make the servo go faster, decrease the time in delays for
delay(5); //to make it go slower, increase the number.
}

if(lockLow){
//makes sure we wait for a transition to LOW before further output is made
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}

if(digitalRead(pirPin) == LOW){

if(takeLowTime){
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 can assume the motion has stopped
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");
delay(50);
}
}
}

basically you want to make a variable m use a variable r to read pir output when detected increment variable m by 1 and then testing var m to be equal to 3 activate servo

Please read and follow the directions in the "How to use this forum" post. Edit your post to add code tags ("</>" editor button).

@ruben5194, please do not cross-post. Threads merged.

@ruben5194, do not cross-post. Threads merged. Again.

The objects are just 3 coins.

A PIR sensor is useless for detecting cold metal coins. PIR sensors detect people, so you'll need to set your apparatus up to count people carrying coins. Good luck with that.

PaulS:
A PIR sensor is useless for detecting cold metal coins. PIR sensors detect people, so you'll need to set your apparatus up to count people carrying coins. Good luck with that.

At the risk of being seen to split hairs, a PIR sensor may work if the coins are cold (relative to the ambient temperature). The problem arises when these are at exactly the ambient temperature. The rest stands, of course.

The OP should look at say IR beam break or proximity sensors if the detection using PIR sensors proves to be unreliable.

The OP should look at say IR beam break or proximity sensors when the detection using PIR sensors proves to be unreliable.

I fixed that for you.