raspberry pi zero 2 w power button on SCL; want to use secon

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
garberw
 
Posts: 130
Joined: Thu Feb 17, 2022 2:25 pm

raspberry pi zero 2 w power button on SCL; want to use secon

Post by garberw »

please help:
power button shorts usual (original) SCL to GND. raspberry pi zero 2 w power button has to be on SCL in order to reboot from power off state; want to use second hardware I2C bus to get SCL,SDA for sensors;

/boot/config.txt contains:
[all]
enable_uart=1
dtoverlay=disable-bt
dtoverlay=i2c-gpio,i2c_gpio_sda=0,i2c_gpio_scl=1,bus=3
# next line gets two hardware i2c buses (1 and 3) but still python code below does not work
# dtoverlay=i2c-gpio,i2c_gpio_sda=19,i2c_gpio_scl=26,bus=3

diagnostics:

garberw@weathera>>> root@weathera### i2cdetect -l
i2c-3 i2c 3.i2c I2C adapter
i2c-1 i2c bcm2835 (i2c@7e804000) I2C adapter
i2c-2 i2c bcm2835 (i2c@7e805000) I2C adapter

root@weathera### i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

root@weathera### i2cdetect -y 3
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- 29 -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- 38 -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@weathera###

this is the code I want to run to provide an i2c bus for sensors e.g. TSL2591:
def setup_i2c(self):
# self.i2c = board.I2C() # uses board.SCL and board.SDA
# valid I2C ports: ((1, 3, 2), (0, 1, 0)) i.e.:
# (scl, sda) =
# ( 1, 0)
# ( 3, 1)
# ( 2, 0)
# 3.3V pin 1 / pin 2 5V
# sda gpio 2 pin 3 / pin 4 5V
# scl gpio 3 pin 5 / pin 6 GND
# ----------------------------
# GPIO 0 = pin 27
# GPIO 1 = pin 28
# GPIO 2 = pin 3 = sda normally
# GPIO 3 = pin 5 = scl normally
# ----------------------------
# so you want
# (pin 28, pin 27) = (GPIO 1, GPIO 0)
scl = 1 # GPIO 1
sda = 0 # GPIO 0
self.i2c = busio.I2C(scl, sda)
return 0

This is the error message: not sure what "Valid I2C ports" means ????????????????

File "/usr/local/lib/python3.9/dist-packages/busio.py", line 123, in init
raise ValueError(
ValueError: No Hardware I2C on (scl,sda)=(1, 0)
Valid I2C ports: ((1, 3, 2), (0, 1, 0))

QUESTION:

Since I got two hardware I2C buses with
dtoverlay=i2c-gpio,i2c_gpio_sda=19,i2c_gpio_scl=26,bus=3
can I use my adafruit sensors with this I2C bus 3?
Briefly what is the code I would need in "setup_i2c" above
(a one liner probably not including comments)
i.e. how do you use self.i2c = busio.I2C(scl,sda) please?
I assume busio.I2C arguments scl,sda refer to GPIO pin numbers, no?

edit:
I see here that you can only have one hardware i2c bus (there is a second one but that is reserved):
https://raspberrypi.stackexchange.com/q ... nd-i2c-bus
so I am not sure wht pins 19 and 26 were doing but the i2cdetect scan was much faster !!!!

User avatar
garberw
 
Posts: 130
Joined: Thu Feb 17, 2022 2:25 pm

Re: raspberry pi zero 2 w power button on SCL; want to use s

Post by garberw »

https://www.youtube.com/watch?v=FUAiELC76aw
# "Using the second (I2C 0) port on a Raspberry Pi"
# next line of "/boot/config.txt" is always a software i2c bus;
dtparm=i2c-gpio ... etc ...
# I believe this next line --IS-- in fact a hardware i2c bus;
dtparm=i2c_vc=on

pin 27 = data (SDA)
pin 28 = clock (SCL)
need 2 Kohm pull ups to 3.3V on both
(WIRING: 3.3V connected to pin 27 with resistor)
(WIRING: 3.3V connected to pin 28 with resistor)
see "/boot/overlays/README" and search for "i2c_vc";

NOTE: says downside is you can not use camera;

This says: "I also read that you can use I2C bus 0, which is used by the OS, but that is risky."
https://raspberrypi.stackexchange.com/q ... nd-i2c-bus
recommends 4k7 resistors as pull-ups;
says hardware bus on GPIO2/3 uses 1k8 resistors as pull-ups;
And also says adding pull-ups makes "i2cdetect -y bus#" much faster if referring to a software bus;

Does anyone know why the bus0 is so-called "risky" please?

EDIT: bus0 is definitely out of the question it is used for essential hardware purposes;
https://forums.raspberrypi.com/viewtopic.php?t=138897

User avatar
garberw
 
Posts: 130
Joined: Thu Feb 17, 2022 2:25 pm

Re: raspberry pi zero 2 w power button on SCL; want to use s

Post by garberw »

Conclusion so far:
The adafruit sensor library blinka or whatever I2C object constructor ("__init__" method) seems to need a hardware I2C bus namely bus 1 and so does the ability to power ON with a button, although you can certainly power OFF with any pin you like without even an extra I2C bus; see:
https://howchoo.com/g/mwnlytk3zmm/how-t ... spberry-pi
that website is what started this whole thing but does not explain how to add anything ELSE to SDA and SCL;
Somewhere I read that it would not damage the hardware to have the button AND the sensors connected to the normal I2C hardware bus "bus 1" standard SDA and SCL (using only one I2C bus plus the reserved one; no software I2C bus) then the button would just short SCL to ground interfering with the sensor on SCL but so what;

I tried it and the adafruit or blinka constructor for the sensors on SDA/SCL did not work or something failed I forget what; I can't see how this could fail though; will try again;

EDIT:
obviously the script on the howchoo website will not work for shutdown if anything else is connected to SCL. The shutdown script is simply
#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess
print("garberw ==== note two warnings are harmless")
print("garberw ==== warning channel already in use (it always says this)")
print("garberw ==== physical pull up resistor already fitted on this channel (SCL does have one)")
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)
subprocess.call(['shutdown', '-h', 'now'], shell=False)

for sure if it sets a pull down on SCL you can't use it (DUH).

So the solution is to use two buttons, one to shut down (any unshared pin) and one to reboot (has to be SCL = GPIO 3). Will test and report solution.

User avatar
garberw
 
Posts: 130
Joined: Thu Feb 17, 2022 2:25 pm

Re: raspberry pi zero 2 w power button on SCL; want to use s

Post by garberw »

Yup that works.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”