Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Raspberry pi PCA9685 class modifying
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Raspberry pi PCA9685 class modifying

by black1990gsx on Thu Apr 29, 2021 4:51 pm

Here is my code. I'm trying to understand how to create Variables in place of the X's in the last line of code. I want to be able to pass in values to that line of code to turn on different chips at different i2c buses at different duty cycles. I have tried a few options but can't any to work so I'm stuck at this point.

I know the last line isn;t correct syntax...just trying to show what I'm trying to do

The second to last line of code is how I have it working now

This is on a raspberry pi. I'm new to circuitpython/python

Thanks,

Code: Select all | TOGGLE FULL SIZE
 from board import SCL, SDA
import busio
from adafruit_extended_bus import ExtendedI2C as I2C

# Import the PCA9685 module.
import adafruit_pca9685

# Create the I2C bus interface.
i2c1 = I2C(1)
i2c3 = I2C(3)
i2c4 = I2C(4)
# Create a simple PCA9685 class instance.

pca0a = adafruit_pca9685.PCA9685(i2c1,address=0x40)
pca1a = adafruit_pca9685.PCA9685(i2c1,address=0x41)
pca2a = adafruit_pca9685.PCA9685(i2c1,address=0x42)
pca3a = adafruit_pca9685.PCA9685(i2c1,address=0x43)
pca4a = adafruit_pca9685.PCA9685(i2c1,address=0x44)
pca0b = adafruit_pca9685.PCA9685(i2c3,address=0x40)
pca1b = adafruit_pca9685.PCA9685(i2c3,address=0x41)
pca2b = adafruit_pca9685.PCA9685(i2c3,address=0x42)
pca3b = adafruit_pca9685.PCA9685(i2c3,address=0x43)
pca4b = adafruit_pca9685.PCA9685(i2c3,address=0x44)
pca0c = adafruit_pca9685.PCA9685(i2c4,address=0x40)
pca1c = adafruit_pca9685.PCA9685(i2c4,address=0x41)
pca2c = adafruit_pca9685.PCA9685(i2c4,address=0x42)
pca3c = adafruit_pca9685.PCA9685(i2c4,address=0x43)
pca4c = adafruit_pca9685.PCA9685(i2c4,address=0x44)

# Set the PWM frequency to 60hz.
pca0a.frequency = 60
pca1a.frequency = 60
pca2a.frequency = 60
pca3a.frequency = 60
pca4a.frequency = 60
pca0b.frequency = 60
pca1b.frequency = 60
pca2b.frequency = 60
pca3b.frequency = 60
pca4b.frequency = 60
pca0c.frequency = 60
pca1c.frequency = 60
pca2c.frequency = 60
pca3c.frequency = 60
pca4c.frequency = 60

pca0a.channels[1].duty_cycle = 0xFFF

[b]pcaXX.channels[X].duty_cycle = X  [/b] 

black1990gsx
 
Posts: 9
Joined: Mon Feb 15, 2016 2:42 am

Re: Raspberry pi PCA9685 class modifying

by adafruit_support_carter on Thu Apr 29, 2021 6:48 pm

Wow! Do you really have that many PCA's? If so, what's the project? That's a lot of outputs. 15 PCAs x 16 channel/PCA = 240 outputs!

I want to be able to pass in values to that line of code to turn on different chips at different i2c buses at different duty cycles.

First, store all of your instances in something. Could be a simple tuple or a list. And then write a helper function to do the above. Something like:
Code: Select all | TOGGLE FULL SIZE
def set_duty_cycle(output, duty_cycle):
    #
    # code here that figures out correct PCA and channel from output number
    # then just output duty_cycle to that PCA/channel
    #

Then in your main code, you can just do:
Code: Select all | TOGGLE FULL SIZE
set_duty_cycle(42, 1234)

To set output 42 (which may be like channel 10 on PCA 3, up to you on mapping) to 1234 duty_cycle.

You could also do:
Code: Select all | TOGGLE FULL SIZE
def set_duty_cycle(pca, channel, duty_cycle):
    #
    # code here
    #

if you want to think in terms of PCAs/channels instead of just N total outputs.

adafruit_support_carter
 
Posts: 20032
Joined: Tue Nov 29, 2016 2:45 pm

Re: Raspberry pi PCA9685 class modifying

by black1990gsx on Mon May 03, 2021 4:46 pm

Thanks for the help getting this started. I think I'm getting close just stuck again it seems.

Yep I'm using 15 pcas on a custom pcb.

Bought a PCA9685 board to get the hardware working then scaled it up for the project.

Its a large grid of LEDS controlled over TCP.

I'm still having issues though. Getting closer but can't get it to compile and work correctly.

This is the code as it sits but its still not quite right.

The end goal is to have

set_duty_cycle(num1, str1)

num1 needs to be a var which is the output I mapped to the spot in the list

str1 needs to be the actual hex duty_cycle

Code: Select all | TOGGLE FULL SIZE
 # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT


from board import SCL, SDA
import busio
from adafruit_extended_bus import ExtendedI2C as I2C

# Import the PCA9685 module.
import adafruit_pca9685

bus_count = 3 
num1 = 4
str1 = "0x7FFF"     

# Create the I2C bus interface.
i2c1 = I2C(1)
i2c3 = I2C(3)
i2c4 = I2C(4)

# Create a simple PCA9685 class instance.

if bus_count == 1:
   pca0a = adafruit_pca9685.PCA9685(i2c1,address=0x40)
   pca1a = adafruit_pca9685.PCA9685(i2c1,address=0x41)
   pca2a = adafruit_pca9685.PCA9685(i2c1,address=0x42)
   pca3a = adafruit_pca9685.PCA9685(i2c1,address=0x43)
   pca4a = adafruit_pca9685.PCA9685(i2c1,address=0x44)
   pca0a.frequency = 60
   pca1a.frequency = 60
   pca2a.frequency = 60
   pca3a.frequency = 60
   pca4a.frequency = 60   
elif bus_count == 2:
      pca0a = adafruit_pca9685.PCA9685(i2c1,address=0x40)
      pca1a = adafruit_pca9685.PCA9685(i2c1,address=0x41)
      pca2a = adafruit_pca9685.PCA9685(i2c1,address=0x42)
      pca3a = adafruit_pca9685.PCA9685(i2c1,address=0x43)
      pca4a = adafruit_pca9685.PCA9685(i2c1,address=0x44)
      pca0b = adafruit_pca9685.PCA9685(i2c3,address=0x40)
      pca1b = adafruit_pca9685.PCA9685(i2c3,address=0x41)
      pca2b = adafruit_pca9685.PCA9685(i2c3,address=0x42)
      pca3b = adafruit_pca9685.PCA9685(i2c3,address=0x43)
      pca4b = adafruit_pca9685.PCA9685(i2c3,address=0x44)
      pca0a.frequency = 60
      pca1a.frequency = 60
      pca2a.frequency = 60
      pca3a.frequency = 60
      pca4a.frequency = 60
      pca0b.frequency = 60
      pca1b.frequency = 60
      pca2b.frequency = 60
      pca3b.frequency = 60
      pca4b.frequency = 60
elif bus_count == 3:
      pca0a = adafruit_pca9685.PCA9685(i2c1,address=0x40)
      pca1a = adafruit_pca9685.PCA9685(i2c1,address=0x41)
      pca2a = adafruit_pca9685.PCA9685(i2c1,address=0x42)
      pca3a = adafruit_pca9685.PCA9685(i2c1,address=0x43)
      pca4a = adafruit_pca9685.PCA9685(i2c1,address=0x44)
      pca0b = adafruit_pca9685.PCA9685(i2c3,address=0x40)
      pca1b = adafruit_pca9685.PCA9685(i2c3,address=0x41)
      pca2b = adafruit_pca9685.PCA9685(i2c3,address=0x42)
      pca3b = adafruit_pca9685.PCA9685(i2c3,address=0x43)
      pca4b = adafruit_pca9685.PCA9685(i2c3,address=0x44)
      pca0c = adafruit_pca9685.PCA9685(i2c4,address=0x40)
      pca1c = adafruit_pca9685.PCA9685(i2c4,address=0x41)
      pca2c = adafruit_pca9685.PCA9685(i2c4,address=0x42)
      pca3c = adafruit_pca9685.PCA9685(i2c4,address=0x43)
      pca4c = adafruit_pca9685.PCA9685(i2c4,address=0x44)
      pca0a.frequency = 60
      pca1a.frequency = 60
      pca2a.frequency = 60
      pca3a.frequency = 60
      pca4a.frequency = 60
      pca0b.frequency = 60
      pca1b.frequency = 60
      pca2b.frequency = 60
      pca3b.frequency = 60
      pca4b.frequency = 60
      pca0c.frequency = 60
      pca1c.frequency = 60
      pca2c.frequency = 60
      pca3c.frequency = 60
      pca4c.frequency = 60
   
print(bus_count)




outputs = [
"zero",
"pca0a.channels[0]",
"pca0a.channels[1]",
"pca0a.channels[2]",
"pca0a.channels[3]",
"pca0a.channels[4]",
"pca0a.channels[5]",
"pca0a.channels[6]",
"pca0a.channels[7]",
"pca0a.channels[8]",
"pca0a.channels[9]",
"pca0a.channels[10]",
"pca0a.channels[11]",
"pca0a.channels[12]",
"pca0a.channels[13]",
"pca0a.channels[14]",
"pca0a.channels[15]",
"pca1a.channels[0]",
"pca1a.channels[1]",
"pca1a.channels[2]",
"pca1a.channels[3]",
"pca1a.channels[4]",
"pca1a.channels[5]",
"pca1a.channels[6]",
"pca1a.channels[7]",
"pca1a.channels[8]",
"pca1a.channels[9]",
"pca1a.channels[10]",
"pca1a.channels[11]",
"pca1a.channels[12]",
"pca1a.channels[13]",
"pca1a.channels[14]",
"pca1a.channels[15]",
"pca2a.channels[0]",
"pca2a.channels[1]",
"pca2a.channels[2]",
"pca2a.channels[3]",
"pca2a.channels[4]",
"pca2a.channels[5]",
"pca2a.channels[6]",
"pca2a.channels[7]",
"pca2a.channels[8]",
"pca2a.channels[9]",
"pca2a.channels[10]",
"pca2a.channels[11]",
"pca2a.channels[12]",
"pca2a.channels[13]",
"pca2a.channels[14]",
"pca2a.channels[15]",
"pca3a.channels[0]",
"pca3a.channels[1]",
"pca3a.channels[2]",
"pca3a.channels[3]",
"pca3a.channels[4]",
"pca3a.channels[5]",
"pca3a.channels[6]",
"pca3a.channels[7]",
"pca3a.channels[8]",
"pca3a.channels[9]",
"pca3a.channels[10]",
"pca3a.channels[11]",
"pca3a.channels[12]",
"pca3a.channels[13]",
"pca3a.channels[14]",
"pca3a.channels[15]",
"pca4a.channels[0]",
"pca4a.channels[1]",
"pca4a.channels[2]",
"pca4a.channels[3]",
"pca4a.channels[4]",
"pca4a.channels[5]",
"pca4a.channels[6]",
"pca4a.channels[7]",
"pca4a.channels[8]",
"pca4a.channels[9]",
"pca4a.channels[10]",
"pca4a.channels[11]",
"pca4a.channels[12]",
"pca4a.channels[13]",
"pca4a.channels[14]",
"pca4a.channels[15]",
"pca0b.channels[0]",
"pca0b.channels[1]",
"pca0b.channels[2]",
"pca0b.channels[3]",
"pca0b.channels[4]",
"pca0b.channels[5]",
"pca0b.channels[6]",
"pca0b.channels[7]",
"pca0b.channels[8]",
"pca0b.channels[9]",
"pca0b.channels[10]",
"pca0b.channels[11]",
"pca0b.channels[12]",
"pca0b.channels[13]",
"pca0b.channels[14]",
"pca0b.channels[15]",
"pca1b.channels[0]",
"pca1b.channels[1]",
"pca1b.channels[2]",
"pca1b.channels[3]",
"pca1b.channels[4]",
"pca1b.channels[5]",
"pca1b.channels[6]",
"pca1b.channels[7]",
"pca1b.channels[8]",
"pca1b.channels[9]",
"pca1b.channels[10]",
"pca1b.channels[11]",
"pca1b.channels[12]",
"pca1b.channels[13]",
"pca1b.channels[14]",
"pca1b.channels[15]",
"pca2b.channels[0]",
"pca2b.channels[1]",
"pca2b.channels[2]",
"pca2b.channels[3]",
"pca2b.channels[4]",
"pca2b.channels[5]",
"pca2b.channels[6]",
"pca2b.channels[7]",
"pca2b.channels[8]",
"pca2b.channels[9]",
"pca2b.channels[10]",
"pca2b.channels[11]",
"pca2b.channels[12]",
"pca2b.channels[13]",
"pca2b.channels[14]",
"pca2b.channels[15]",
"pca3b.channels[0]",
"pca3b.channels[1]",
"pca3b.channels[2]",
"pca3b.channels[3]",
"pca3b.channels[4]",
"pca3b.channels[5]",
"pca3b.channels[6]",
"pca3b.channels[7]",
"pca3b.channels[8]",
"pca3b.channels[9]",
"pca3b.channels[10]",
"pca3b.channels[11]",
"pca3b.channels[12]",
"pca3b.channels[13]",
"pca3b.channels[14]",
"pca3b.channels[15]",
"pca4b.channels[0]",
"pca4b.channels[1]",
"pca4b.channels[2]",
"pca4b.channels[3]",
"pca4b.channels[4]",
"pca4b.channels[5]",
"pca4b.channels[6]",
"pca4b.channels[7]",
"pca4b.channels[8]",
"pca4b.channels[9]",
"pca4b.channels[10]",
"pca4b.channels[11]",
"pca4b.channels[12]",
"pca4b.channels[13]",
"pca4b.channels[14]",
"pca4b.channels[15]",
"pca0c.channels[0]",
"pca0c.channels[1]",
"pca0c.channels[2]",
"pca0c.channels[3]",
"pca0c.channels[4]",
"pca0c.channels[5]",
"pca0c.channels[6]",
"pca0c.channels[7]",
"pca0c.channels[8]",
"pca0c.channels[9]",
"pca0c.channels[10]",
"pca0c.channels[11]",
"pca0c.channels[12]",
"pca0c.channels[13]",
"pca0c.channels[14]",
"pca0c.channels[15]",
"pca1c.channels[0]",
"pca1c.channels[1]",
"pca1c.channels[2]",
"pca1c.channels[3]",
"pca1c.channels[4]",
"pca1c.channels[5]",
"pca1c.channels[6]",
"pca1c.channels[7]",
"pca1c.channels[8]",
"pca1c.channels[9]",
"pca1c.channels[10]",
"pca1c.channels[11]",
"pca1c.channels[12]",
"pca1c.channels[13]",
"pca1c.channels[14]",
"pca1c.channels[15]",
"pca2c.channels[0]",
"pca2c.channels[1]",
"pca2c.channels[2]",
"pca2c.channels[3]",
"pca2c.channels[4]",
"pca2c.channels[5]",
"pca2c.channels[6]",
"pca2c.channels[7]",
"pca2c.channels[8]",
"pca2c.channels[9]",
"pca2c.channels[10]",
"pca2c.channels[11]",
"pca2c.channels[12]",
"pca2c.channels[13]",
"pca2c.channels[14]",
"pca2c.channels[15]",
"pca3c.channels[0]",
"pca3c.channels[1]",
"pca3c.channels[2]",
"pca3c.channels[3]",
"pca3c.channels[4]",
"pca3c.channels[5]",
"pca3c.channels[6]",
"pca3c.channels[7]",
"pca3c.channels[8]",
"pca3c.channels[9]",
"pca3c.channels[10]",
"pca3c.channels[11]",
"pca3c.channels[12]",
"pca3c.channels[13]",
"pca3c.channels[14]",
"pca3c.channels[15]",
"pca4c.channels[0]",
"pca4c.channels[1]",
"pca4c.channels[2]",
"pca4c.channels[3]",
"pca4c.channels[4]",
"pca4c.channels[5]",
"pca4c.channels[6]",
"pca4c.channels[7]",
"pca4c.channels[8]",
"pca4c.channels[9]",
"pca4c.channels[10]",
"pca4c.channels[11]",
"pca4c.channels[12]",
"pca4c.channels[13]",
"pca4c.channels[14]",
"pca4c.channels[15]"   ]





# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
# but the PCA9685 will only actually give 12 bits of resolution.

def set_duty_cycle(output, duty_cycle):
   
    pca = outputs[num1]
    output = outputs.index(pca)
    #duty_cycle = duty_cycle
    #pca.duty_cycle = 0x7FFF
    print(outputs[num1])
    print(output)
   
   


#pca = pca0a.channels[0]
#pca.duty_cycle = 0

set_duty_cycle(num1, str1)

pca.duty_cycle = str1

print(num1)
print(str1)

black1990gsx
 
Posts: 9
Joined: Mon Feb 15, 2016 2:42 am

Re: Raspberry pi PCA9685 class modifying

by adafruit_support_carter on Mon May 03, 2021 5:15 pm

Keep trying. You'll figure it out eventually. There's all kinds of ways to do this.

Here's some simple math that could help:
Code: Select all | TOGGLE FULL SIZE
def get_pca_and_channel(output):
    return output // 16, output % 16


Instead of making a list of 240 channels, just make a list of your 15 PCA's. Then with the above, the first return gives you the PCA and the second return gives you the channel number on that PCA.

adafruit_support_carter
 
Posts: 20032
Joined: Tue Nov 29, 2016 2:45 pm

Re: Raspberry pi PCA9685 class modifying

by black1990gsx on Wed May 05, 2021 10:45 am

That last section confused me.

I'm just trying to get the code I have already figured out working. I can then refine it but the list I have works when I print out the values.

I'm still having issues calling the function with var in it for the values.

Just trying to understand what I'm doing wrong so i can learn from it.

if I call for example pca = pca0a.channels[0] outside the function it works. If I try the same inside the function it says its not defined.

This is where I'm having the most issues.

If I can understand how to call the pca.duty_cycle inside the function that would help out..if there is a tutorial out there on that subject that would also help

Thanks,

black1990gsx
 
Posts: 9
Joined: Mon Feb 15, 2016 2:42 am

Please be positive and constructive with your questions and comments.