Qwiic Quad Relay with Raspberry Pi, i2c not working

Hi

I just got my Qwiic Quad Relay (https://www.sparkfun.com/products/15102) and connected it with Raspberry Pi 3 and wrote code with Python 3. I checked commands from Arduino example and first it seemed to work but at times there is strange behaviors. Some times status says that relay is ON, but it really is OFF. So my code tries to toggle it to “OFF” but then it is set ON :!: :!: :!: Some times I have seen relays to blink even if there is no activity in i2c bus!!??

I noticed there is no pullup resistors, so I added first 1.8k but same happened.

Then I saw this issue viewtopic.php?f=105&t=50806&p=207512&hi … ay#p207512 and changed my resistors to 8.2k (closest to 10k that I have) but same thing. I soldered the address jumber close but nothing changed (except the address).

At first I was using adafruit’s i2c library but then I wrote test code with SMBus:

import smbus

i2c_ch = 1
address = 0x6c

RELAY_ONE_STATUS = 0x5

bus = smbus.SMBus(i2c_ch)

count = 10000
r1_on = 0
r1_off = 0
r1_unknown = 0
error = 0

ON = 0xf
OFF = 0
state = OFF

#Turn all off
bus.write_byte(address, 0xA)
print("Test 10")
for i in range(count):
    try:
        state = bus.read_byte_data(address, RELAY_ONE_STATUS)
    except:
        error += 1
        continue
    if state == ON:
        r1_on += 1
    elif state == OFF:
        r1_off += 1
    else:
        r1_unknown += 1

print(f'On: {r1_on}, Off: {r1_off}, Unknown: {r1_unknown}, Error: {error}')

And here is the result:

>>> %Run relay_test.py
Test 1
On: 0, Off: 9212, Unknown: 788, Error: 0
>>> %Run relay_test.py
Test 2
On: 5956, Off: 3291, Unknown: 750, Error: 3
>>> %Run relay_test.py
Test 3
On: 6173, Off: 3061, Unknown: 763, Error: 3
>>> %Run relay_test.py
Test 5
On: 4595, Off: 4603, Unknown: 800, Error: 2
>>> %Run relay_test.py
Test 6
On: 0, Off: 9164, Unknown: 835, Error: 1
>>> %Run relay_test.py
Test 7
On: 3778, Off: 5583, Unknown: 638, Error: 1
>>> %Run relay_test.py
Test 8
On: 0, Off: 9379, Unknown: 621, Error: 0
>>> %Run relay_test.py
Test 9
On: 3965, Off: 5380, Unknown: 654, Error: 1
>>> %Run relay_test.py
Test 10
On: 0, Off: 9319, Unknown: 680, Error: 1
>>>

As you can see, on test 1, 6, 8, 10 status is unknown 7-10% at the time (many times the result value was 127). On the other tests, relays switched state during the test, don’t know why. During these tests, there was no other devices at the bus.

So is there someting I should know when trying to use Raspberry with Qwiic system (some special i2c controls/setup) or is this board broken?

Mika

Hi Mika,

That’s really cool you’re using the board with a Raspberry Pi. Have you tested with our example sketches with Arduino and a RedBoard to see if you still get the same weird behavior?

Hi!

I have Arduino Uno R2 but it is 5V and don’t have IO ref selection. And I don’t have spare level shifter to test with 5V.

Mika

In my case I have two MPU6050’s connected, which should have the addresses 0x68 and 0x69 however, neither one shows up.

They both are work fine when I tried connecting to an arduino.

The I2c is enabled and also i2c tools are installed.

Both issues seems to be clock-stretching. This is not supported as it should on the standard I2C on the Raspberry Pi :

From the MPU6050 datasheet : If a slave is busy and cannot transmit or receive another byte of data until some other task has been performed, it can hold SCL LOW, thus forcing the master into a wait state.

From the Attiny84 datasheet : The slave can insert wait states at start or end of transfer by forcing the SCL clock low. This means that the master must always check if the SCL line was actually released after it has generated a positive edge

Maybe give this a try :

I run into that issue sometime ago for the SCD30 and created a version that DOES provide clock stretching : https://github.com/paulvha/twowire. You can check how I have used that on https://github.com/paulvha/scd30_on_raspberry

Hi paulvha

paulvha:
Both issues seems to be clock-stretching. This is not supported as it should on the standard I2C on the Raspberry Pi :

I googled and found https://learn.adafruit.com/circuitpytho … stretching.

I tested with speed of 10kHz, and run my test several times and it WORKED.

Now I need to add other devices to bus and see if it works with them also and check that this won’t cause other problems…

Maybe SparkFun quys could think how to get these Qwiic systems to Rpi without extra settings…

Mika

Good to hear it solved your issue, BUT… this is NOT an error or issue for Sparkfun. According to the I2C protocol definition clock stretching must be supported by the master.

The BCMXX-chip in the RPI is not handling it correct. Hence many other sensors that use clock stretching (like a CCS811) fail, unless you put the speed low (as you did) and “hope and pray” that the slave-device will release on-time before the RPI will start next communication or use different driver (like twowire)

However… Sparkfun could include a warning/hint in there documentation.

BTW if you would connect the relay to an ESP32, you will run into the same issue as clock stretching is not implemented on the default I2C driver either