How can I prevent runaway "Keystroke on pir" rpi4 with dtoverlay?

92 Views Asked by At

So there is a dtoverlay function that you can add to the config.txt file that allows a gpio input to trigger a keystroke on raspberry pi4. However, it has given me some complications. For example, I have a motion censor that is supposed to activate the backspace key but every time my motion activates I get a runaway occurrence of the keystroke so that it presses the backspace key repeatedly until the motion is activated again then stops briefly then starts again. I gained some control over it by running the circuitry through a nand gate with a 1k resistor but I still get phantom keystrokes sometimes when the room has been completely empty where the motion detector is located. It could be the motion censor I'm using though as it was really cheap china generic. Is there someway to ensure only one instance of the keystroke? Potentially force the gpio input to pull back to 0 after the initial pull to 1? I've tried multiple physical fixes to make sure there isn't some residual voltage being detected on the input causing this and have had limited or no success. In case you were wondering here is the function:

    sudo nano /boot/config.txt
    #Keystroke on PIR
    dtoverlay=gpio-key,gpio=17,keycode=14, 
    label="KEY_BACKSPACE" gpio_pull=1
1

There are 1 best solutions below

0
Kevin Green On

Ok, so feeling a bit silly now but I was able to answer my own question. I wanted to leave the initial question on here incase anyone else runs into this in the future. The issue was that the designated GPIO (17 in my case) was being pulled high then going low after a few seconds which is the opposite of what the "dtoverlay=gpio-key" is designed to do by default. Because the architects mostly had push buttons in mind when they added this to the kernel, It defaults to key up on rising voltage and key down on dropping voltage. So my motion detector would go high when motion was detected causing really nothing to happen since it was the first occurrence of the event and then would drop causing the key down effect. That effect would then run uninterrupted until the next time the motion censor activated. The motion censor was causing the GPIO pin to go high, then low and was basically just pausing a long continuous keystroke. I had to modify the config to override the default functions here like this: Keystroke on PIR:

    sudo nano /boot/config.txt
    # add line
    #Keystroke on GPIO (PIR activated)
    dtoverlay=gpio-key,gpio=17,active_low=0,gpio_pull=1,keycode=14, 
    label="KEY_BACKSPACE"

This solved the continuous or "runaway" keystroke that I described earlier. My motion detector is now (mostly) working the way I intended with the small exception of phantom activations from time to time. I'm still trying to work that bit out. Maybe grounding issue or being too close to the Pi's wireless chip. Not sure exactly but I will keep searching for an answer to that one and report back once I have it.