-1

CONTEXT: I was following this tutorial to add an on/off switch to my Raspberry Pi 3. It works perfectly and now I can turn on/off the raspberry by using the GPIO 3.

PROBLEM: I want to add an OLED display (like this one) for stats, but to make it work via i2c, I need to use GPIO 3 and 5. Creating a conflict with the GPIO 3 I'm using for the "turn on" functionality (sadly, this is the only pin that allows that)

QUESTION: How can I integrate an OLED i2c display to the raspberry pi without removing the "turn on" functionality I already implemented using python? Did someone here achieve something similar?

Thanks :D

Bufofa
  • 3
  • 2

3 Answers3

1

Not your question exactly, but...

the tutorial you used is more complicated than it needs to be. There is now a device tree overlay (dtoverlay) that requires no code. It's described in the documentation for gpio-shutdown:

This overlay only handles shutdown. After shutdown, the system can be powered up again by driving GPIO3 low. The default configuration uses GPIO3 with a pullup, so if you connect a button between GPIO3 and GND (pin 5 and 6 on the 40-pin header), you get a shutdown and power-up button. Please note that Raspberry Pi 1 Model B rev 1 uses GPIO1 instead of GPIO3.

You can use any momentary pushbutton switch, and the gpio-shutdown overlay also gives you the option of a debounce function on the switch if contact bounce is an issue - or if you simply want to delay the shutdown for a short period.

As for your question:

In addition to the "bit-banging" software i2c suggested in joan's answer, you may also be able to use the "hardware-based" i2c0 channel on the RPi 3. AIUI, i2c0 may be unavailable for alternative assignments if you use the HDMI port, or the Pi camera (?), but otherwise it should work fine. I am using i2c0 for the Realtime Clock on my RPi 3B+, and have encountered no issues - click the link for configuration details.

Another potential complication: As is typical of Amazon's vendors, their specifications are sketchy and unreliable. The vendor you referenced states:

Embedded driver IC: Communication: I2C/IIC Interface, only need 2 I/O ports; Compatible with MMDVM, Pi-Star, and it is compatible with Raspberry pi

If you know what this means or have some experience with this device, pay no heed, but if you need a RPi driver, it may simplify your integration to learn which one it is.

In any case, wrt the i2c0 configuration and hookup, that can all be done in your /boot/config.txt file. Know first that i2c0 is a (now deprecated) synonym for i2c_vc. Here are the changes/additions you'll need to make when editing /boot/config.txt:

  1. dtparam=i2c_vc=on
  2. i2c_vc_baudrate: default is 100,000; otherwise set dtparam=i2c_vc_baudrate=<value>

Alternatively, use the dtoverlay (ref the Info section for this overlay in the documentation):

  1. dtoverlay=i2c0

After rebooting, you should be able to use i2c0 on GPIO 0 & 1 (physical pins 27 & 28) to communicate with your display. Note that if you find you need to use a driver from the RPi distro, that may require a different overlay, which may or may not support i2c0.

FWIW:

The RPi 4 offers many more options for i2c support than prior versions. In addition to i2c0, i2c1 and software i2c, you will find i2c3, 4, 5 & 6 (4 additional).

Finally note that it may be possible to relocate i2c1 to another pair of GPIO pins by changing the default pin configuration. I'll leave that for another day as I've not actually tried that myself as yet - and it's more difficult than simply invoking a pre-configured dtoverlay.

Seamus
  • 18,728
  • 2
  • 27
  • 57
0

The only options for I²C are pins 3,5

The "startup" function on pin 5 is implemented in the kernel and should still work (unless you use the incompatible dtoverlay=gpio-poweroff function).

See Will pulling pin 5 low will make the pi boot up again

You mention python code (which you have not listed) BUT none is needed. See https://raspberrypi.stackexchange.com/a/117017/8697

Milliways
  • 54,718
  • 26
  • 92
  • 182
0

Remember you can use software I2C which is integrated into the Linux kernel. You can implement as many additional buses as you have spare GPIO for SDA/SCL.

For details see /boot/overlays/README.

Name:   i2c-gpio
Info:   Adds support for software i2c controller on gpio pins
Load:   dtoverlay=i2c-gpio,<param>=<val>
Params: i2c_gpio_sda            GPIO used for I2C data (default "23")

        i2c_gpio_scl            GPIO used for I2C clock (default "24")

        i2c_gpio_delay_us       Clock delay in microseconds
                                (default "2" = ~100kHz)

        bus                     Set to a unique, non-zero value if wanting
                                multiple i2c-gpio busses. If set, will be used
                                as the preferred bus number (/dev/i2c-<n>). If
                                not set, the default value is 0, but the bus
                                number will be dynamically assigned - probably
                                3.

Note that you will need pull-ups to 3V3 on the GPIO you use for SDA and SCL. The internal pulls are possibly OK for testing but for regular use you probably want to fit something stronger. The external pulls hardwired to GPIO 2/3 are 1k8. The internal pulls are about 50k.

joan
  • 67,803
  • 5
  • 67
  • 102