Write "some stuff" to I2C


8 posts
by HeavensBlade » Tue Mar 05, 2013 12:06 pm
Hey,
today I got a new I2C slave. Its a board controlling a stepper motor.
I also ordered a USB-I2C interface to try it on my win pc. The software included gives me the I2C commands send when a specific action is performed like this:
Image
How can I send code like this from the Pi with python?
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am
by techpaul » Tue Mar 05, 2013 12:17 pm
Look at resources like these for python-smbus or Quick2wire or other Linux I2C libraries for talking to I2C driver.
http://www.acmesystems.it/i2c
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/i2c/smbus-protocol
Here is a simple I2C example
Code: Select all
#!/usr/bin/env python
#
# Basic example of using Python-SMBus and a PCF8574
# Use at your own risk.
#
#  1 Write 0x55AA to device
#  2 Check for I/O error (no device connected)
#  3 Read the device
#  4 Display result
#
# Assumes PCF8575 at I2C address 0x20 (see i2c_addr)
#   Nothing connected to PCF8575 that will cause problems
#
#   This version requires Python V2.6+
#      Not Python 3
#
# PC Services PCF8575/Python-smbus example
# November 2012

# define I2C address of PCF8575 (0x20 to 0x27 are valid)
i2c_addr = 0x20

# import libraries
import smbus as smbus

#configure I2C bus for functions
i2c = smbus.SMBus(0)

# value to send
temp = 0x55AA

# Set PCF8575 outputs
try :
   print 'PCF8575 at address 0x{0:02x} WRITE 0x{1:04x}'.format( i2c_addr, temp )
   i2c.write_byte_data( i2c_addr, temp & 0xff, ( temp & 0xff ) >> 8 )
except IOError :
   print 'PCF8575 Device not found at I2C address 0x{1:02x}'.format( i2c_addr )
   error = 1
else :
   # Now read from PCF8575
   temp = i2c.read_word_data( i2c_addr, 0 )
   print 'PCF8575 at address 0x{0:02x} READ 0x{1:04x}'.format( i2c_addr, temp )
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1482
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by HeavensBlade » Tue Mar 05, 2013 12:57 pm
I'm sorry but I'm new to the hole programming and interfacing thing and I don't get what those sites can do for me.
Isn't there a commandline tool that I can use with like "sC0 86 p" or "sC0 8B FF FF 7fff p" as arguments?
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am
by techpaul » Tue Mar 05, 2013 2:27 pm
If there is I dont know of one to do that level in Linux.

Have a look at i2c tools in conjuction with the documents to see what can be done.

In particular i2cset command

You did originally ask about how to do this in python not command line, hence my original response.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1482
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by HeavensBlade » Tue Mar 05, 2013 3:17 pm
Yeah, you're right.
I want to do this via a python script but I could use
Code: Select all
from subprocess import call
call(["i2cset..."])

to do that, even it's not that nice as when I do it with python smbus.

I got it running with
Code: Select all
import smbus
b=smbus.SMBus(1)
b.write_byte(0x61, 0x86)


But what can I do if I don't need to write "86" but "8B FF FF 0000" ?
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am
by techpaul » Tue Mar 05, 2013 4:23 pm
If you look at the pages I suggested you need to use
Code: Select all
write_i2c_block_data(addr,cmd,vals)
Where
  • addr = i2c address
  • cmd is first byte to send
  • vals is an array of the exact numbers of bytes to be sent for the rest of the data
e.g.
Code: Select all
write_i2c_block_data( 0x61, 0x8B , (0xFF, 0xFF, 00, 00 ) )
Check it but I think that is right
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1482
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by HeavensBlade » Tue Mar 05, 2013 5:06 pm
Thank you very much!
Your code almost did it:
Code: Select all
write_i2c_block_data( 0x61, 0x8B , [0xFF, 0xFF, 0x00, 0x00 ] )

is correct.

I have tried
Code: Select all
write_block_data
and didn't realise there was the i2c part missing.
Posts: 26
Joined: Fri Aug 03, 2012 10:45 am
by techpaul » Tue Mar 05, 2013 5:09 pm
Glad you are working, have fun messing around with it.

My code was quick and oiff the top of my head.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1482
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK