Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

I2C

Tue Aug 18, 2015 4:39 pm

Which I2C library is the best ( these days) for using with Python?
Is there any tutorial how to use I2C ?
Thanks

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: I2C

Tue Aug 18, 2015 4:47 pm

You can use Pigpio

Laurens

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Tue Aug 18, 2015 5:15 pm

The standard solution is the Python smbus module. As Laurens-wuyts points out you can use (my) pigpio's in-built I2C support if smbus doesn't do what you need.

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Tue Aug 18, 2015 6:50 pm

Thank you for the replies

At the moment I have:
Windows (running a program there) --> USB/I2C bridge --> device I need to control via I2C bus
I know( from a reverse engineering) how the program communicates and how controls the device .
Now I would like to write my own program( preferably )in Python.

So, I think with Raspi It could be
RasPi --> device
( so Raspi would work also as USB/I2C bridge.Is it so?)

So, will be smbus module enough?
Is there any tutorial/documentation?

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Tue Aug 18, 2015 7:08 pm

I2C and SMBus is used to communicate with devices connected to one of the Pi's two I2C buses. The buses are implemented as gpios on the expansion header.

Are your devices connected to the expansion header?

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Tue Aug 18, 2015 7:29 pm

At the moment I run a program on windows that communicate ,via USB to USB/I2C bridge, with I2C bus on the device.
How should (or how can I ) connect the device to the expansion header?

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Tue Aug 18, 2015 7:53 pm

You generally connect the 3V3, ground, SDA, and SCL pins to the appropriate pins on the device with jumper wires.

There should be plenty of diagrams such as http://fritzing.org/media/fritzing-repo ... 017_bb.png

It would help if you could mention the device model or type of device.

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Wed Aug 19, 2015 10:10 am

Thank you for the reply.
Will I2C (Pigpio)work with the older model of Raspi. Mine has a date 2011,12 .
Are the SDA/SCL pins the same?

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Wed Aug 19, 2015 10:20 am

Jan1 wrote:Thank you for the reply.
Will I2C (Pigpio)work with the older model of Raspi. Mine has a date 2011,12 .
Are the SDA/SCL pins the same?
pigpio and the Python smbus module should work with all models of Pi.

There were some changes between Pi models.

Could you post the contents of /proc/cpuinfo? We only actually need the revision line. That should confirm your model.

See http://elinux.org/RPi_HardwareHistory

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Thu Aug 20, 2015 5:11 pm

Thanks for the reply.
/proc/cpuinfo is
Processor : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS : 697.95
Features : swp half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xb76
CPU revision : 7

Hardware : BCM2708
Revision : 000d
Serial : 00000000ad4e3bc3


Is it OK for Pigpio ?

Thanks

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Thu Aug 20, 2015 5:13 pm

Yes, that will be fine.

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Fri Aug 21, 2015 10:35 am

Thank you for the reply. I tried to install PIGPIO but I received an error
[email protected] ~/PIGPIO $ make install
sudo install -m 0755 -d /opt/pigpio/cgi
sudo install -m 0755 -d /usr/local/include
sudo install -m 0644 pigpio.h /usr/local/include
sudo install -m 0644 pigpiod_if.h /usr/local/include
sudo install -m 0755 -d /usr/local/lib
sudo install -m 0755 libpigpio.so /usr/local/lib
sudo install -m 0755 libpigpiod_if.so /usr/local/lib
sudo install -m 0755 -d /usr/local/bin
sudo install -m 0755 -s pig2vcd /usr/local/bin
sudo install -m 0755 -s pigpiod /usr/local/bin
sudo install -m 0755 -s pigs /usr/local/bin
sudo python2 setup.py install
sudo: python2: command not found
make: *** [install] Error 1


Can you please help?Thanks

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Fri Aug 21, 2015 10:51 am

You don't seem to have Python set up in the expected way.

May I ask what cat /etc/os-release reports?

Do an ls /usr/bin/pyth* to see which versions of Python you have installed.

Code: Select all

$ ls /usr/bin/pyth*
/usr/bin/python            /usr/bin/python2-config  /usr/bin/python3m
/usr/bin/python2           /usr/bin/python3         /usr/bin/python-config
/usr/bin/python2.7         /usr/bin/python3.4
/usr/bin/python2.7-config  /usr/bin/python3.4m
$
Then use the following command for each version of Python you might use.

sudo pythonx.y setup.py install

e.g. sudo python3.4 setup.py install

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Fri Aug 21, 2015 11:00 am

Thank you for help.

[email protected] / $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"
NAME="Raspbian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=raspbian
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"




[email protected] / $ ls /usr/bin/pyth*
/usr/bin/python /usr/bin/python3 /usr/bin/python3.2mu
/usr/bin/python2.7 /usr/bin/python3.2 /usr/bin/python3mu

I tried
[email protected] / $ sudo python3.4 setup.py install
sudo: python3.4: command not found


Do you have any idea where can be a problem?
Thanks

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Fri Aug 21, 2015 11:24 am

There is no problem as such, you just have a pretty old software version, which isn't set up like the more recent Pi distributions.

You have Python 2.7 and 3.2.

So I'd use

sudo python2.7 setup.py install

and

sudo python3.2 setup.py install

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Mon Aug 24, 2015 12:55 pm

Thank you for your reply.
So I upgraded and now it works.But can you please help me how to send the commands to get the result like this
[img]
LAPicture.jpg
LAPicture.jpg (31.79 KiB) Viewed 2489 times
[/img]

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C

Mon Aug 24, 2015 12:57 pm

I tried

pi = pigpio.pi() # Connect to local Pi.

handle = pi.i2c_open(0, 0x08, 0)
pi.i2c_write_byte_data(handle, 0x08 , 0x80)

but received an error
pi.i2c_write_byte_data(handle, 0x08 , 0x80)
File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 2237, in i2c_write_byte_data
self.sl, _PI_CMD_I2CWB, handle, reg, 4, extents))
File "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 796, in _u2i
raise error(error_text(v))
pigpio.error: 'I2C write failed'


Thank you for help

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C

Mon Aug 24, 2015 1:22 pm

Have you checked the device is seen on the bus?

i2cdetect -y 0

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: I2C

Mon Aug 24, 2015 2:02 pm

Have you removed i2c from the blacklist?

I2C setup for older versions of raspbian required you to do this manually by editing /etc/modules to enable i2c and editing the blacklist to remove the i2c blacklist /etc/modprobe.d/raspi-blacklist.conf

reading the adafruit page I linked above will help with setting up i2c for the first time on older versions of the raspbian OS.

more recent raspbian versions use Device Tree, I am less familiar with how to enable I2C using Device Tree.
Doug.
Building Management Systems Engineer.

Return to “Interfacing (DSI, CSI, I2C, etc.)”