Pull_Up / Pull_Down function for MCP23017 while using channels as input ?

2,371 views
Skip to first unread message

Julian

unread,
May 5, 2014, 12:22:11 AM5/5/14
to web...@googlegroups.com
Hi everyone
and thanks to trouch for WebIOPi - its just fantastic how versatile it is !!

I am using 3x MCP23017 to monitor my house, so I need it for a few extra digital inputs.
There is a function to enable the onboard pull-up or pull-down resistors for the RPI's onboard GPIOs in the python script:

def setup():
    mcp = webiopi.deviceInstance("mcp") # retrieve the device named "mcp" in the configuration
    mcpI1 = webiopi.deviceInstance("mcpI1") # retrieve the device named "mcpI1" in the configuration
    mcpI2 = webiopi.deviceInstance("mcpI2") # retrieve the device named "mcpI2" in the configuration
  
    GPIO.setFunction(17, GPIO.IN, GPIO.PUD_UP)

However, when I try to use it for the mcp:

    mcpI2.setFunction(1, GPIO.IN, GPIO.PUD_UP)

I get this error message, when starting up from the command line:

ERROR - setFunction() takes exactly 3 positional arguments (4 given)
Traceback (most recent call last):
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/__main__.py", line 75, in <module>
    main(sys.argv)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/__main__.py", line 69, in main
    server = Server(port=port, configfile=configfile, scriptfile=scriptfile)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/server/__init__.py", line 75, in __init__
    loader.loadScript(name, source, self.restHandler)
  File "/usr/local/lib/python3.2/dist-packages/WebIOPi-0.7.0-py3.2-linux-armv6l.egg/webiopi/utils/loader.py", line 12, in loadScript
    script.setup()
  File "/home/pi/Regeln/python/script.py", line 13, in setup
    mcpI2.setFunction(1, GPIO.IN, GPIO.PUD_UP)
TypeError: setFunction() takes exactly 3 positional arguments (4 given)

I am wondering, if I can use the internal Pull-Up resistors of the MCP23017,
or if I have to solder some pull-ups to the input channels. (quite a few ...)

Thanks for any help :-)
Julian





Andreas Riegg

unread,
May 5, 2014, 2:35:15 AM5/5/14
to web...@googlegroups.com
Julian,

the pull-up/down function is only available for the native GPIO ports and is tbd for a future realease of the MCP23xxx driver and not implemented currently.

Soldering the few resistors will be quicker ... :-) :-)

Andreas

Julian

unread,
May 13, 2014, 8:54:56 AM5/13/14
to web...@googlegroups.com
Thanks Andreas,

that is the info that I needed,
no worries, I have already soldered all the resistors and now it is working fine :-)
Thanks heaps ! :-)

Julian

Krystian Nowicki

unread,
Jan 5, 2015, 9:26:01 PM1/5/15
to web...@googlegroups.com
I'm sorry replying to an old thread but I cannot agree with soldering. More quickly method is use this command in console: 

sudo i2cset -y 1 0x20 0x06 0x03 

(0x20 - device address, 0x06 - register address [pull up configuration], 0x03 - value 00000011 - output 1 and 2 to pull up for example.   

You can use a cron to automatic run this line at startup. 
Message has been deleted

jor...@gmail.com

unread,
Jan 11, 2015, 3:04:30 PM1/11/15
to web...@googlegroups.com
Hi Krystian
This would be a good solution for me, I would very much like to avoid too many wires and connections, especially if I need to hand over the project later to someone else.
Can you help me confirm if I got the commands correct?

I have two I/O boards, 32 pins on each and one set up as MCP0 / MCP1 with addresses 0x20 and 0x21, the other as MCP2 / MCP3  with addresses 0x22 and 0x23.
I want to use board two as IN and pull up all resistors on this board, my guess is that I would use

sudo i2cset -y 1 0x22 0x06 0xff
sudo i2cset -y 1 0x23 0x06 0xff

/Henrik

(edited for typo)

Krystian Nowicki

unread,
Jan 11, 2015, 3:53:21 PM1/11/15
to web...@googlegroups.com

If you use MCP23017 (16 pins I/O) this commands it's half correct. Now, you set pull up for first 8 pins on your MCP2 and MCP3 (GPA0-7).
MCP23017 use 2 pull-up register, first for GPA 0-7, second for GPB 0-7. 

sudo i2cset -y 1 0x22 0x06 0xff
sudo i2cset -y 1 0x22 0x16 0xff
sudo i2cset -y 1 0x23 0x06 0xff
sudo i2cset -y 1 0x23 0x16 0xff

I think it is correct, but i don't make a test with this chip. 
Many information about i2c is in datasheet. http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf




To set chip from python i use command os.system from os libary. Example:

import os

def mowa(tekst):

os.system('espeak -v pl -a 200 tekst --stdout | aplay 2>/etc/null')

return tekst

Another example:

os.system('sudo i2cset -y 1 0x23 0x16 0xff')

jor...@gmail.com

unread,
Jan 11, 2015, 7:23:32 PM1/11/15
to web...@googlegroups.com
Thank you very much Krystian, I will look into this

/Henrik

Andreas Riegg

unread,
Jan 14, 2015, 5:18:22 AM1/14/15
to web...@googlegroups.com
Hi,

just some additional remarks and tips from my side.

Doing this kind of additional tweaks for chips is a valid way to temporarily overcome limitations/missing functions of the current chip drivers of WebIOPi. However, some things must be kept in mind:

  • The proposed solution does only work for I2C chips. It does NOT work for SPI or any other kind of chips. For those, other kind of Linux tools (which I don't know) have to be used.
  • The proposed solution does only work if you install and enable the needed I2C tools/modules by your own. The WebIOPi setup process does NOT do this. You can use the following commands:
"sudo apt-get install i2c-tools" (this installs the needed tools)
"sudo nano /etc/modprobe.d/raspi-blacklist.conf" (make sure to disable the blacklisting of the I2C modules)
"sudo nano /etc/modules" (add the following two modules for automatic loading at Linux boot)
     i2c-bcm2708
     i2c-dev
  • When using i2cset (or i2cget) please make sure you really understand the I2C protocol of the chips (which is "buried" in the sequence of the bytes transfered via the 0x... bytes contained in the example above). The case for MCP23017 is luckily simple, but other chips exist where the situation is much more complex because special bits have to be set or a specific command sequance has to be obeyed.
  • It is likely to work stable and cause no problems if you touch only chip registers that the WebIOPi drivers do not touch (at least for setting them, reading should be ok). On the other hand, it may cause unwanted behaviour or become unreliable if concurrent updates of chip registers are done by WebIOPi drivers and I2C tools (it does not matter if you call these tools from a shell or from within Python using the os library). WebIOPi chip drivers assume that they are the only instance that does communication with the chips and have (almost) no code built in that takes simultaneous chip register manipulation from other sources into account. So, please have a look at the source of the WebIOPi chip drivers to check which registers are touched by WebIOPi and which are not.
  • All these things require root privileges, so using "sudo" is mandatory for all commands.
Hope this clarifies things a bit further for the moment.

Best,
Andreas

jor...@gmail.com

unread,
Jan 18, 2015, 8:13:30 PM1/18/15
to web...@googlegroups.com
Thanks for the tips, Andreas, luckily I only need to set the pull-up resistor on the MCP23017, so in theory it should be simple enough.
I did get it to work using the tutorial at https://www.abelectronics.co.uk/iopi-tutorial2/info.aspx
At the moment I am looking to find the correct code to listen for inputs from the physical button and get it to fire off a macro, I posted that question in my thread about my project https://groups.google.com/forum/#!topic/webiopi/f6JVW7ctE3M

/Henrik
Reply all
Reply to author
Forward
0 new messages