Project: I am creating a light switch panel to control my LIFX WiFi lights. I have 3 buttons hooked up to GPIO pins 6, 20 and 22 and I'm using the GPIO.add_event_detect to capture when the button is pressed.
Problem: My issue is that if I push one button, sometimes it triggers one or more of the other channels. For example pressing button channel 6 will also trigger 20 and/or 22. The odd thing is that it works perfectly fine 80% of the time so I really don't believe its anything physical like signals crossing wires. I've been at this on and off for about 2 weeks.
What I've Tried: Originally I made a little protoboard which I soldered leads to a 40 pin header and I thought that there was a chance a wire was crossing causing this phantom button press. I then bought a $20 board off Amazon that has screw terminals for each GPIO pin... same issue. I tried switching up the GPIO pins I was using to be further apart on the board, in case somehow they were crossing.
I've tried updating the GPIO library but I have the latest.
I've tried different push buttons to see if they where faulty.
I've checked that pressing one button doesnt wiggle the other buttons enough that they trigger.
I've tried using GPIO.input(channel) to see if the button is pressed in the buttonPressed callback but more times than not that makes it so that no buttons are pressed (even the one I actually pressed).
I've tried removing other code for the LIFX lan library and the TCP server that I start.
The buttons are screwed into a piece of wood and have several inches between them so I know wires are not crossing there.
Next I'm going to try swapping to another PI but I have everything screwed down into a cabinet and its difficult to get to.
Setup: Raspberry Pi 2 B+ Python 2.7
Code: I've omitted irrelevant code to make it easier to read
import os
import sys
from time import sleep
import time
import random
import threading
import json
import itertools, glob
from socket import error as SocketError
from TcpClient import *
from lifxlan import BLUE, CYAN, GREEN, LifxLAN, ORANGE, PINK, PURPLE, RED, YELLOW
from itertools import cycle
import RPi.GPIO as GPIO
BUTTON_1 = 6
BUTTON_2 = 20
BUTTON_3 = 22
# Setup Network
HOST = ''
PORT = 9998
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
lifx = None
currentScene = None
def stopScene():
...
def setScene(lan, scene):
...
def lightsOn(lan):
...
def lightsOff(lan):
...
class Message(object):
...
class Client(threading.Thread):
...
class Server(object):
...
def buttonPressed(channel):
global lifx
global currentScene
global BUTTON_1
global BUTTON_2
print("Button Pressed: " + str(channel));
lifx = LifxLAN(None)
print("Discovering Lights...")
numLights = len(lifx.get_lights())
print("Lights discovered: " + str(numLights))
GPIO.add_event_detect(BUTTON_1, GPIO.RISING, callback=buttonPressed, bouncetime=1000)
GPIO.add_event_detect(BUTTON_2, GPIO.RISING, callback=buttonPressed, bouncetime=1000)
GPIO.add_event_detect(BUTTON_3, GPIO.RISING, callback=buttonPressed, bouncetime=1000)
server = Server(HOST, PORT)
server.start(10)
Example Output: Pressing only button 6 (Left button in picture)
Discovering Lights...
Lights discovered: 2
[SERVER] Starting...
[SERVER] Listening
Button Pressed: 6
Button Pressed: 20
Example Output: Pressing only button 6 again
Discovering Lights...
Lights discovered: 0
[SERVER] Starting...
[SERVER] Listening
Button Pressed: 22
Button Pressed: 20
Button Pressed: 6
UPDATE 1: I just swapped to a Raspberry PI 3 and using the same SD card I now only get 1 of 3 button registering, then after a few presses it just stops detecting presses. Double checked to ensure that the wires are still all connected.
UPDATE 2: When swapping the hat to the RPI3, I missed an entire row of pins which caused my previous issue. I reseated it and ran the monitor and everything seems to be okay. I noticed one of my inputs however didn't fire on every button press so I switched to another (could be a pi hat issue). I'm not seeing any phantom button presses as of right now but only time will tell if changing PI's fixed it.
Event:
is being printed. Surely the actual button press is a falling edge event? You default it high with the pull so presumably a button press connects the other end to ground. Run abyz.me.uk/rpi/pigpio/examples.html#Python_monitor_py to see if that has the same problem with multiple signals.