jollyjohn
Posts: 16
Joined: Mon Mar 04, 2013 9:54 am

Addresses for 2nd servo board

Tue Jan 02, 2018 11:57 am

I have set up a Pi Zero with an adafruit servo controller using default address 0x40 and this works. I have added a second board and given it the address 0x41.
I have two servos - for testing - both connected to channel 0 one on each board.
My code works but moves both servos at the same time when run.
How do address the second board correctly??

Thanks

SurferTim
Posts: 1480
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Addresses for 2nd servo board

Tue Jan 02, 2018 12:14 pm

I don't know which board you are using, but I presume the 16 channel i2c model. I haven't tried a second servo board, but this what I would try:

Code: Select all

# Initialise the PCA9685 using the default address (0x40).
pwm1 = Adafruit_PCA9685.PCA9685(address=0x40)

# Alternatively specify a different address and/or bus:
pwm2 = Adafruit_PCA9685.PCA9685(address=0x41)
You will need to change your code from just pwm to pwm1 and pwm2.

jollyjohn
Posts: 16
Joined: Mon Mar 04, 2013 9:54 am

Re: Addresses for 2nd servo board

Tue Jan 02, 2018 4:11 pm

Hi

Thanks

I can confirm that's what I have done but I cant seem to address channel 16 - i.e the first channel on board 2.

If I use the line "setDegree(0, degree0)" - channel 0 on board 1 works and so does channel 0 on board 2 at the same time.

If I try the line "setDegree(16, degree 0)" I get an error message "Error accesing 0x40 check i2c address" and "Error accessing 0x41 check i2c address" But the program does run......

So how do I directly address the channels on board 2?

Cheers

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4953
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Addresses for 2nd servo board

Tue Jan 02, 2018 4:29 pm

What library are you using?
At a guess the Adafruit one at https://github.com/adafruit/Adafruit_Python_PCA9685, but that doesn't appear to have a function called setDegree. If extending it with the code from https://www.gadgetdaily.xyz/control-ser ... pberry-pi/ then it needs modification, and SurferTim has already given you 50% of it.

Code: Select all

def setDegree(channel, d):
    degreePulse = servoMin
    degreePulse += int((servoMax - servoMin) / maxDegree) * d
    pwm.setPWM(channel, 0, degreePulse)
See that "pwm." at the start of the last line? That needs to be the pwm1 or pwm2 that he told you to create.
You need to amend the function so you can call "setDegree([pwm1|pwm2], channel, degrees)", where channel is still in the range 0 to 15.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

SurferTim
Posts: 1480
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Addresses for 2nd servo board

Tue Jan 02, 2018 4:30 pm

This is a mod of the test code included with the Adafruit library. I have not tested it because I don't have 2 boards.

Code: Select all

# Simple demo of of the PCA9685 PWM servo/LED controller library.
# This will move channel 0 from min to max position repeatedly.
# Author: Tony DiCola
# License: Public Domain
from __future__ import division
import time

# Import the PCA9685 module.
import Adafruit_PCA9685

# Uncomment to enable debug output.
#import logging
#logging.basicConfig(level=logging.DEBUG)

# Initialise the PCA9685 using the default address (0x40).
pwm0 = Adafruit_PCA9685.PCA9685(address=0x40)

# Alternatively specify a different address and/or bus:
pwm1 = Adafruit_PCA9685.PCA9685(address=0x41)

# Configure min and max servo pulse lengths
servo_min = 150  # Min pulse length out of 4096
servo_max = 600  # Max pulse length out of 4096

# Helper function to make setting a servo pulse width simpler.
def set_servo_pulse(pwmno,channel, pulse):
    pulse_length = 1000000    # 1,000,000 us per second
    pulse_length //= 60       # 60 Hz
    print('{0}us per period'.format(pulse_length))
    pulse_length //= 4096     # 12 bits of resolution
    print('{0}us per bit'.format(pulse_length))
    pulse *= 1000
    pulse //= pulse_length
    if pwmno == 0:
        pwm0.set_pwm(channel, 0, pulse)
    elif pwmno == 1:
        pwm1.set_pwm(channel, 0, pulse)

# Set frequency to 60hz, good for servos.
pwm0.set_pwm_freq(60)
pwm1.set_pwm_freq(60)

print('Moving servo on channel 0, press Ctrl-C to quit...')
while True:
    # Move servo on board 0 channel O between extremes.
    pwm0.set_pwm(0, 0, servo_min)
    time.sleep(1)
    pwm0.set_pwm(0, 0, servo_max)
    time.sleep(1)
    # Move servo on board 1 channel O between extremes.
    pwm1.set_pwm(0, 0, servo_min)
    time.sleep(1)
    pwm1.set_pwm(0, 0, servo_max)
    time.sleep(1)

jollyjohn
Posts: 16
Joined: Mon Mar 04, 2013 9:54 am

Re: Addresses for 2nd servo board

Wed Jan 03, 2018 12:47 pm

Thanks, that looks helpful I will look at it again at the weekend as I am back to work now.

jollyjohn
Posts: 16
Joined: Mon Mar 04, 2013 9:54 am

Re: Addresses for 2nd servo board

Sun Jan 07, 2018 4:33 pm

Thanks Surfer Tim

I have updated the libraries from Adafruit and run your code - It Works!

All I need to do now is have a careful read of the code and incorporate it into my own program.

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 7 guests