1 2 Previous Next 17 Replies Latest reply on Dec 31, 2014 8:19 AM by rgb

    i2c-tool pgk to be able to use SMBUS in python


      Hello community,

           I. trying to use smbus to access i2c in python (as there are many examples out there), however the i2c-tool package is not available in AlexT repository.

           Any workaroud in python?



        • 1. Re: i2c-tool pgk to be able to use SMBUS in python

          If you have the required dev tools installed on the Galileo, you can compile the i2c tools from the source on the Galileo.

          But before compile&install you have to patch a single line in py-smbus/smbusmodule.c. There is a constant named I2C_SLAVE but the constant I2C_SLAVE_FORCE is required.

          After doing this, I was able to use the SMBus extension in Python without any problems.

          • 2. Re: i2c-tool pgk to be able to use SMBUS in python

            Hi AlexanderMerz,

            I do have the latest dev tools working in my SD card image with the BSP 1.0.1 posted by AlexT(about 2GB). Everything is working fine, however in python, it does not recognize "import smbus".

            To avoid mistake, would you please elaborate more on how you were able to use it?

            I would appreciate if you can post your code example so the community can take the most of this blog.




            • 3. Re: i2c-tool pgk to be able to use SMBUS in python

              The source code for the i2c tools is avaible here:



              Download the sourcecode or get them via git. See the right column of the Github page for the avaible options.

              After the source code is on the Galileo, go into the directory where the source code is.


              By checking the source today, I saw the line to patch ist now 161, change the constant I2C_SLAVE to I2C_SLAVE_FORCE with a text editor on the Galileo.


              On the commandline call:


              make EXTRA="py-smbus"


              This will create the i2c-tools, then install them:


              make install


              The tools like i2cdetect and i2cget should be work now.


              Then go into the py-symbus directory and call:


              python setup.py install


              Now you should be able to excute a python script like this:

              from smbus import SMBus

              bus = SMBus(0)

              bus.write_byte_data(0x20, 0x29, 0x04)

              The first parameter in write_byte_data adresses the IOExpander chip on the Galileo (the chip hast the adress 0x20 by default), the second parameter refers to the register to set and the third ist value to set.

              1 of 1 people found this helpful
              • 4. Re: i2c-tool pgk to be able to use SMBUS in python

                Hi AlexanderMerz

                     I'm almost done. I'm able to use i2cdetect and i2cget as per your post.

                     I got an error when running the setup.py. Here is my update:


                root@clanton:/media/mmcblk0p1/i2c-tools-master/py-smbus# python setup.py install

                running install

                running build

                running build_ext

                building 'smbus' extension

                i586-poky-linux-uclibc-gcc -m32 -march=i586 -fno-strict-aliasing -O2 -pipe -g -feliminate-unused-debug-types -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c smbusmodule.c -o build/temp.linux-i586-2.7/smbusmodule.o

                smbusmodule.c:19:20: fatal error: Python.h: No such file or directory

                compilation terminated.

                error: command 'i586-poky-linux-uclibc-gcc' failed with exit status 1


                For your reference, this is my Python version in the Galileo

                Python 2.7.3 (default, May 24 2014, 23:06:31)

                [GCC 4.7.2] on linux2


                let me know your recommendations.



                • 5. Re: i2c-tool pgk to be able to use SMBUS in python

                  Update on Python.h error

                    I realized I didn't have the python-dev package in my SD. I followed the instructions posted by AlexT : http://alextgalileo.altervista.org/package-repo-configuration-instructions.html

                    and performed the following command:  # opkg install python-dev


                  I succesfully performed the instructions. Trying to import smbus in Python, I have the following error:

                  >>> import smbus

                  /usr/bin/python2.7: symbol 'i2c_smbus_access': can't resolve symbol in lib '/usr/lib/python2.7/site-packages/smbus.so'.

                  Traceback (most recent call last):

                    File "<stdin>", line 1, in <module>

                  ImportError: unknown dlopen() error


                  let me know your thoughts.



                  • 6. Re: i2c-tool pgk to be able to use SMBUS in python

                    If the build process of the python module was really succesful, then there should be a smbus.so in the py-smbus directory. Now two things might have gone wrong:

                    Copying of the file, because the destination directory did not exists or missing user rights for the operation

                    Accessing the file from python, because the file is missing or missing user rights for the operation


                    Check the python manual, where python is looking for the module: 6. Modules — Python v2.7.8 documentation

                    According to the error message, python expects the file in /usr/lib/python2.7/site-packages/ - check if the directory exists and the file exists. If the directory exists but not the file, copy the smbus.so file into this directory.


                    If python still fails, use the commandline command "ls -als" in the python module directory to take a look at the owner and its access rights of smbus.so, if the owner or rights differs from the other files in directory, change the owner and/or rights to the match the other files using chown and chmod.

                    1 of 1 people found this helpful
                    • 7. Re: i2c-tool pgk to be able to use SMBUS in python

                      Thanks a lot AlexanderMerz.

                      I didn't reply before as I was doing some testing. I was able to write, however not able yet to use the read_byte or read_byte_data, I wonder if Galileo has the capability as I was also not able to do that with the IDE neither. ( I get a -1).

                      Interesting fact is that having the IDE sending a string with the IDE, I checked the gpio29/value and it was 1. I set it to 0 and worked as well.


                      For anyone who is testing this out, here is what I found out so far. After re- installing, I was able to get send data thru python to a Arduino device.

                      In the Arduino, just use any example using the wire library (e.g: slave_receiver) , using Wire.begin(4).. to access i2c address 4. 

                      in Galileo, accessing python:

                      >>from smbus import SMBus

                      >>bus = SMBus(0)


                      >>bus_write_byte(4, 5) 

                      # you'll se a 5 printed in the Arduino.



                      >>bus.write_word(4, 2, 6) 


                      you'll se a 6 printed in the Arduino.



                      Alex, I also noticed that

                      bus.write_byte_data(0x20, 0x29, 0x04)

                      did not work. I was able to get something in the Arduino with bus.write_byte_data(4, 0x29, 0x04), in this case I got a 4 to the other end, and does not matter if I change the 0x29... to a 0x30, it sends the 4.


                      Let me know if you were able to read.




                      • 8. Re: i2c-tool pgk to be able to use SMBUS in python

                        If it would not work, then your Galileo would not work properly


                        Some simplified I2C basics: The I2C is a bus system with a master-slave characteristic, where each slave must have an unique address. So the master can directly point messages to a specific slave.


                        You configured your Arduino as slave through the call of Write.beginn(4). The parameter in the function call tells the Arduino/the Wire library which address the slave has. Now the Arduino is listening for incoming messages from the master - but only for messages addressed to slave address 4. Messages to other slaves will be ignored and not passed to your sketch via onReceive() or onRequest().


                        If you call bus.write_byte_data(0x20, 0x29, 0x04), you send a message to the slave with the address 0x20. 0x20 is not 4, so the Arduino is ignoring the message. (Compare this to your real world mailman, he wouldn’t give you the mail of your neighbor even if your house numbers are close and you live at the same street )


                        Try another test:

                        Login on your Galileo with the Arduino attached as I2C slave and call

                        i2cdetect -y -f 0

                        on the commandline (The -f parameter may be not required).

                        This tool ist part of the i2c tools and show you all slaves of the first available I2C bus.


                        You will see a lot of strokes and at least two numbers: 04 and 20.


                        Now remove the Arduino and call i2cdetect again. You will see again a lot of strokes, but only the number 20.


                        The 4 in the first call, was your Arduino. But what about the device with the slave address 20 which is still present?


                        This is the IO Expander chip on the Galileo board, a data sheet of the chip can be found here:


                        The chip does all the magic to make the Galileo to act like a Arduino, it controls the behavior of the GPIO pins and the PWM functionality. The chip is connected through the I2C bus like your Arduino and can be configured by I2C messages.

                        1 of 1 people found this helpful
                        • 9. Re: i2c-tool pgk to be able to use SMBUS in python



                          I'm having a similar problem than you seemed to solve. The i2c-tools project builds (and installs) without complaints, but I'm getting an error while importing smbus on python:


                          root@clanton:~/i2c-tools# python
                          Python 2.7.3 (default, May 23 2014, 23:51:57)
                          [GCC 4.7.2] on linux2
                          Type "help", "copyright", "credits" or "license" for more information.
                          >>> import smbus
                          /usr/bin/python2.7: symbol 'i2c_smbus_access': can't resolve symbol in lib '/usr/lib/python2.7/site-packages/smbus.so'.


                          The smbus.so library is placed in the right directory:


                          root@clanton:/usr/local/lib# ls -la /usr/lib/python2.7/site-packages
                          total 1460
                          drwxr-sr-x  4 root root    1024 Sep 15 04:16 .
                          drwxr-sr-x 27 root root    9216 Sep 15 02:03 ..
                          -rw-r--r--  1 root root     119 May 23 21:52 README
                          -rw-r--r--  1 root root      21 Apr 11 10:15 cv.py
                          -rwxr-xr-x  1 root root 1412532 Jul  6 13:01 cv2.so
                          drwxr-sr-x  5 root root    1024 May 23 23:49 nose-1.2.1-py2.7.egg
                          drwxr-sr-x 18 root root    1024 May 23 23:49 numpy
                          -rw-r--r--  1 root root    1630 May 23 22:23 numpy-1.7.0-py2.7.egg-info
                          -rw-r--r--  1 root root    2362 May 23 22:51 site.py
                          -rw-r--r--  1 root root    1937 May 23 22:51 site.pyo
                          -rw-r--r--  1 root root     263 Sep 15 04:16 smbus-1.1-py2.7.egg-info
                          -rwxr-xr-x  1 root root   50299 Sep 15 02:58 smbus.so


                          But the problem seems much bigger, though:


                          root@clanton:~/i2c-tools# i2cdetect -y -f 0
                          /usr/local/sbin/i2cdetect: can't load library 'libi2c.so.0'


                          If I just copy the libi2c.so.0 library into /usr/lib then i2cdetect works fine, but not python smbus.


                          To build i2c-tools I used AlexT's image and installed several tools:


                          opkg install make gcc-dev gcc-symlinks cpp cpp-symlinks g++ g++-symlinks uclibc-dev uclibc-utils binutils-dev binutils-symlinks python-dev coreutils-dev


                          And the libraries' permissions seem to be correct, I even included '/usr/local/lib' into my PATH to try to make things work:


                          root@clanton:~/i2c-tools# ls -la /usr/local/lib/       
                          total 16
                          drwxr-sr-x 2 root root 1024 Sep 15 03:37 .
                          drwxr-sr-x 7 root root 1024 Sep 15 02:12 ..
                          -rw-r--r-- 1 root root 4076 Sep 15 03:37 libi2c.a
                          lrwxrwxrwx 1 root root   11 Sep 15 03:37 libi2c.so -> libi2c.so.0
                          lrwxrwxrwx 1 root root   15 Sep 15 03:37 libi2c.so.0 -> libi2c.so.0.1.0
                          -rwxr-xr-x 1 root root 9496 Sep 15 03:37 libi2c.so.0.1.0
                          root@clanton:~/i2c-tools# echo $PATH
                          root@clanton:/usr/local/lib# echo $LD_LIBRARY_PATH


                          Any help would be greatly appreciated. Thanks in advance!

                          • 10. Re: i2c-tool pgk to be able to use SMBUS in python

                            You might be also interested giving the IoT devkit (Beta) https://software.intel.com/en-us/iot/getting-started a try.

                            It comes with "libmraa" intel-iot-devkit/mraa · GitHub inclusive python bindings abstracing away GPIO / i2c complexities.

                            • 11. Re: i2c-tool pgk to be able to use SMBUS in python

                              Hi - problem solved

                              I had the same problem as Keiner, and am using AlexT's version of Yocto

                              I found a newer version at:  http://www.lm-sensors.org/wiki/I2CTools
                              removed old: make uninstall

                              Changed line in py-smbus/smbusmodule.c. line 161, from I2C_SLAVE to I2C_SLAVE_FORCE

                              did a new installation following your instructions

                              when ran i2cdetect it didn't see the i2c LCD backpack didn't doesn't show up at 27,  but when I run your test program,

                              from smbus import SMBus
                              bus = SMBus(0)
                              bus.write_byte_data(0x20, 0x29, 0x04)

                              I get nothing back.

                              I loaded a sketch to access the i2c LED Backpack and i2cdetect now shows the address, 27

                              Seems all is working, at least if I use the Arduino library to get i2c running.

                              Now need to find a way to get it to work from Python




                              I got I2C and Python working with Galileo Gen 1 with I2C LCD & Grove LCDand posted the information at Python, Wiring_x86, I2C LCD, Grove RGB LCD

                              hope it helps someone


                              • 12. Re: i2c-tool pgk to be able to use SMBUS in python

                                I have the same problem, but I'm on the Edison board.


                                And when i try to do on the python console: from smbus import SMBus, I have

                                ImportError: /usr/lib/python2.7/site-packages/smbus.so: undefined symbol: i2c_smbus_process_call

                                If anyone can help, thx

                                • 13. Re: i2c-tool pgk to be able to use SMBUS in python


                                  Since none of the people with experience with Edison answered I'll throw in my 2 cents. First you might try the Edison maker community, as they have a lot of postings on problems with I2C. Second, assuming, you followed the above instructions, did you change the bus to 6? If I remember, the Edison uses bus 6 not 0.



                                  • 14. Re: i2c-tool pgk to be able to use SMBUS in python

                                    I changed the setup.py end by


                                                    "smbus", ["smbusmodule.c"],

                                                    libraries = ['i2c'],

                                                    include_dirs = ['../include'])])


                                    I cleaned and uninstall everything, installed again, and it worked. Thanks !

                                    1 2 Previous Next