baskarz
Posts: 4
Joined: Thu Apr 02, 2015 2:34 am

mcp23s17 as gpio outputs along rpi gpio in python script

Thu Oct 26, 2017 2:39 pm

Hi, looking for some help, my python knowledge and programming in general is very basic.
I generally copy paste existing code and fiddle with it til it works how i want.

So my plea for help is i have a python script i use for my xmas lights every year.
I had been limited to 15 channels but this year i have added a MCP23S17 io expander.
I have managed to get this to work with LightshowPi easy enough but i have python scripts which just toggle gpio in binary sequences.
Attached is the code in question.

What i need to do is add the mcp23s17 outputs to the list so i can expand the sequences from 15 to 30.
I just have no idea how to include the mcp23s17 in the script...

For example - where the gpio are defined at the beginning

# Define GPIO outputs
RpiGPIO = [30,28,29,17,18,27,10,24,22,23,31,8,9,11,25]

I want to add the mcp23s17 gpio to the end so it would be like -

# Define GPIO outputs
RpiGPIO = [30,28,29,17,18,27,10,24,22,23,31,8,9,11,25,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80]

where the gpio 65 to 80 are the mcp23s17 gpio
Then i can expand my sequences to suit the extra gpio

Hopefully that makes sense to someone and i will be eternally grateful to whoever can assist me! :D

here is one of the shorter scripts, i want to basically add the new gpio to it .
ie: where i define gpio outputs i want to add the mcp23 gpio outputs and extend the sequences to suit

Code: Select all

#!/usr/bin/python
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
 
# Define GPIO outputs
RpiGPIO = [30,28,29,17,18,27,10,24,22,23,31,8,9,11,25]
 
# Set all pins as output
for pinref in RpiGPIO:
#  print "Setup pins"
  GPIO.setup(pinref,GPIO.OUT)
 
# Define some settings
StepCounter = 0
StepDir = 1
WaitTime = 0.2
 
# all flash then sequential
StepCount3 = 24
Seq3 = []
Seq3 = range(0,StepCount3)
Seq3[0] =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Seq3[1] =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Seq3[2] =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Seq3[3] =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Seq3[4] =[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Seq3[5] =[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0]
Seq3[6] =[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0]
Seq3[7] =[1,1,1,1,0,0,0,0,0,0,0,0,0,0,0]
Seq3[8] =[1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
Seq3[9] =[1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]
Seq3[10] =[1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]
Seq3[11] =[1,1,1,1,1,1,1,0,0,0,0,0,0,0,0]
Seq3[12] =[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0]
Seq3[13] =[1,1,1,1,1,1,1,1,1,0,0,0,0,0,0]
Seq3[14] =[1,1,1,1,1,1,1,1,1,1,0,0,0,0,0] 
Seq3[15] =[1,1,1,1,1,1,1,1,1,1,1,0,0,0,0]
Seq3[16] =[1,1,1,1,1,1,1,1,1,1,1,1,0,0,0]
Seq3[17] =[1,1,1,1,1,1,1,1,1,1,1,1,1,0,0]
Seq3[18] =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0]
Seq3[19] =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Seq3[20] =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Seq3[21] =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Seq3[22] =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Seq3[23] =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

# Choose a sequence to use
Seq = Seq3
StepCount = StepCount3
 
# Start main loop
while True:
 # print "-- Step : %i --" %StepCounter
  for pinref in range(0, 15):
    xpin=RpiGPIO[pinref]#
    # Check if LED should be on or off
    if Seq[StepCounter][pinref]!=0:
    #  print " Enable %i" %xpin
      GPIO.output(xpin, True)
    else:
    #  print " Disable %i" %xpin
      GPIO.output(xpin, False)
 
  StepCounter += StepDir
 
  # If we reach the end of the sequence reverse
  # the direction and step the other way
  if (StepCounter==StepCount) or (StepCounter<0):
    StepDir = StepDir * -1
    StepCounter = StepCounter + StepDir + StepDir
 
  # Wait before moving on
  time.sleep(WaitTime)

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: adding mcp23s17 to xmas light script

Thu Oct 26, 2017 8:09 pm

You have a couple of choices

First if you confident then you can write your own driver to drive the mcp23s17 see data sheet http://ww1.microchip.com/downloads/en/D ... 01952C.pdf the meaty part starts at page 12

Second you can down load someone elses driver that have already written, there will be lots of them if you search the net here is tutorital from someone that downloaded a driver and used it see https://blog.petrockblock.com/2016/05/1 ... pberry-pi/

baskarz
Posts: 4
Joined: Thu Apr 02, 2015 2:34 am

Re: adding mcp23s17 to xmas light script

Sat Nov 25, 2017 12:48 pm

thanks for the reply, but my skills are minimal and i have no idea how to do what your saying, all my script experience has been copy paste and try fiddle with it til it works the way i want it. :roll:

Ideally if someone could show me how to add the mcp23s17 gpios to my script and basically allow me to control the extra outputs as if they are rpi gpios.

so in the script i posted first, i want to expand the
Seq1[0] =[1,0,0,1,1,1,1,0,0,0,0,1,1,1,1]
to
Seq1[0] =[1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]

with the last 16 being the mcp gpio if that makes sense.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13142
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: mcp23s17 as gpio outputs along rpi gpio in python script

Sun Nov 26, 2017 2:59 pm

you double posted, which isn't allowed, so I deleted your later post, but merged some of its newer content with this one as a service.
hopefully someone will help.

Return to “Python”