Pascal
Posts: 70
Joined: Sat Oct 20, 2012 7:23 am

buttons and pcf8574 in python

Fri Feb 15, 2013 6:52 am

I read that it is possible to conenct buttons on pcf8574 and i also found a schematic.
But odes someone have a working part of code to let this work?
I search google but no Python code to be found.

Thx Pascal.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: buttons and pcf8574 in python

Fri Feb 15, 2013 1:15 pm

You need to enable i2c support on the Raspberry Pi and install the python-smbus package. There are several threads and web pages that tell you how.

Simple test:

Code: Select all

#!/usr/bin/env python

import smbus
import time

# i2c address of PCF8574
PCF8574=0x20

# open the bus (0 -- original Pi, 1 -- Rev 2 Pi)
b=smbus.SMBus(0)

# make certain the pins are set high so they can be used as inputs
b.write_byte(PCF8574, 0xff)

while 1:
    pins = b.read_byte(PCF8574)
    print "%02x" % pins
    time.sleep(1)
Enjoy!
Bill

Pascal
Posts: 70
Joined: Sat Oct 20, 2012 7:23 am

Re: buttons and pcf8574 in python

Fri Feb 15, 2013 7:28 pm

Thx Bill , the script shows in a loop FF andif i push a button then its shows the a new number for example on port 4 its ef.
But say i want to print hallo on the screen if the button on port 4 is pushed how must i put that then?
I tryed with:

Code: Select all

if pins == ef:
print "Hallo"
But if i push the button i get the error.:
Nameerror: name ef is not defined
and if i try.:

Code: Select all

if pins == "ef":
print "Hallo"
Then it stays nice in a loop but if i push the button i dont get the text Hallo but only again ef.
What do i wrong?

Edit.:
I solved it to remove this part.:

Code: Select all

print "%02x" % pins
and only use print pins and use that number and it works.

Just one question.
I've 4 buttons on P4,5,6,7 and on p0,1,2,3 i have leds.
But the leds only burn very weak, what could be wrong because normaly they burn bright.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: buttons and pcf8574 in python

Fri Feb 15, 2013 8:44 pm

The print format I use shows the value in hexadecimal. You need to test against hexadecimal if you want to try a specific value.

Try this:

Code: Select all

if pins == 0xef:
    print "Hallo"
The "0x" says the following is in hexadecimal. :)

Even better, and allowing for multiple key presses:

Code: Select all

if (pins & 0x01) == 0:
    print "Hallo 0"
if (pins & 0x02) == 0:
    print "Hallo 1"
if (pins & 0x04) == 0:
    print "Hallo 2"
if (pins & 0x08) == 0:
    print "Hallo 3"
if (pins & 0x10) == 0:
    print "Hallo 4"
if (pins & 0x20) == 0:
    print "Hallo 5"
if (pins & 0x40) == 0:
    print "Hallo 6"
if (pins & 0x80) == 0:
    print "Hallo 7"
This tests each bit separately.

Enjoy!
Bill

Pascal
Posts: 70
Joined: Sat Oct 20, 2012 7:23 am

Re: buttons and pcf8574 in python

Sat Feb 16, 2013 7:26 am

Thx it works like a charm :D

BinaryBedlam
Posts: 1
Joined: Mon Jun 10, 2013 5:37 pm

Re: buttons and pcf8574 in python

Mon Jun 10, 2013 5:49 pm

To make the leds bright, connect the led directly to 5v instead of ground, then turn on by setting bit to 0 instead of 1. This is simply inversed because the pcf8574 has "open drain" outputs, it means when the bit is zero its like a switch closed to ground, but when its a one, the switch is open with no connection to ground or a voltage.

13ros27
Posts: 1
Joined: Thu Sep 01, 2016 5:11 pm

Re: buttons and pcf8574 in python

Thu Sep 01, 2016 5:20 pm

In case anyone here needs to install python-smbus the code in Lx-Terminal is:

Code: Select all

sudo apt-get update
sudo apt-get install python-smbus
:D

Return to “Python”