Posts: 9
Joined: Mon Apr 22, 2019 1:41 am

WiringPi + Perl - button inputs randomly stop working

Thu May 02, 2019 12:58 am


I've written a Perl program that uses SDL to display graphics and sound based on a couple of LED button inputs. Everything works fine most of the time, but twice I've had a situation where the RPi acts as if the buttons are not connected (or at least it doesn't take input from them). This seems to happen after the RPi has been turned off or reset.

When this happens, the program seems to be functioning correctly. It still lights and darkens the LEDs on queue, video displays, everything seems fine except the button input doesn't occur.

The first time this happened I thought maybe I had a short/bad connection in the button wires (I'm using a somewhat complex set of connector steps to connect the LED buttons to the GPIO pins). I think I rebooted the RPi and it worked and I didn't think about the issue again.

Today it happened again. A reboot didn't help. I ended up taking the RPi out of the case (I have custom connectors for internal wiring inside) and I checked the wiring from buttons to GPIO pins and everything checked out. I reassembled and booted but still the buttons didn't seem to work. I took everything apart again and swapped out the RPi board for a new one and still no luck.

In desperation, I wrote a simple button input check program using an alternate perl library (Device::BCM2835) and suddenly the buttons are working again. I went back to my original program that uses the WiringPi library and it works too! I'm completely confused at this.

I think I have pretty much ruled out a hardware/wiring issue. I'm wondering if there is some type of setting that I'm missing for WiringPi or the RPi? Maybe a semi-persistent setting? It seems to work reliably over and over until it doesn't and then it just doesn't work until something gets set/reset and then all is well again.

Here's a very abbreviated bit of code that I use to set up the buttons and LEDs.

Any thoughts or suggestions that would help me track this down would be appreciated.

Code: Select all


use strict;
use warnings;

use RPi::WiringPi;
use RPi::Const qw(:all);

my $pi = RPi::WiringPi->new;

# Set up to read button input pin 18 (GPIO 24) & pin 20 (GND)
my $green_button = $pi->pin(24);

# Set up to illuminate LED on pin 16 (GPIO 23) & pin 14 (GND)
my $green_LED = $pi->pin(23);

# check for button press
my $green_level = $green_button->read;

    if ( $green_level == 0 ) {    # green button pushed    
       # do stuff

Posts: 15
Joined: Fri Jan 13, 2017 2:40 pm

Re: WiringPi + Perl - button inputs randomly stop working

Sun May 19, 2019 1:34 am

I'm the author of RPi::WiringPi.

Can you please post the wiring diagram you're using?

Also, what version of RPi::WiringPi are you using? (eg:

Code: Select all

perl -MRPi::WiringPi -e 'print $RPi::WiringPi::VERSION'

Update: There's no settings that you're missing with the simple code you added to your post. Perhaps you could share what you originally had and I'll have a look. Even on a program crash, the software will perform all cleanup of all pins and set them back to the way you had them all before your program started.

One last thing, have you tried switching out GPIO pins? Perhaps you've got one (or more) that are having slight issues.

Return to “Other programming languages”