I am testing my pir sensor. I encountered a problem. It is giving me wrong readings and it take about FIVE minutes to print on serial monitor.
int led = 13; // the pin that the LED is atteched to int sensor = 2; // the pin that the sensor is atteched to int state = LOW; // by default, no motion detected int val = 0; // variable to store the sensor status (value) void setup() { pinMode(led, OUTPUT); // initalize LED as an output pinMode(sensor, INPUT); // initialize sensor as an input Serial.begin(9600); // initialize serial } void loop(){ val = digitalRead(sensor); // read sensor value if (val == HIGH) { // check if the sensor is HIGH digitalWrite(led, HIGH); // turn LED ON delay(100); // delay 100 milliseconds if (state == LOW) { Serial.println("Motion detected!"); state = HIGH; // update variable state to HIGH } } else { digitalWrite(led, LOW); // turn LED OFF delay(200); // delay 200 milliseconds if (state == HIGH){ Serial.println("Motion stopped!"); state = LOW; // update variable state to LOW } } }
Given below is the sensor
And I have properly connected the wires
int led = 13; // the pin that the LED is atteched to int
sensor = 2; // the pin that the sensor is atteched to int
state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup()
{ pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial }
void loop(){ val = digitalRead(sensor); // read sensor
value if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON delay(100); // delay 100 milliseconds
if (state == LOW) { Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH } }
else { digitalWrite(led, LOW); // turn LED OFF delay(200); // delay 200 milliseconds if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW }
}
}
Hi,
I have troubleshooted your code to get it to compile, also changed a variable name, "state".
If you look at your code you will see that "state" is in a highlighted colour, this means it is used in C++ code already as a reserved variable and may cause odd bugs.
I have also used the "Auto Format" tool , TOOLs tab and select it.
I have put the { and } on their own separate line so that the structure of your code can be easily read.
int ledPin = 13; // the pin that the LED is atteched to int
int sensorPin = 2; // the pin that the sensor is atteched to int
bool detectionstate = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup()
{
pinMode(ledPin, OUTPUT); // initalize LED as an output
pinMode(sensorPin, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop()
{
val = digitalRead(sensorPin); // read sensor
if (val == HIGH) // check if the sensor is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED ON delay(100); // delay 100 milliseconds
if (detectionstate == LOW)
{
Serial.println("Motion detected!");
detectionstate = HIGH; // update variable state to HIGH }
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF delay(200); // delay 200 milliseconds if (state == HIGH){
Serial.println("Motion stopped!");
detectionstate = LOW; // update variable state to LOW }
}
}
}
Can you post a circuit diagram of your project please?
Can you explain exactly what you want your code to do?
If you look at your code you will see that "state" is in a highlighted colour, this means it is used in C++ code already as a reserved variable and may cause odd bugs.
Close, but no cigar
For instance, if I type softStop into a sketch it is highlighted but that is because it is the name of a function in a library that I wrote and it is highlighted whether or not the library is #included in the sketch. The highlighting is triggered by an entry in the keywords.txt file that I have in the library folder.
Summary : the Arduino keyword colour coding is broken and is a waste of time
int led = 13;
int sensor = 2;
int state = LOW;
int val = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(sensor);
if (val == HIGH) {
digitalWrite(led, HIGH);
delay(100);
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}
UKHeliBob:
Summary : the Arduino keyword colour coding is broken and is a waste of time
Not completely. I suspect that most users would not know the cause of the highlighting, nor want to take the time to research it- just simply change the variable name.
Vaibhav2710:
Yes I have tried adjusting the delay control...
Well, don't.
Just turn it to minimum, and leave it there.
Then you have the shortest release time (about 3 seconds),
so you can use the Arduino to stretch that to any length (if needed).
Leave the sensitivity pot in the middle. It does NOT adjust detection distance.
It adjusts detection delay, to minimise false triggers.
Try this sketch.
Leo..
const byte ledPin = 13;
const byte pirPin = 2;
boolean pirState, previousState;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
pirState = digitalRead(pirPin); // read sensor pin
if (pirState != previousState) { // if changed
if (pirState) { // if HIGH
Serial.println(F("Motion detected!"));
digitalWrite(ledPin, HIGH); // LED ON
} else { // if LOW
Serial.println(F("Motion stopped!"));
digitalWrite(ledPin, LOW); // LED OFF
}
previousState = pirState; // update
}
}
I get to know my sensor really Does not work
In previous posts I have told about the readings I got from the sensor
But now I think whenever I touches top of sensitivity adjust potentiometer, It prints 'motion detected' on serial monitor.
PIR sensors are VERY sensitive.
You must not move within 5-7 metres, or they will register.
An open window or heater (hot/cold air) will also set them off.
As said, leave the sensitivity pot in the middle, and the time pot fully anticlockwise.
Leo..
Wawa:
As said, leave the sensitivity pot in the middle, and the time pot fully anticlockwise.
I have exactly done what you have said
Wawa:
PIR sensors are VERY sensitive.
You must not move within 5-7 metres, or they will register.
The main thing is when I wave my hand in front of the sensor it does not detect anything
But when I touch the tip of potentiometer it prints on serial monitor
Wawa:
An open window or heater (hot/cold air) will also set them off
Vaibhav2710:
How can I check that is my sensor dead or not
You have already been given some good tips. From your description, the sensor is working. It's you that is not understanding.
The PIR is EXTREMELY sensitive, but it is looking for motion, not the presence of a warm object. You can stand motionless in front of it and it will not detect you- because you aren't moving. The same with your hand in front of it. But, move your hand and t will detect motion.