I2C support in Elk PiHat + Raspberry Pi


In my project I want to connect an Arduino-type SBC to my Elk system. I will not be using a pre-programmed device such as an OLED display, so I have a double challenge of programming the source and destination correctly. Triple challenge if you include getting the wiring correct :wink:

  1. Wiring

I had been planning to use SPI for this, until I found out that the SPI was in use by Elk already, per this thread:

I shifted to looking at I2C. The RPi has 2 sets of I2C pins. If I understand correctly, Device 0 is connected to the built in SD card, and Device 1 is available for use. Also, if I understand correctly, the correct pins (on the RPi) for Device 1 are GPIO2 and GPIO3. (GPIO0 and GPIO1 are used by Device 0). There are also pins on the RPi dedicated to hats, which may(?) be sharing Device 0?

There are pins labelled SDA0 and SCL0 on the PiHat board. That implies Device 0, which is already in use?

I’ve tried to figure out from the Elk PiHat schematics which set of I2C pins are brought out to the PiHat edge connectors for my use. Sheet 1/15 shows SDA0 and SCL0 from the Pi connected to a “Codec” module. It’s not clear to me whether those are really Device 0 or Device 1 from the RPi. Because Pins 27 & 28 are labelled SDA1 and SCL1, and shown as connected to the GPIOs module.

Clearly, there is much to learn here, or I have not found the correct documentation. I would like to use the I2C on my Elk/RPi as the primary device, and another SBC as a secondary device.

Can you provide a reference?

  1. Software

Which C++ software library/API do you recommend using on the RPi to control the I2C?


  • Rick

Hi Rick,
we used a similar display in our “Blackboard” expansion for the Elk Pi.
You can check the HW schematics for the project as a reference on how to connect an I2C display here:

There we controlled it using Python’s Luma library, check the example here:

I don’t have personal experience with C++ libraries for I2C displays, I just did a quick google search that returned some Arduino-style driver libraries for the same chipsets supported by luma so those are probably a good starting point but I don’t know how good they are for production.

As in the blackboard, you can use I2C device 0 since I2C device 1 is connected to the audio codec.

Hi @Stefano,

Thanks for the reply.

As I said above, I will not be controlling a display. I will check the the Blackboard schematics to see about the wiring. The information about using Device 0 is very important. I guess it is OK, even though the Pi’s boot sd card is also connected there?

I have already been looking at Raspberry Pi C++ libraries for I2C. But if you were already using one, then of course that would be my first choice. So thank you for the information about that too.

  • Rick

For future developers, the story so far and a summary at the end:

Part 1: Raspberry Pi

On a Raspberry Pi pinout, pins 27 and 28 are called GPIO0 (ID_SD) and GPIO1 (ID_SC). Pins 3 and 5 are called GPIO2 (SDA) and GPIO3 (SCL). It is recommended on this page to use GPIO0 and GPIO1 for EEPROM data (presumably IDs for the hat).


This page was also handy:

This page implies that I2C1 (port 1, pins 3 and 5) is where most RPi developers would connect their I2C devices, and that I2C0 (port 0, pins 27 and 28) is for EEPROM data.

The Raspberry Pi 4 schematic is here:

This note is in the schematic next to the GPIO expansion:

ID_SD and ID_SC pins:

These pins are reserved for HAT ID EEPROM

At boot time this I2C interface will be interrogated to look for an EEPROM that identifies the attached board and allows automagic setup of the GPIOs (and optionally, Linux drivers)

DO NOT USE these pins for anything other than attaching an I2C ID EEPROM. Leave unconnected if ID EEPROM not required.

That note makes me a bit nervous.

I realize that I’ve stated the same things multiple times above. Sorry about that, and please bear with me.

Part 2: Elk PiHat

Per the Elk PiHat Rev C schematics sheet 1/15, SDA0 and SCL0 of the RPi are connected to the Codec. Pins 27 and 28 (labeled SDA1 and SCL1) are shown as connected to the GPIOS. On sheet 10/15, these are shown as connected to pins 27 and 29 of the Digital Inputs connector on the PiHat.

Part 3: Blackboard and display

Per the Blackboard schematics sheet 1/1, the Blackboard’s display is connected to J28 pins 27 and 29 (labelled SDA0 and SCL0).

Raspberry Pi I2C seems to have 2 ports (port 0 and port 1). The usual port for connecting devices to the RPi seems to be port 1 for all recent RPis. Pins 27/28 make Port 0 available to developers. Their own schematic warns against using Port 0.

If I read this correctly, RPi pins 27 and 28 (Port 0) are connected to the Blackboard’s OLED display.

If I read this correctly, then using this command at the ElkOS command line:

sudo i2cdetect -y 1

should be detecting the codec on the PiHat.

Using this command at the ElkOS command line:

sudo i2cdetect -y 0

should be detecting an I2C device connected to SCL0/SDA0 on the PiHat. Perhaps if you have a Blackboard this would detect the display.

In summary, this would confirm what Stefano states in a previous reply. Rpi I2C1 (Port 1) is connected to the PiHat’s codec. Use I2C0 (Port 0) for other connections. These pins are labelled SDA0 and SCL0 on the PiHat board. The labeling in the PiHat schematics contradicts the labeling on the PiHat PCB.

If you’ve made it this far, thanks!

Please let me know if this post is correct.

  • Rick