FAQ: How can I make a software-based (bit-bang) I2C bus/Can I use any 2 GPIOs as an I2C bus?
Software-based (bit-bang) I2C bus on the GPIOs is doable!
Check out the OpenWRT documentation on this topic: https://openwrt.org/docs/techref/hardware/port.i2c#i2c_over_gpio
Setting up a Software I2C Bus
Install the required kernel modules
opkg update opkg install kmod-i2c-gpio-custom
Setup a bus:
insmod i2c-gpio-custom bus<BUS NUMBER>=<BUS NUMBER>,<SDA GPIO>,<SCL GPIO>
For example, to setup a SW I2C bus with SDA on GPIO18 and SCL on GPIO19:
insmod i2c-gpio-custom bus1=1,18,19
If all goes well, there will be a message in the kernel log:
[ 308.931002] Custom GPIO-based I2C driver version 0.1.1 [ 308.936785] i2c-gpio i2c-gpio.1: using pins 18 (SDA) and 19 (SCL)
And there will be a new I2C bus 1 device available:
root@Omega-F195:/# ls /dev/i2c-* /dev/i2c-0 /dev/i2c-1
- Check out the Omega2 pinout and GPIOs article in the documentation to select your GPIOs
- The Omega's hardware I2C bus is occupying the
bus0label, so your new sw i2c bus will need to be
- Keep in mind this is bit banging so the timing is not exact. However since I2C is synchronous it should not be a problem.
- If you're using Onion's Omega2 firmware, you'll need to be on the
latestavailable firmware to install the required kernel modules. See our documentation for more details.
Changing the SW I2C Bus Speed
If you are using SW I2C over GPIO, then when you insert the kernel module you can specify a delay value
insmod i2c-gpio-custom bus1=1,5,4,udelay
where I2C speed (kHz) = 500 / udelay
udelay can only be an integer
Keep in mind this is bit banging so the timing is not exact however since I2C is synchronous it should not be a problem.