Input Receive High Even Not Triggered

Hi,

I am new in arduino. Currently, my arduino are triggered when receive a signal from PLC relay. But sometimes, it also receive a signal even the PLC relay didn't triggered.

It keep sent serial with value '3' even not triggered. How I can avoid this by program?
Please guide and support me.

void setup()
{
    pinMode(12, OUTPUT); 
    digitalWrite(12, HIGH); 
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
    pinMode(8, INPUT_PULLUP);
    Serial.begin(9600);
    while (!Serial);

}

void loop() 
{
    if (digitalRead(8) == LOW) {
        Serial.println("3");
        delay(1000);
    }
  
    if (Serial.available())
    {
        int state = Serial.parseInt();
        if (state == 1)
        {
            digitalWrite(12, LOW);
            delay(100);
            digitalWrite(12, HIGH);
        }
        if (state == 2)
        {
            digitalWrite(13, LOW);
            delay(100);
            digitalWrite(13, HIGH);
        }
    } 
}

I just tried it with a switch to ground in pin 8 and it works ok for me.

Of course if I hold the switch closed it repeats the "3" every second so are you 100% sure that the PLC is reverting to a high? Have you verified that the input to pin 8 is changing, say with a volt meter or a logic probe?

elvon_blunden:
I just tried it with a switch to ground in pin 8 and it works ok for me.

Of course if I hold the switch closed it repeats the "3" every second so are you 100% sure that the PLC is reverting to a high? Have you verified that the input to pin 8 is changing, say with a volt meter or a logic probe?

Yes im sure it reverting to a high sometimes. Nothing abnormal with the volt.

If I try with the switch, it is same result as yours. But it happen when connect it with PLC relay.

SafwanR:
Yes im sure it reverting to a high sometimes**.**
[/quote]
Not always, then?

elvon_blunden:
Not always, then?

Yes, not always happen. For example, Relay PLC pass 10 signal to arduino, but the arduino might receive 12 or 13 signals. I never experienced use arduino with Relay PLC before.

Maybe it's a timing thing.

How long are the low pulses? If they can be longer than the Arduino delay, then the Arduino will see 2x lows even though the PLC thinks it's one. Conversely, any short lows from the PLC that arrive during the delay will be missed.

Then those delays in the led on/off parts will also upset things.

See StateChangeDetect to see how to look for rising / falling edges of inputs.

It may be time to re-jig your sketch with BlinkWithOutDelay style thinking, I'd say; see here and here.