Qwiic Button + Rpi + Python

I’m trying a perhaps ill-advised setup of running plain Python on my Model B+ Raspberry Pi to control some Qwiic buttons over i2c – and having some issues. New to this so maybe I’m missing something.

The button does not have an official python driver (yet, I might build it), and so am doing low-level i2c prodding. Here is code that polls the button watching for status changes. The problem is … that I sometimes get presses/noise even when nothing has been pressed. It runs fine for a few seconds (no presses) and then sees presses when there weren’t any. It DOES pick up real presses in addition to the noise! I also have no problem setting the LED brightness and even blinking.

Any advice would be appreciated. I’ve tried shorter qwiic wires, only having the one device, increasing the sleeps. I haven’t yet tried messing with the i2c bus clock speed, I think the default is 100 kHz.

import time
import qwiic

qwiic_i2c = qwiic._QwiicInternal.get_i2c_driver()

# Set brightness to 0x10
qwiic_i2c.writeByte(111, 0x19, 0x0)

def monitor_button():
    print("Reset the current status")
    qwiic_i2c.writeByte(111, 0x03, 0)
    time.sleep(0.05)

    # Wait for a click
    while True:
        time.sleep(0.05)
        button_status = qwiic_i2c.readByte(111, 0x03)
        # Bit 0 (eventAvailable) is set to 1 when a new event occurs user must write 0 to clear.
        # Bit 1 (hasBeenClicked) defaults to 0 on POR, is set to one when the button is clicked, must be cleared by the user.
        # Bit 2 (isPressed) is set to 1 when the button is pushed
        event_available = button_status & 0b001
        has_been_clicked = button_status & 0b010
        is_pressed = button_status & 0b100

        print(f"Status: {bin(button_status)} Event Available: {event_available} Has clicked: {has_been_clicked} Currently pressed: {is_pressed}")

        if has_been_clicked:
            print(f"CLICK DETECTED")
            qwiic_i2c.writeByte(111, 0x03, 0)
            # break

monitor_button()

Sample output:

Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b0 Event Available: 0 Has clicked: 0 Currently pressed: 0
Status: 0b1111111 Event Available: 1 Has clicked: 2 Currently pressed: 4
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b1 Event Available: 1 Has clicked: 0 Currently pressed: 0
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED
Status: 0b11 Event Available: 1 Has clicked: 2 Currently pressed: 0
CLICK DETECTED

I made some progress! I decreased the i2c clock speed all the way down to 10 kHz and now it is completely stable. I’ll do some more experimenting to see how fast I can get it and still be stable. I wonder if I should mess with the pull down resistors? I also wonder how the pi might differ from an arduino type thingie for this.

Thanks to [this forum post I got it working! Switched to a gpio based (software) i2c implementation that can do clock-stretching. I think this is working at the default 100 kHz (and looks fast based on a display I have on the bus). A simple change to the /boot/config.txt file is all that was needed:

# What I had before to force 10 kHz
#dtparam=i2c_arm=on,i2c_arm_baudrate=10000

# Switch to software i2c, works much better! This uses the same bus=1
dtoverlay=i2c-gpio,bus=1,i2c_gpio_sda=2,i2c_gpio_scl=3

](https://forum.sparkfun.com/viewtopic.php?f=105&t=52585&p=214794&hilit=raspberry#p214794)

Awesome! Glad to see that everything is working for you now.

Thank you so much for posting the fix on the SFE forums. I am sure these posts will really help some of our users out.

If we don’t hear back from you soon we wish you a happy New Year!