Motion sensor value 0 only

Asked

Viewed 53 times

0

I’m trying to detect movement using the mini PIR motion sensor but only at 0, does anyone know how to solve this problem? The code is as follows::

//Declaramos os pinos que vamos a usar
int Pin = 7;   //Analogico A5 para o sensor
int ledPin = 9;       //Digital 9 para o LED

//Declaramos uma variável para armazenar o valor que nos devolve o sensor
int val = 0;        
int state = LOW;


 bool executed = false;
void setup()
{
  Serial.begin(9600);         //Iniciamos a comunicação serial
  pinMode(ledPin, OUTPUT);    //Configuramos o pino digital 9 como saída
  pinMode(Pin, INPUT);  //Configuramos o pino analogico A5 como entrada
}

void loop()
{
  val = digitalRead(Pin);    //Lemos o valor analogico que nos devolve o sensor
  if(val == HIGH)                   //Si o valor é maior que 100
  {
    Serial.println(val);          //Sacamos o valor do sensor por serial
    digitalWrite(ledPin,HIGH);    //Acendemos o LED
    delay(3000);                  //Fazemos uma pausa de 3 segundos

    if(state == LOW)
    {
      Serial.println('MOtion detected...');
       state = HIGH;
     }
  }
  else                            //Se o valor é menor que 100
  {
     Serial.println(val);         //Obtemos o valor do sensor por serial
     digitalWrite(ledPin,LOW);    //Apagamos o LED

    if(state == HIGH)
    {
      Serial.println('MOtion stop...');
       state = LOW;
     }
  }
}
  • I think this community can help you: https://arduino.stackexchange.com

  • If the input is analog, why are you using the function digitalRead for reading?

  • The input would not be analog?

  • I have used as analog and as digital and in both happens the same

1 answer

0

Before analyzing the code, check that the mini Pir works without the Arduino: inserir a descrição da imagem aqui

Check your scheme if it is in accordance with the proposed scheme: inserir a descrição da imagem aqui

with the code below:

void setup() {
  pinMode(2, INPUT); //Pin 2 as INPUT
  pinMode(3, OUTPUT); //PIN 3 as OUTPUT
}


void loop() {
  if (digitalRead(2) == HIGH)
  {
  digitalWrite(3, HIGH);   // turn the LED/Buzz ON
  delay(100);                       // wait for 100 msecond 
  digitalWrite(3, LOW);   // turn the LED/Buzz OFF
  delay(100);                       // wait for 100 msecond 
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.