3

I use i2c-detect to find connected i2c devices on my Raspberry. For example:

enter image description here

Now I want to use that information to perform some tasks, for example:

if “Address 76 is connected”:

DO STUFF

For this, I need a numeric output from i2c-detect, like "76", as in the output table shown above. How should I proceed?

3
  • 1
    I do not understand the question. If English is not your first language try Google Translate.
    – joan
    Oct 28, 2021 at 16:50
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Oct 28, 2021 at 17:07
  • 1
    you could write your own program ... examine the source code of i2c-detect
    – jsotola
    Oct 28, 2021 at 19:09

3 Answers 3

2

You should be able to test a known address with i2cget:

if i2cget -y 1 0x76
then
    echo "device 0x76 is there"
else
    echo "device 0x76 is missing"
fi

Note that there is no universal detection method on I2C. i2cdetect uses different methods for different addresses, unless you tell it which method to use. i2cget is the equivalent of i2cdetect -r. This may confuse write-only chips.

1

If you want to use the i2c-tools package, you should start by reading the available documentation. Two items I'd recommend are:

  1. The summary & system manuals for i2c-tools provides a useful overview, and has links to the I2C Wiki, and the git source tree.

  2. The 4-part I2C programming tutorial from AB Electronics(https://www.abelectronics.co.uk/kb/article/1090/i2c-part-1---introducing-i2c)

After you finish this, you may have a different view toward the toolset to use (e.g. Python's smbus2 library) in your project, or you may decide that the i2c-tools package is a good fit. In any case, some familiarity with the options will be a benefit.

Wrt using the i2c-tools package for your development, I think that there are options available (e.g. the MATLAB version) that you may wish to consider. Also, perhaps consider your approach in light of your programming experience; for example, extracting the device address you need from the i2c-detect output will demand some additional programming to parse the output; do you have any experience parsing text with sed or awk?

So - that's one way to proceed...

0

Here is some Python which tests for devices on Pi I2C buses 0 and 1. It needs the pigpio daemon to be running (sudo pigpiod).

#!/usr/bin/env python

import pigpio

pigpio.exceptions = False # handle errors

pi = pigpio.pi()

for bus in range(2):
   for x in range(0x08, 0x79):
      h = pi.i2c_open(bus, x)
      if h >= 0:
         s = pi.i2c_read_byte(h)
         if s >= 0:
            print("device {} found on bus {}".format(x, bus))
         pi.i2c_close(h)

pi.stop()

Look at the i2cdetect documentation (man i2cdetect) for why this sort of test can be risky.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.