gavhot
Posts: 5
Joined: Sun Dec 29, 2013 6:28 pm

PCF8574AP I2C LED's

Sun Dec 29, 2013 6:44 pm

Hi all,

Very new to Raspberry PI but loving it, I can use the GPIO easily to control LED's but I tried to create more by adding an expansion module but I have run into problems.

My setup is Raspberry 512mb model with PCF8574AP from http://cpc.farnell.com/jsp/search/produ ... KU=SC10500

I was trying to follow this tutorial http://www.cl.cam.ac.uk/projects/raspbe ... e/two.html - They have a part on Expanding the Raspberry Pi pin header.

So my issue is getting to the PCF8574AP to do anything, the first issue is there are 2 different header versions and the tutorial only suggested running "sudo i2cdetect -y 0" so after google I found the correct command should be "sudo i2cdetect -y 1" which returns:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- 38 -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

I tried the code:

import smbus
import time
# Access the i2c bus now.
bus = smbus.SMBus(1)
# Now write 1 to the device with the address 38, turn off the LED by setting pin 0 to 1, and reset the switch by switchi$
bus.write_byte(38, 1)
time.sleep(0.1)
while 1:
# If the button is pressed, pin 1 will be 1 and the byte read from the device with address 56 will be 00000010 (2) or 00$

if bus.read_byte(38) in (2,3):

# Write 00000000, setting pin 0 to 0, turning on the LED, and resetting the switch with pin 1 to 0.
bus.write_byte(38, 0)
time.sleep(0.1)
else:

# Write 00000010, setting pin 0 to 1, turning off the LED, and pin 1 to 0 to reset the switch.

bus.write_byte(38, 1)
time.sleep(0.1)

This gives the error and does nothing:
Traceback (most recent call last):
File "smbus", line 10, in <module>
bus.write_byte(38, 1)
IOError: [Errno 5] Input/output error

So looking around I found "sudo i2cset -y 1 0x38 0xff 0xff" which does seem to run from the terminal but I can't seem to find how to turn pin 1 to light an LED, or another pin actually.

Anyone got any ideas?

Also is there anyway to get data from a pin on the PCF8574AP i.e. if I was to connect a temperature sensor.

Thanks

User avatar
DougieLawson
Posts: 39837
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: PCF8574AP I2C LED's

Sun Dec 29, 2013 10:43 pm

The address for the device is hexadecimal (base 16 maths). So that's 0x38 or 56 (decimal). So you need to fix all of your bus.write_byte & bus.read_byte calls.

BTW, when you post code (especially python which is whitespace critical) can you post it in [code][/code] tags.

Then it comes out as

Code: Select all

#!/usr/bin/python
try: 
  print ("Hello")
  x=0x38
  print x
except:
  exit
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: PCF8574AP I2C LED's

Sun Dec 29, 2013 10:52 pm

Dougie is right. you need 0x38 to be declared, not 38.

I find it is helpful to first do what you need with i2cset and i2cget. Then once it is fully understood, then translate that to python, c, etc.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

gavhot
Posts: 5
Joined: Sun Dec 29, 2013 6:28 pm

Re: PCF8574AP I2C LED's

Sun Dec 29, 2013 11:23 pm

Hi guys,

thanks for the responses, when i try:

Code: Select all

import smbus
import time

# Access the i2c bus now.

bus = smbus.SMBus(1)

# Now write 1 to the device with the address 56, turn off the LED by setting pin 0 to 1, and reset the switch by switchi$

bus.write_byte(56, 1)
time.sleep(0.1)
while 1:
# If the button is pressed, pin 1 will be 1 and the byte read from the device with address 56 will be 00000010 (2) or 00$

    if bus.read_byte(56) in (2,3):

# Write 00000000, setting pin 0 to 0, turning on the LED, and resetting the switch with pin 1 to 0.
        bus.write_byte(56, 0)
        time.sleep(0.1)
    else:

# Write 00000010, setting pin 0 to 1, turning off the LED, and pin 1 to 0 to reset the switch.

        bus.write_byte(56, 1)
time.sleep(0.1)
I get the following error:
Traceback (most recent call last):
File "smbus", line 24, in <module>
bus.write_byte(56, 1)
IOError: [Errno 5] Input/output error

If I change it to 0x38 I get the following error:
Traceback (most recent call last):
File "smbus", line 15, in <module>
if bus.read_byte(0x38) in (2,3):
IOError: [Errno 5] Input/output error


sudo i2cset -y 1 0x38 0xff 0xff - seems to run and not report an error from terminal, what would I need to do to set a pin to turn on a LED on,

Thanks

gavhot
Posts: 5
Joined: Sun Dec 29, 2013 6:28 pm

Re: PCF8574AP I2C LED's

Sun Dec 29, 2013 11:31 pm

Also when I run "sudo i2cset -y 1 0x38 0xff 0xff" over and over every now and again I get "Error: Write failed" as you can see from below,

pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
Error: Write failed
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
Error: Write failed
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
Error: Write failed
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
Error: Write failed
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
Error: Write failed
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
Error: Write failed
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
Error: Write failed
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff

User avatar
DougieLawson
Posts: 39837
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: PCF8574AP I2C LED's

Mon Dec 30, 2013 10:29 am

Are you running your python program with sudo python scriptname.py ?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: PCF8574AP I2C LED's

Mon Dec 30, 2013 10:42 am

do the following

Code: Select all

i2cdump 1 0x38
and post the results
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

gavhot
Posts: 5
Joined: Sun Dec 29, 2013 6:28 pm

Re: PCF8574AP I2C LED's

Mon Dec 30, 2013 11:43 am

Hi guys

DougieLawson - I type:
pi@raspberrypi ~/scripts $ sudo python smbus
Traceback (most recent call last):
File "smbus", line 24, in <module>
bus.write_byte(0x38, 1)
IOError: [Errno 5] Input/output error
The only thing is different is the file smbus is not a smbus.py file.

Richard-TX - as below:

Code: Select all

pi@raspberrypi ~/scripts $ i2cdump 1 0x38
No size specified (using byte-data access)
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1, address 0x38, mode byte
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 01 02 03 XX 05 06 07 XX 09 0a 0b 0c 0d 0e XX    .???X???X??????X
10: 10 11 12 13 14 15 16 17 18 XX 1a 1b 1c 1d 1e 1f    ?????????X??????
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f     !"#$%&'()*+,-./
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c XX 3e 3f    0123456789:;<X>?
40: XX 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f    XABCDEFGHIJKLMNO
50: 50 51 52 53 54 55 56 XX 57 59 5a 5b 5c 5d 5e 5f    PQRSTUVXWYZ[\]^_
60: 60 XX 61 63 64 65 XX 67 XX 69 6a 6b 6c 6d 6e XX    `XacdeXgXijklmnX
70: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f    pqrstuvwxyz{|}~?
80: 80 81 82 83 XX 85 86 87 XX 89 8a 8b 8c 8d 8e 8f    ????X???X???????
90: 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f    ????????????????
a0: a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab XX ad ae af    ????????????X???
b0: b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf    ????????????????
c0: c0 c1 c2 c3 c4 c5 c6 XX c8 c9 ca cb cc cd ce cf    ???????X????????
d0: XX d1 d2 d3 d4 d5 d6 d7 d8 XX da db dc dd de df    X????????X??????
e0: e0 e1 e2 e3 e4 e5 e6 XX e8 e9 ea eb ec ed ee ef    ???????X????????
f0: f0 f1 f2 f3 f4 XX f6 XX f8 f9 fa fb fc fd fe ff    ?????X?X???????.
thanks

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: PCF8574AP I2C LED's

Mon Dec 30, 2013 12:47 pm

pi@raspberrypi ~/scripts $ sudo i2cset -y 1 0x38 0xff 0xff
referring to the data sheet at http://www.nxp.com/documents/data_sheet ... F8574A.pdf try to find out why you would want or not want to write at location 0xff a value of 0xff.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

gavhot
Posts: 5
Joined: Sun Dec 29, 2013 6:28 pm

Re: PCF8574AP I2C LED's

Mon Dec 30, 2013 1:35 pm

Hi Richard-TX

Going over that document is like another language, any guidance on how I work out the addresses would be great if possible,

Is this a normal expansion chip or have I chosen a bad one, if so what would be the best and easiest to work with.

Cheers.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: PCF8574AP I2C LED's

Mon Dec 30, 2013 3:41 pm

I happen to like the MCP23017. There are many tutorials around that chip. Cost is under $2 each.

Lots of I-O with that chip as it has 16 ports.

Back to the 8574.

I know reading a data sheet is difficult. The point was to get you to ask about the control register.

Since the device defaults to inputs, try applying a +3 to one of the port pins and compare a i2c dump from before you applied the voltage to what a i2c dump looks like after.

Once you see what byte/bit changes, then you are well on your way to understanding how this really works.

BTW there is no reason to write 0xff to address 0xff. It is an invalid location.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

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