{"id":130,"date":"2013-03-09T18:44:34","date_gmt":"2013-03-09T23:44:34","guid":{"rendered":"http:\/\/www.the-greathouses.net\/blog\/?p=130"},"modified":"2013-12-30T10:23:26","modified_gmt":"2013-12-30T15:23:26","slug":"pcduino-i2c-test","status":"publish","type":"post","link":"https:\/\/www.the-greathouses.net\/blog\/2013\/03\/pcduino-i2c-test\/","title":{"rendered":"pcDuino &#8211; I2C Test"},"content":{"rendered":"<p>Today I connected up a my prototype board for an I2C experiment with the pcDuino. Blinky Lights!<br \/>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/8VGFSRm7rQE?rel=0\" frameborder=\"0\" allowfullscreen=\"\"><\/iframe><\/p>\n<p>YouTube video of simple I2C parallel port test using Python.<!--more--><\/p>\n<h6>Using I2C<\/h6>\n<p>If you want to try interfacing with the pcDuino I2C yourself, you will need to install the software for using the I2C bus:<\/p>\n<pre>sudo apt-get install i2c-tools\r\nsudo apt-get install python-smbus<\/pre>\n<p><i><b>i2c-tools<\/b><\/i> provides command line applications for checking and accessing i2c bus devices.<\/p>\n<p style=\"padding-left: 30px;\"><b>i2cdetect<\/b> &#8211; scans an I2C bus for devices<br \/>\n<b>i2cget<\/b> &#8211; reads registers visible through the I2C bus<br \/>\n<b>i2cset<\/b> &#8211; writes registers visible through the I2C bus<\/p>\n<p><i><b>python-smbus<\/b><\/i> provides Python access to I2C (SMBus) devices.<\/p>\n<p><h6>Connecting PCF8574<\/h6>\n<p>Connect a PCF8574 I2C parallel port chip to the pcDuino I2C bus. For feedback, connect each of the outputs through resistors to LEDs. Be sure to connect power to 3.3 Vdc and GND. I connected the address select pins to ground (address 0x20). I can post a detailed wiring diagram if anyone is interested.<\/p>\n<p>To verify your connections, use <i><b>i2cdetect<\/b><\/i> from the command line. It should show a single device at address 0x20 on bus 2 (the I2C bus available on the GPIO connectors).<\/p>\n<pre class=\"toolbar-overlay:false toolbar-hide:false toolbar-delay:false show-title:false show-lang:2 striped:false nums:false nums-toggle:false wrap-toggle:false show-plain:3 plain-toggle:false copy:false lang:default decode:true \" >i2cdetect -y 2\r\n     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\r\n00:          -- -- -- -- -- -- -- -- -- -- -- -- --\r\n10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n70: -- -- -- -- -- -- -- --\r\n<\/pre>\n<h6>Python<\/h6>\n<p>Python source code for testing the I2C bus will cycle the LEDs as shown in the video.<\/p>\n<pre class=\"height-set:true height:500 show-lang:2 lang:python decode:true\" title=\"i2cparallel.py\">#! \/usr\/bin\/env python\r\n#######################################################################\r\n# Simple I2C parallel port demostration Python class\r\n# Originally developed for the pcDuino platform\r\n#\r\n# This class file may be included in other application to provide\r\n# simple read and write support for i2c attached 8574 i2c parallel\r\n# port.\r\n#\r\n# The connection will default to I2C Bus 2 - the default external I2C\r\n# bus on the pcDuino. The default parallel port address is 0x20 - the\r\n# first standard address for the 8574.\r\n#\r\n# If this file is executed, it will run a pattern demo output on the\r\n# parallel port. Best viewed by having LEDs connected to the outputs.\r\n#\r\n# Enjoy!\r\n# Bill\r\n#######################################################################\r\n# Copyright (c) 2013 by William Greathouse, Brecksville, OH, USA\r\n#\r\n# Permission is granted to use the source code within this\r\n# file in whole or in part for any use, personal or commercial,\r\n# without restriction or limitation.\r\n#\r\n# No warranties, either explicit or implied, are made as to the\r\n# suitability of this code for any purpose. Use at your own risk.\r\n#######################################################################\r\n\r\nimport sys\r\nimport smbus\r\nimport time\r\n\r\nI2CBUS=2\r\nPARADR=0x20\r\nDELAY=0.05\r\n\r\nclass I2Cparallel:\r\n  # initialize the i2c connection to the parallel port\r\n  def __init__(self, adr=PARADR, bus=I2CBUS):\r\n    self.bus=bus\r\n    self.adr=adr\r\n    self.i2c=smbus.SMBus()\r\n    self.i2c.open(self.bus)\r\n    # Set all outputs to high state (state required for input)\r\n    # If this fails, chip is not responding on i2c bus\r\n    try:\r\n      self.i2c.write_byte(self.adr, 0xff)\r\n    except:\r\n      print \"Unable to access parallel port on bus %d, addresss 0x%02x\" % (self.bus, self.adr)\r\n      sys.exit(1)\r\n\r\n  # output value to parallel port\r\n  def out_byte(self,value):\r\n    self.i2c.write_byte(self.adr, value)\r\n\r\n  # input value from parallel port\r\n  def in_byte(self):\r\n    return self.i2c.read_byte(self.adr)\r\n\r\nif __name__==\"__main__\":\r\n  # initialize the interface, passing just the address as an example\r\n  parport=I2Cparallel(PARADR)\r\n\r\n  # flash all output lines\r\n  for _ in range(10):\r\n    parport.out_byte(0x00)\r\n    time.sleep(DELAY)\r\n    parport.out_byte(0xff)\r\n    time.sleep(DELAY)\r\n\r\n  # Do a scanning display, single bit on\r\n  for _ in range(10):\r\n    for i in range(8):\r\n      parport.out_byte(1&lt;&lt;i) time.sleep(delay)=\"\" for=\"\" i=\"\" in=\"\" range(8):=\"\" parport.out_byte(0x80=\"\"&gt;&gt;i)\r\n      time.sleep(DELAY)\r\n\r\n  # Do a scanning display, single bit off\r\n  for _ in range(10):\r\n    for i in range(8):\r\n      parport.out_byte(~(1&lt;&lt;i)) time.sleep(delay)=\"\" for=\"\" i=\"\" in=\"\" range(8):=\"\" parport.out_byte(~(0x80=\"\"&gt;&gt;i))\r\n      time.sleep(DELAY)\r\n\r\n  # Set all outputs to high state (state required for input)\r\n  parport.out_byte(0xff)<\/pre>\n","protected":false},"excerpt":{"rendered":"Today I connected up a my prototype board for an I2C experiment with the pcDuino. Blinky Lights! YouTube video of &hellip;<a href=\"https:\/\/www.the-greathouses.net\/blog\/2013\/03\/pcduino-i2c-test\/\">Continue reading &rarr;<\/a>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,21],"tags":[38,25,39],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-computers","category-python","tag-cooking-3","tag-i2c","tag-python"],"_links":{"self":[{"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":79,"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":341,"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions\/341"}],"wp:attachment":[{"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.the-greathouses.net\/blog\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}