RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Programming an i2C interface in Python

Mon Feb 06, 2017 10:54 pm

I have a product named 'Bright Pi' that consists of a small board with 8 IR LED's and 4 white LED's that mounts in front of the Pi Camera. It has an i2C interface and I have got it working perfectly using sudo commands from the terminal.

I now want to incorporate it into a Python program but I have been unable to find a suitable Python Program.

I have the following terminal commands:
sudo i2cset -y 1 0x70 0x00 0x5a (This command turns on the white LED's)
sudo i2cset -y 1 0x70 0x00 0xa5 (This command turns on the IR LED's)
sudo i2cset -y 1 0x70 0x00 0xff (Turns on all the LED's)
sudo i2cset -y 1 0x70 0x02 0x32 (Turns up the brightness of one white LED - the others are similar part from 1 character)
sudo i2cset -y 1 0x70 0x00 0x00 (Turns off all the LED's)

Is there a way of incorporating the above commands in a Python 3 program?
Thank you.

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

Re: Programming an i2C interface in Python

Mon Feb 06, 2017 11:10 pm

Typically you would use the Python smbus module for I2C. It should be able to send those commands.

As an alternative you could use my pigpio Python module which has in-built support for I2C.

RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Programming an i2C interface in Python

Tue Feb 07, 2017 8:31 am

Thank you joan, I will look at those, probably with preference towards your pigpio Python Module as I am fairly regular user of your Piscope program.

User avatar
bensimmo
Posts: 4177
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Programming an i2C interface in Python

Tue Feb 07, 2017 9:08 am

I think you need to use smbus2 with python3

At least that's what I remember altering all the python2 code with to make it work with python3.

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

Re: Programming an i2C interface in Python

Tue Feb 07, 2017 9:38 am

If you use the smbus module the most appropriate call may be write_byte_data(reg, data). The 0x70 part is the address and will automatically be sent. Put the first parameter in reg and the second parameter in data.

E.g. for sudo i2cset -y 1 0x70 0x02 0x32 try write_byte_data(0x02, 0x32).

For pigpio I would use i2c_write_device(handle, [0x02, 0x32]).

RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Programming an i2C interface in Python

Tue Feb 07, 2017 5:17 pm

Sorry, to be a nuisance but I cannot get the simplest of parts to work.
I decided to start with the smbus option.

when I first tried the 'write_byte_data' command, it reported that 'write_byte_data' is not defined, so I added the
'bus = smbus.SMBus(1)' and changed 'write_byte_data' to 'bus.write_byte_data'.
then it informed me that
write_byte_data() takes exactly 3 arguments (2 given), so I added the address (0x70) as the first parameter (I only guessed that it needed 0x70).

The Python3 program now runs without error but the lights do not come on.
My final program is shown below, which I had hoped would put the lights on for 10 seconds and then turn them off:

Code: Select all

import time
import smbus as smbus
bus = smbus.SMBus(1)
bus.write_byte_data(0x70, 0x02, 0x5a)
time.sleep(10)
bus.write_byte_data(0x70, 0x02, 0x00)
I would appreciate more help.

User avatar
bensimmo
Posts: 4177
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Programming an i2C interface in Python

Tue Feb 07, 2017 6:42 pm

Looking at your I2C commands, shouldn't you use
0x70, 0x00, 0xff to turn them all on
And the corresponding one to turn them all off.

Yes the first should be the I2C address.

RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Programming an i2C interface in Python

Tue Feb 07, 2017 6:47 pm

@bensimmo
Yes, that is the command to turn them ALL on but I am just trying to turn the White ones on using 0x5a and this command works perfectly from the Terminal. The 0xff doesn't work either from the Python script but it is fine from the Terminal.

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: Programming an i2C interface in Python

Tue Feb 07, 2017 7:32 pm

RDS wrote: write_byte_data() takes exactly 3 arguments (2 given), so I added the address (0x70) as the first parameter (I only guessed that it needed 0x70).
Instead of guessing, having a look at the documentation might be a more promising approach...

RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Programming an i2C interface in Python

Wed Feb 08, 2017 2:27 pm

@dasmanul
I am confident and as confirmed by bensimmo above, that the address is in the correct position but I still cannot get it working.

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

Re: Programming an i2C interface in Python

Wed Feb 08, 2017 3:16 pm

You could try the following from the command line.

sudo pigpiod # start the daemon

pigs i2co 1 0x70 0 # open I2C device on bus 1 address 0x70

pigs i2cwd 0 0x00 0x5a # turn on the white LEDs
pigs i2cwd 0 0x00 0xff # turn on all the LEDs
pigs i2cwd 0 0x00 0x00 # turn off all the LEDs

pigs i2cc 0 # release handle when finished

The first 0 is the handle returned by i2co.

User avatar
bensimmo
Posts: 4177
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Programming an i2C interface in Python

Wed Feb 08, 2017 3:45 pm

I just noticed you mentioned BrightPi.
Have a look at BrightPi.codeplex.com and see some code in use.

Edit and a newer python3 update to it search brightpi-python3 on github.

RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Programming an i2C interface in Python

Wed Feb 08, 2017 4:25 pm

joan wrote:You could try the following from the command line.
Thank you joan but I can get working perfectly from the command line.
I need to be able to get it working in Python 3 so that I can incorporate the code in my existing Python Program.

RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Programming an i2C interface in Python

Wed Feb 08, 2017 4:36 pm

@bensimmo
Thank you but none of those links provide working Python Code. All programs appear to be incomplete.

The actual BrightPi website and contact details seem to be obsolete. The BrightPi website states that Python code is coming soon and that was Sept 2014. The website no longer seems to accept new posts - it is as if there is no-one there anymore.

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

Re: Programming an i2C interface in Python

Wed Feb 08, 2017 5:18 pm

RDS wrote:
joan wrote:You could try the following from the command line.
Thank you joan but I can get working perfectly from the command line.
I need to be able to get it working in Python 3 so that I can incorporate the code in my existing Python Program.
There is a direct mapping from those commands to Python commands. If it works from the command line it will work the same in Python.

Using pigs would also let you interleave the SMBus commands to see which if any would work using the smbus module.
  • I2CWB h r bv smb Write Byte Data: write byte to register i2cWriteByteData
  • I2CWD h bvs i2c Write data i2cWriteDevice
  • I2CWI h r bvs smb Write I2C Block Data i2cWriteI2CBlockData
  • I2CWK h r bvs smb Write Block Data: write data to register i2cWriteBlockData
  • I2CWQ h bit smb Write Quick: write bit i2cWriteQuick
  • I2CWS h bv smb Write Byte: write byte i2cWriteByte
  • I2CWW h r wv smb Write Word Data: write word to register i2cWriteWordData

User avatar
bensimmo
Posts: 4177
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Programming an i2C interface in Python

Thu Feb 09, 2017 7:47 am

RDS wrote:@bensimmo
Thank you but none of those links provide working Python Code. All programs appear to be incomplete.

The actual BrightPi website and contact details seem to be obsolete. The BrightPi website states that Python code is coming soon and that was Sept 2014. The website no longer seems to accept new posts - it is as if there is no-one there anymore.
Did you run the test program, importing the module that was made.
They should work as that is the whole point of it being there.
The module sits doing the hard it part in the background (or you modify it/look at how they do it) and you use the interface to do the easy part.
If it doesn't work, then likely something is wrong your end. Best bet is to ask the Python3 Github creator for some help. Since they've been there, done that.
Are you running from IDLE3, Geany, the command line ?
Is python3-smbus installed (since it seems you don't need smbus2 any more)
Does it work from python2?

:-)

RDS
Posts: 718
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Programming an i2C interface in Python

Thu Feb 09, 2017 9:45 am

bensimmo wrote:
RDS wrote:.Did you run the test program, importing the module that was made.

Thank you, yes, I finally got it working late last night on smbus. I had those 2 .py programs all along but I just thought that they were incomplete - I had not realised that they worked together, until I saw a question from someone else, asking the same thing. I will adapt them now, to just include the parts I require.

I have still not got it working with pigpio, although I am down to just 1 error now. I will persist with that though.

Return to “Python”