petrockblog / RPi-MCP23S17

This Python module abstracts the GPIO expander MCP23S17 for usage on the Raspberry Pi.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

default speed is too fast for the chip.

CarlFK opened this issue · comments

(venv) pi@raspberrypi:~/RPi-MCP23S17 $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from RPiMCP23S17.MCP23S17 import MCP23S17
>>> mcp1 = MCP23S17(bus=0x00, pin_cs=0x00, device_id=0x00)
>>> mcp1.open()
>>> mcp1.setDirection(0, mcp1.DIR_OUTPUT)
>>> mcp1.digitalWrite(0, MCP23S17.LEVEL_HIGH)
>>> assert mcp1.digitalRead(0) == MCP23S17.LEVEL_HIGH
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> mcp1.digitalRead(0)
0

additional info:
if I run this code:
https://github.com/CarlFK/Ripley-1/blob/master/src/tests/23s17.c

from RPiMCP23S17.MCP23S17 import MCP23S17
import time

mcp1 = MCP23S17(bus=0x00, pin_cs=0x00, device_id=0x00)
mcp1.open()

print("DIR_OUTPUT")
mcp1.setDirection(0, mcp1.DIR_OUTPUT)
while True:
    time.sleep(.001)
    print(mcp1.digitalRead(0))

when the C code sets the pin, the print() above prints 1.

more baseline of what works:

(venv) pi@cccpi:~ $ gpio -x mcp23s17:123:0:0 mode 123 out

(venv) pi@cccpi:~ $ gpio -x mcp23s17:123:0:0 write 123 1  # solenoid fires  
(venv) pi@cccpi:~ $ gpio -x mcp23s17:123:0:0 read 123 
1
(venv) pi@cccpi:~ $ gpio -x mcp23s17:123:0:0 write 123 0  # solenoid off
(venv) pi@cccpi:~ $ gpio -x mcp23s17:123:0:0 read 123 
0

problem solved: the default speed is too fast for the chip.

"Supports up to 10MHz SPI™ clock speeds"

from RPiMCP23S17.MCP23S17 import MCP23S17
mcp1 = MCP23S17(bus=0x00, pin_cs=0x00, device_id=0x00)
mcp1.open()
mcp1._spi.max_speed_hz
125000000

lowering that fixed all my problems

all these worked:

mcp1._spi.max_speed_hz=100000000
mcp1._spi.max_speed_hz=1000000
mcp1._spi.max_speed_hz=7