equals
Posts: 11
Joined: Thu Oct 27, 2016 7:48 pm

Adafruit servo HAT - How to move servo?

Wed Nov 30, 2016 5:16 am

I recently put together a servo HAT https://www.adafruit.com/products/2327 and I am not quite sure how to move a servo that I have attached to it. I setup i2c and have the address of 0x40 for the HAT. I have been using wiringPi for all of my other sensors and would like to use it for the servo HAT but I am unsure how to control it. Do I use the wiringPi i2c library or the wiringPi PWM library, or both? I am confused because the wiringPi PWM library uses a gpio pin but I am using an i2c address.

http://wiringpi.com/reference/software-pwm-library/

http://wiringpi.com/reference/i2c-library/

If wiringPi is incapable of controlling this HAT does anyone have another C library they recommend for this HAT?

Thanks

User avatar
mikronauts
Posts: 2717
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Adafruit servo HAT - How to move servo?

Wed Nov 30, 2016 5:30 am

On the Adafruit product page for your hat, there is a "LEARN" link on the right hand side.

Click on that, and it will show you how
equals wrote:I recently put together a servo HAT https://www.adafruit.com/products/2327 and I am not quite sure how to move a servo that I have attached to it. I setup i2c and have the address of 0x40 for the HAT. I have been using wiringPi for all of my other sensors and would like to use it for the servo HAT but I am unsure how to control it. Do I use the wiringPi i2c library or the wiringPi PWM library, or both? I am confused because the wiringPi PWM library uses a gpio pin but I am using an i2c address.

http://wiringpi.com/reference/software-pwm-library/

http://wiringpi.com/reference/i2c-library/

If wiringPi is incapable of controlling this HAT does anyone have another C library they recommend for this HAT?

Thanks
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

donburch
Posts: 9
Joined: Sat Dec 01, 2012 7:03 am

Re: Adafruit servo HAT - How to move servo?

Tue Dec 13, 2016 1:15 pm

equals wrote:I recently put together a servo HAT https://www.adafruit.com/products/2327 and I am not quite sure how to move a servo that I have attached to it.
...
Do I use the wiringPi i2c library or the wiringPi PWM library, or both? I am confused because the wiringPi PWM library uses a gpio pin but I am using an i2c address.
Hi equals
I have recently bought a Servo HAT as well, and found that Adafruit's tutorial is great ... until it gives you the OLD location for the driver software - and then the screen shots do NOT match the software you download and use ! Extremely confusing :?

I'm pretty new to this myself, but my understanding is that:
  • - PWM is used to operate servos
    - The Raspberry Pi has only one GPIO pin which natively does PWM (pin 18)
    - hence you need to use an expansion board (such as the servo HAT) to control multiple servos
    - if the Servo HAT used one GPIO pin for each servo, you would quickly use all of the GPIO pins
    - SO... the Servo HAT uses I2C to receive instructions from the raspberry Pi, and
    - the Servo HAT does its bit of magic to drive up to 16 servos without the Raspberry Pi needing to manage all the details
Specificly, you need to use I2C to send commands to the Servo HAT. I guess the wiringPi i2c library will work, but you may need to do some research to find the correct format for the command to send to the Servo HAT.

I found that https://www.adafruit.com/products/815 (Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface - PCA9685) is essentially the same board but without the Raspbery Pi GPIO connector (and https://www.adafruit.com/products/1411 is the same but with connectors for an arduino). They all use the PCA9685 chip, and the same driver software. By following the directions for installing the adafruit driver in https://learn.adafruit.com/adafruit-16- ... it-library I got my servoHAT working easily.

semucon
Posts: 1
Joined: Fri Sep 01, 2017 2:45 pm

Re: Adafruit servo HAT - How to move servo?

Sun Sep 10, 2017 4:05 pm

For what it's worth, I struggled a little to understand what control inputs were need for specific servos, relative to the somewhat more intuitive Servo.write() method in the Arduino libraries for the PCA9685 shield. To help me understand servo control in terms of maximum and minimum PWM pulse widths I knocked up the following example for an SG90 servo (as included in many educational kits), adapted from the RPi library example:

Code: Select all

# Demo of using RPi Adafruit PWM hat for servo control using SG90 servo
# Need to be careful with min and max pulse width values otherwise SG90
# goes crazy

from __future__ import division
from time import sleep
#import RPi.GPIO as GPIO
import Adafruit_PCA9685

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

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

# Alternatively specify a different address and/or bus:
#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)

# Configure static values for PWM hat
resolution = 4096 # 12 bit resolution
frequency = 50 # pwm frequency in Hz
channel = 0 # using first of 16 channels

# Configure min, centre and max servo pulse lengths for SG90
# "Official" values are 1000, 1500 and 2000 microseconds but these only appear to give
# around 90 degrees of travel
servo_min = 800
servo_max = 2400
servo_centre = int((servo_min+servo_max)/2)
step_speed = 4

# This helper function sets servo position from pulse length in microseconds
def set_servo(channel, pulse_length):
    
    pwm.set_pwm(channel,0,int(pulse_length*frequency*resolution/1000000))

try:

    pwm.set_pwm_freq(frequency)
    set_servo(channel, servo_centre) 

    while True:

        # move from central to max
        print('Moving servo from centre to max {0}'.format( servo_max)) 
        for i in range (servo_centre, servo_max, step_speed):
            set_servo(channel,i)
        sleep(.5)
        
        # Move from max to min
        print('Moving servo from max {0} to min {1}'.format(servo_max, servo_min)) 
        for i in range (servo_max, servo_min, step_speed * -1):
            set_servo(channel,i)
        sleep(.5)

        # move from min back to centre    
        print('Moving servo from min {0} to centre'.format(servo_min)) 
        for i in range (servo_min, servo_centre, step_speed):
            set_servo(channel,i)
        sleep(.5)

except KeyboardInterrupt:
    print ("\nKeyboard Interrupt!\n") 

finally:
    set_servo(channel, servo_centre)       
    pwm = None
    #GPIO.cleanup()

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

Re: Adafruit servo HAT - How to move servo?

Sun Sep 10, 2017 8:21 pm

I use the PCA9685 chip for all my PWM controls most commonly controlling lots of servos at once like a ant robot with 13 servos.

I have now wrote my own drivers libraries in Python to control the chip but they probably need a bit if cleaning up and commenting out to be more user friendly before I would make them public. I first started using the Adafruit python libraries see https://github.com/adafruit/Adafruit_Python_PCA9685

I did just google "C PCA9685" and found this as first hit https://github.com/TeraHz/PCA9685 and also found that others have used this https://github.com/Reinbert/pca9685

You control an analog servo by setting a freq of 50hz to talk to it and then the timing of the duty cycle will set the position see http://www.jameco.com/jameco/workshop/h ... -work.html

Return to “Automation, sensing and robotics”