digitalWrite delayed due to higher value resistor

Hi,

I made a piece of Arduino code that detects when a pin gets disconnected (based on arduino nano - How can I detect a disconnected pin? - Arduino Stack Exchange). This pin is connected to a PIR sensor. This code allows the board to detect when the sensor get's disconnected.

This works fine now, but I had to apply a small fix (delay in de code) to resolve an issue, but don't understand why I had to fix this.

In the circuit there is a resistor between the 2 pins (https://i.stack.imgur.com/soUQi.png):

  • 100k ohm resistor: I do NOT need to add a delay.
  • 1M ohm resistor: I have to add a 1ms delay before reading the value back.

This is a part of my Arduino code, it makes it probably more clear...

    ... 
    pinMode(tamperPin, OUTPUT);
    digitalWrite(tamperPin, HIGH);


    delay(1); // Needed when using a 1M ohm resistor
    if (digitalRead(pirPin) == HIGH)
    {
      digitalWrite(tamperPin, LOW);

      delay(1); // Needed when using a 1M ohm resistor
      if (digitalRead(pirPin) == LOW)
      {
        tamperCounter++;

        if (tamperCounter == 3)
        {
          Serial.print(millis() / 1000);
          Serial.println(": Detected sensor tampering!");          
        }
      }
      else
      {
       tamperCounter = 0; 
      }
    }
    else
    {
      tamperCounter = 0;

    }
    ...

Maybe someone can explain why this delay is needed when using a 1M ohm resistor? Thanks. :slight_smile:

Regards,
Sander

Firstly you'll probably fine delayMicroseconds(50) is enough.

But the basic issue is you have a long time constant in your RC circuit. Time constant is the product of
R in ohms by C in farads. Typical circuits have stray capacitance to ground of the order of 5 to 30pF
(or more if long cables are involved).

1M x 5pF = 5 microseconds time constant for the pin to charge/discharge if there's only 5pF, longer if more.

For logic speed signals its usual for signals to have resistances in the range a few hundred ohms to a few k ohms,
so that such delays are kept short.

In general the higher speed the circuitry the lower the impedances have to be, ultimately ending up with
transmission line techniques.

Thanks! You are right, I just tested with delayMicroseconds(50) and it's enough.

Is there a way to calculate or measure the capacitance of the circuit?

Sander88:
Is there a way to calculate or measure the capacitance of the circuit?

Yes, the laws of physics apply, many capacitors have the value marked on them,
and most chip's datasheets quote an approximate input capacitance for input
pins.

In general its tedious to calculate stray capacitance, but for PCB traces over a groundplane
you'll find calculator apps, cables normally have a capacitance-per-unit-length spec.

You'll find pull-ups for logic signals are normally in the 1k to 100k range, with 100k considered
"very weak".