I cant get my PIR sensor to open a tinkerkit relay after it closes a relay

Hey Guys,

I am trying to get my PIR sensor to activate and deactivate the relay when motion is sensed. I was able to get it to turn on, but once on, it will not turned off. I have run into a lot of code but they all turn off the relay when no motion is detected. I have it connected to the N.O. on the tinkerkit relay and have downloaded the tinkerkit library. Here's the code I was able to come up with so far:

#include <TinkerKit.h>

int pirPin = 13;
int ledPin = 12;
TKRelay relay(11);

void setup(){
Serial.begin(9600);
Serial.println("Warming up...");
delay(7000);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
}
void loop (){
Serial.print("IN2 = ");
Serial.println(digitalRead(13), DEC);
delay(1000);
digitalWrite (ledPin,LOW);
int sensor_1 = digitalRead(pirPin);
if (sensor_1 == HIGH){
digitalWrite(ledPin,HIGH);
delay(250);
digitalWrite(ledPin,LOW);
delay(250);
digitalWrite(pirPin,LOW);
relay.on();
}
}

Also, the led light comes on automatically after PIR warm up and flashed 3x instead of once. Any ideas?

There's nothing in your code that turns the relay off, so that behaviour is unsurprising.

One way to do it would be to use the relay's state method when you detect movement. If the relay is on, turn it off and vice versa.

I had to rewrite the code from scratch because my computer had issues.I am not sure how to use the state function of even how to begin the next code. Can you please give me an example?

This is the additional code I came up with :

#include <TinkerKit.h>

int pirPin = 13;
int ledPin = 12;
TKRelay relay(11);

void setup(){
Serial.begin(9600);
Serial.println("Warming up...");
delay(7000);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
}
void loop (){
Serial.print("IN2 = ");
Serial.println(digitalRead(13), DEC);
delay(1000);
digitalWrite (ledPin,LOW);
int sensor_1 = digitalRead(pirPin);
int relay_1 = digitalRead(11);
if (sensor_1 == HIGH){
digitalWrite(ledPin,HIGH);
delay(250);
digitalWrite(ledPin,LOW);
delay(250);
digitalWrite(pirPin,LOW);
relay.on();
}
else{ (sensor_1==HIGH && relay_1==HIGH){//error occurs here
digitalWrite(ledPin,HIGH);
delay(250);
digitalWrite(ledPin,LOW);
delay(250);
digitalWrite(pirPin,LOW);
relay.off();
}
}
}

I'm getting an expected ; or [ error on line 30

if this is true
if (sensor_1 == HIGH){

then this one will never happen (and it needs an "if" added) because the initial 'if' already occurred:
else{ if (sensor_1==HIGH && relay_1==HIGH){//error occurs here

so you may need to adjust your logic.

Also, many PIRs just have an open collector output to pull an input low, you may need a 10K pullup to bring it back high.

Thanks CrossRoads, any idea on how i can adjust the code to open the circuit back up? I am new to programming and don't know much. Also, you mean to add the resistor to the out pin on the PIR?

Yes, pullup on the PIR pin.

You seem to be reading the PIR pin, and then writing it. Once declared as an INPUT, all that writing it does is enable/disable the internal pullup resistor.
So read it, and after you see it active, keep reading it until it goes inactive, then act when you see it go inactive again.

okay CrossRoads,

I had modified the code and added a pull up 10k ohm resistor to vcc, 100 ohm resistor to the out. I am still getting the same thing. Here the code as it stands now:

#include <TinkerKit.h>

int pirPin = 13;
int ledPin = 12;
TKRelay relay(11);

void setup(){
Serial.begin(9600);
Serial.println("Warming up...");
delay(7000);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
}
void loop (){
Serial.print("IN2 = ");
Serial.println(digitalRead(13), DEC);
delay(1000);
digitalWrite (ledPin,LOW);
int sensor_1 = digitalRead(pirPin);
int relay_1 = digitalRead(11);
if (sensor_1 == HIGH){
digitalWrite(ledPin,HIGH);
delay(250);
digitalWrite(ledPin,LOW);
delay(250);
relay.on();
}
else{
if(sensor_1==HIGH && relay_1==HIGH){
digitalWrite(ledPin,HIGH);
delay(250);
digitalWrite(ledPin,LOW);
delay(250);
digitalWrite(pirPin,LOW);
relay.off();
}
}
}

also, I am using a parallax PIR Sensor (Rev B):Microcontroller KickStarts | LEARN.PARALLAX.COM