I2C Detect Slave Method?

  • Good Day,

    I am looking at using the i2c functionality of the Onion 2+ to be able to "detect" connect i2c slaves. unfortunately my tests with both python i2c library and c library both show that the omega returns "success" when reading and writing to an i2c slave that is not even there.

    Does anyone know how to use i2c to detect if a slave is valid or not? i.e. If I try to "address" i2c slave "0x21" which is definitely not connected, how can I get some sort of error?


  • Hi,

    Just some feedback if you happen to stumble across this topic in desperation. So my analysis is that there must be a bug with the Onion 2 I2C implementation. With that said @t-gabor had a thread that described how he managed to overcome some of the i2c limitations by moving to a "bit-banged" i2c approach.

    For those interested the following method was used on a Omega Onion 2+ (firmware v0.1.10-b160):

    1. Install the relevant kernal modules (Thanks @t-gabor for compiling):

    This is done by downloading the above packages locally and running them in:
    opkg install --force-depends kmod-i2c-algo-bit_4.4.46-1_mipsel_24kc.ipk
    opkg install --force-depends kmod-i2c-gpio_4.4.46-1_mipsel_24kc.ipk
    opkg install --force-depends kmod-i2c-gpio-custom_4.4.46-2_mipsel_24kc.ipk

    the --force-depends is required as the onion thinks the kernel is at a too low version. if you are interested you can run:

    root@Omega-XXXX:~# opkg list-installed | grep kernel
    kernel - 4.4.46-1-611bddde2031bc44b7d050f62495d772

    In order to get the current kernel version, which you will find the last section of numbers are lower that the expected value for the compiled packages above.

    Anyhow what confused me for a good few hours was that the opkg install will show errors at the end even though it was successful (i.e. notice the "Installing" and "Configuring" sections):

    root@Omega-XXXX:~/ipk# opkg install --force-depends kmod-i2c-algo-bit_4.4.46-1_mipsel_24kc.ipk
    Installing kmod-i2c-algo-bit (4.4.46-1) to root...
    Configuring kmod-i2c-algo-bit.
    Collected errors:
     * satisfy_dependencies_for: Cannot satisfy the following dependencies for kmod-i2c-algo-bit:
     *      kernel (= 4.4.46-1-d0a487fc7d1c9f083b7de0fb8c2a7c07) *

    Once these modules are installed we need to load them using the following commands:

    omega2-ctrl gpiomux set i2c gpio
    insmod i2c-algo-bit
    insmod i2c-gpio
    insmod i2c-gpio-custom bus0=1,5,4

    with that you can now try the newly created /dev/i2c-1 device:

    i2cdetect -y 1

    And vola:

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

    Anyhow to wrap things up you can do this setup automatically at bootup with the following init.d script. create using vi in /etc/init.d/I2Cbb

    #!/bin/sh /etc/rc.common
    # Creates bit-banged i2c-device /dev/i2c-1 on pins 5,4.
    # NOTE: This hides /dev/i2c-0!
    start() {
            omega2-ctrl gpiomux set i2c gpio
            insmod i2c-algo-bit
            insmod i2c-gpio
            insmod i2c-gpio-custom bus0=1,5,4
    stop() {
            rmmod i2c-gpio-custom
            rmmod i2c-gpio
            rmmod i2c-algo-bit
            omega2-ctrl gpiomux set i2c i2c

    And another hour or so of frustration and rebooting reminded me again of missing the basics, do not forget to enable and make executable the script!

    chmod 755 /etc/init.d/I2Cbb
    /etc/init.d/I2Cbb enable

    *** EDIT ***
    On an final note, using bitbanged I2C allows you to set the Onion I2C bus clock speed (SCL). To do this add the extra parameter to the the init.d script

    insmod i2c-gpio-custom bus0=1,5,4,x

    where x is is number based on the following formula:

    I2C speed (kHz) = 500 / x 


  • UFD, congratulations and thank you for sharing.

  • @UFD Thanks for this "solution" (or more workaround). This was the only way I was able to get my Python code towards a keller pressure meter work. So I'm lifting this fabioulus post

    I'm still unable to fix my related problem with write a byte wait 8ms then read 5 bytes without specifying register.

    there seems also to still be a open drain issue

    Why hasn't this been fixed in Omega2+? I have scanned several posts in the forum that probably has problems that are related to this. I2C is a vital bus doing smart IoT stuff for real. So if Onion would like to be part of commercial projects I2C needs to have top priority.
    Could any of the staff comment?

  • In case any body else is looking for this.

    If for example you wanted two I2C busses, you would do that like this.

    Example: using GPIO 14 for the second SDA and GPIO 16 for the 2nd SCL

    insmod i2c-gpio-custom bus0=1,5,4 bus1=2,14,16

    and you can then verify them with

    i2cdetect -y 1


    i2cdetect -y 2

Log in to reply

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.