Error with digital arduino input

0

Hi, I have a question about my arduino, I'm trying to use an on-site sensor in an arduino one, this one throws a 5-volt signal or 0 v depending on whether or not it detects an object, my code is as follows

const int pinLED = 12;
int f;
int pirState = LOW;

void setup() {
  // put your setup code here, to run once:
  pinMode(pinLED, INPUT);
  Serial.begin(9600);

}
void loop()
{
   f = digitalRead(12);
   if (f == HIGH)   //si está activado
   { 
    if (pirState == LOW)  //si previamente estaba apagado
      {
        Serial.println("Sensor activado");
        pirState = HIGH;
      }
   } 
   else   //si esta desactivado
   {
      if (pirState == HIGH)  //si previamente estaba encendido
      {
        Serial.println("Sensor parado");
        pirState = LOW;
      }
   }
}

What happens is that when I connect the voltage cable of the sensor the arduino starts printing on the serial sensor activated sensor stopped, as if they were making very fast changes of voltage when I'm not even sending voltage, even if I have connected only the cable does this, I do not know if it is a mistake in the programming and of my arduino if someone can help me thanks.

The error that I think it can be is that it is not reading a 0 and in itself or any voltage above 0 causes it to trigger a change in the where it detects voltage and does not detect, because when sending the 5v stops and only Sensor is activated, while when these 5v are removed the error is made that I comment

    
asked by R. Nuñez 09.03.2018 в 20:50
source

0 answers