Adafruit pwm servo driver library описание библиотеки

Обновлено: 05.07.2024

Class that stores state and functions for interacting with PCA9685 PWM chip. More.

Assembly

Install the Servo Headers

Install 4 3x4 pin male headers into the marked positions along the edge of the board.

Pinouts

adafruit_products_815-06.jpg

There are two sets of control input pins on either side. Both sides of the pins are identical! Use whichever side you like, you can also easily chain by connecting up two side-by-side

  • GND - This is the power and signal ground pin, must be connected
  • VCC - This is the logic power pin, connect this to the logic level you want to use for the PCA9685 output, should be 3 - 5V max! It's also used for the 10K pullups on SCL/SDA so unless you have your own pullups, have it match the microcontroller's logic level too!
  • V+ - This is an optional power pin that will supply distributed power to the servos. If you are not using for servos you can leave disconnected. It is not used at all by the chip. You can also inject power from the 2-pin terminal block at the top of the board. You should provide 5-6VDC if you are using servos. If you have to, you can go higher to 12VDC, but if you mess up and connect VCC to V+ you could damage your board!
  • SCL - I2C clock pin, connect to your microcontrollers I2C clock line. Can use 3V or 5V logic, and has a weak pullup to VCC
  • SDA - I2C data pin, connect to your microcontrollers I2C data line. Can use 3V or 5V logic, and has a weak pullup to VCC
  • OE - Output enable. Can be used to quickly disable all outputs. When this pin is low all pins are enabled. When the pin is high the outputs are disabled. Pulled low by default so it's an optional pin!

There are 16 output ports. Each port has 3 pins: V+, GND and the PWM output. Each PWM runs completely independently but they must all have the same PWM frequency. That is, for LEDs you probably want 1.0 KHz but servos need 60 Hz - so you cannot use half for LEDs @ 1.0 KHz and half @ 60 Hz.

They're set up for servos but you can use them for LEDs! Max current per pin is 25mA.

There are 220 ohm resistors in series with all PWM Pins and the output logic is the same as VCC so keep that in mind if using LEDs.

Using the Adafruit Library

Since the PWM Servo Driver is controlled over I2C, its super easy to use with any microcontroller or microcomputer. In this demo we'll show using it with the Arduino IDE but the C++ code can be ported easily

To begin reading sensor data, you will need to install the Adafruit_PWMServo library (code on our github repository). It is available from the Arduino library manager so we recommend using that.

From the IDE open up the library manager.

adafruit_products_managelib.jpg

And type in adafruit pwm to locate the library. Click Install

adafruit_products_pwmlib.jpg

First make sure all copies of the Arduino IDE are closed.

Next open the Arduino IDE and select File->Examples->Adafruit_PWMServoDriver->Servo. This will open the example file in an IDE window.

adafruit_products_demo.jpg

If using a Breakout:

Connect the driver board and servo as shown on the previous page. Don't forget to provide power to both Vin (3-5V logic level) and V+ (5V servo power). Check the green LED is lit!

If using a Shield:

Plug the shield into your Arduino. Don't forget you will also have to provide 5V to the V+ terminal block. Both red and green LEDs must be lit.

If using a FeatherWing:

Plug the FeatherWing into your Feather. Don't forget you will also have to provide 5V to the V+ terminal block. Check the green LED is lit!

Servo pulse timing varies between different brands and models. Since it is an analog control circuit, there is often some variation between samples of the same brand and model. For precise position control, you will want to calibrate the minumum and maximum pulse-widths in your code to match known positions of the servo.

Find the Minimum:
Using the example code, edit SERVOMIN until the low-point of the sweep reaches the minimum range of travel. It is best to approach this gradually and stop before the physical limit of travel is reached.

Find the Maximum:
Again using the example code, edit SERVOMAX until the high-point of the sweep reaches the maximum range of travel. Again, is best to approach this gradually and stop before the physical limit of travel is reached.

Install Power Terminals

If you are chaining multiple driver boards, you only need a power terminal on the first one.

Member Function Documentation

Setups the I2C interface and hardware.

Parameters

prescaleSets External Clock (Optional)

Sets EXTCLK pin to use the external clock.

Parameters

prescaleConfigures the prescale value to be used by the external clock

Sets the PWM frequency for the entire chip, up to

Parameters

freqFloating point frequency that we will attempt to match

Sets the output mode of the PCA9685 to either open drain or push pull / totempole. Warning: LEDs with integrated zener diodes should only be driven in open drain mode.

Parameters

totempoleTotempole if true, open drain if false.

Gets the PWM output of one of the PCA9685 pins.

Parameters

numOne of the PWM output pins, from 0 to 15
Returns requested PWM output value

Sets the PWM output of one of the PCA9685 pins.

Parameters

numOne of the PWM output pins, from 0 to 15
onAt what point in the 4096-part cycle to turn the PWM output ON
offAt what point in the 4096-part cycle to turn the PWM output OFF

Helper to set pin PWM output. Sets pin without having to deal with on/off tick placement and properly handles a zero value as completely off and 4095 as completely on. Optional invert parameter supports inverting the pulse for sinking to ground.

Parameters

numOne of the PWM output pins, from 0 to 15
valThe number of ticks out of 4096 to be active, should be a value from 0 to 4095 inclusive.
invertIf true, inverts the output, defaults to 'false'

Reads set Prescale from PCA9685.

Returns prescale value

Sets the PWM output of one of the PCA9685 pins based on the input microseconds, output is not precise.

Parameters

numOne of the PWM output pins, from 0 to 15
MicrosecondsThe number of Microseconds to turn the PWM output ON

Setter for the internally tracked oscillator used for freq calculations.

Parameters

freqThe frequency the PCA9685 should use for frequency calculations

Getter for the internally tracked oscillator used for freq calculations.

Returns The frequency the PCA9685 thinks it is running at (it cannot introspect)

Adafruit PCA9685 PWM Servo Driver Library

This is a library for our Adafruit 16-channel PWM & Servo driver, shield or FeatherWing


Pick one up today in the adafruit shop!

These drivers use I2C to communicate, 2 pins are required to interface.

Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!

You will be redirected back to this guide once you sign in, and can then subscribe to this guide.

Since the PWM Servo Driver is controlled over I2C, its super easy to use with any microcontroller or microcomputer. In this demo we'll show using it with the Arduino IDE but the C++ code can be ported easily

To begin reading sensor data, you will need to install the Adafruit_PWMServo library (code on our github repository). It is available from the Arduino library manager so we recommend using that.

From the IDE open up the library manager.

adafruit_products_managelib.jpg

And type in adafruit pwm to locate the library. Click Install

adafruit_products_pwmlib.jpg

First make sure all copies of the Arduino IDE are closed.

Next open the Arduino IDE and select File->Examples->Adafruit_PWMServoDriver->Servo. This will open the example file in an IDE window.

adafruit_products_demo.jpg

If using a Breakout:

Connect the driver board and servo as shown on the previous page. Don't forget to provide power to both Vin (3-5V logic level) and V+ (5V servo power). Check the green LED is lit!

If using a Shield:

Plug the shield into your Arduino. Don't forget you will also have to provide 5V to the V+ terminal block. Both red and green LEDs must be lit.

If using a FeatherWing:

Plug the FeatherWing into your Feather. Don't forget you will also have to provide 5V to the V+ terminal block. Check the green LED is lit!

Servo pulse timing varies between different brands and models. Since it is an analog control circuit, there is often some variation between samples of the same brand and model. For precise position control, you will want to calibrate the minumum and maximum pulse-widths in your code to match known positions of the servo.

Find the Minimum:
Using the example code, edit SERVOMIN until the low-point of the sweep reaches the minimum range of travel. It is best to approach this gradually and stop before the physical limit of travel is reached.

Find the Maximum:
Again using the example code, edit SERVOMAX until the high-point of the sweep reaches the maximum range of travel. Again, is best to approach this gradually and stop before the physical limit of travel is reached.

Use caution when adjusting SERVOMIN and SERVOMAX. Hitting the physical limits of travel can strip the gears and permanently damage your servo. The Arduino "map()" function is an easy way to convert between degrees of rotation and your calibrated SERVOMIN and SERVOMAX pulse lengths. Assuming a typical servo with 180 degrees of rotation; once you have calibrated SERVOMIN to the 0-degree position and SERVOMAX to the 180 degree position, you can convert any angle between 0 and 180 degrees to the corresponding pulse length with the following line of code:

This guide was first published on Oct 16, 2012. It was last updated on Oct 16, 2012.

You will be redirected back to this guide once you sign in, and can then subscribe to this guide.

adafruit_products_ID815servo_LRG.jpg

Driving servo motors with the Arduino Servo library is pretty easy, but each one consumes a precious pin - not to mention some Arduino processing power. The Adafruit 16-Channel 12-bit PWM/Servo Driver will drive up to 16 servos over I2C with only 2 pins. The on-board PWM controller will drive all 16 channels simultaneously with no additional Arduino processing overhead. What's more, you can chain up to 62 of them to control up to 992 servos - all with the same 2 pins!

The Adafruit PWM/Servo Driver is the perfect solution for any project that requires a lot of servos.

adafruit_products_hexapod.jpg

Constructor & Destructor Documentation

Instantiates a new PCA9685 PWM driver chip with the I2C address on a TwoWire interface.

Parameters

addrThe 7-bit I2C address to locate this chip, default is 0x40

Instantiates a new PCA9685 PWM driver chip with the I2C address on a TwoWire interface.

Parameters

addrThe 7-bit I2C address to locate this chip, default is 0x40
i2cA reference to a 'TwoWire' object that we'll use to communicate with

Chaining Drivers

Multiple Drivers (up to 62) can be chained to control still more servos. With headers at both ends of the board, the wiring is as simple as connecting a 6-pin parallel cable from one board to the next.

adafruit_products_MultiBoard_bb-1024.jpg

Each board in the chain must be assigned a unique address. This is done with the address jumpers on the upper right edge of the board. The I2C base address for each board is 0x40. The binary address that you program with the address jumpers is added to the base I2C address.

To program the address offset, use a drop of solder to bridge the corresponding address jumper for each binary '1' in the address.

adafruit_products_2012_10_13_IMG_0692-1024.jpg

Board 0: Address = 0x40 Offset = binary 00000 (no jumpers required)
Board 1: Address = 0x41 Offset = binary 00001 (bridge A0 as in the photo above)
Board 2: Address = 0x42 Offset = binary 00010 (bridge A1)
Board 3: Address = 0x43 Offset = binary 00011 (bridge A0 & A1)
Board 4: Address = 0x44 Offset = binary 00100 (bridge A2)

In your sketch, you'll need to declare a separate pobject for each board. Call begin on each object, and control each servo through the object it's attached to. For example:

Solder all pins

Add Headers for Control

  • Forbreadboarduse, install headers on the bottom of the board.
  • For use withjumper wires, install the headers on top of the board.
  • For use with our6-pin cable, install the headers on top of the board.

Hooking it Up

The PWM/Servo Driver uses I2C so it take only 4 wires to connect to your Arduino:

  • +5v -> VCC (this is power for the BREAKOUT only, NOT the servo power!)
  • GND -> GND
  • Analog 4 -> SDA
  • Analog 5 -> SCL
  • +5v -> VCC (this is power for the BREAKOUT only, NOT the servo power!)
  • GND -> GND
  • Digital 20 -> SDA
  • Digital 21 -> SCL
  • +5v -> VCC (this is power for the BREAKOUT only, NOT the servo power!)
  • GND -> GND
  • SDA -> SDA
  • SCL -> SCL

adafruit_products_I2C_bb-1024.jpg

The VCC pin is just power for the chip itself. If you want to connect servos or LEDs that use the V+ pins, you MUST connect the V+ pin as well. The V+ pin can be as high as 6V even if VCC is 3.3V (the chip is 5V safe). We suggest connecting power through the blue terminal block since it is polarity protected.

Most servos are designed to run on about 5 or 6v. Keep in mind that a lot of servos moving at the same time (particularly large powerful ones) will need a lot of current. Even micro servos will draw several hundred mA when moving. Some High-torque servos will draw more than 1A each under load.

  • 5v 2A switching power supply
  • 5v 10A switching power supply
  • 4xAA Battery Holder - 6v with Alkaline cells. 4.8v with NiMH rechargeable cells.
  • 4.8 or 6v Rechargeable RC battery packs from a hobby store.
It is not a good idea to use the Arduino 5v pin to power your servos. Electrical noise and 'brownouts' from excess current draw can cause your Arduino to act erratically, reset and/or overheat.

adafruit_products_Connections_bb-1024.jpg

We have a spot on the PCB for soldering in an electrolytic capacitor. Based on your usage, you may or may not need a capacitor. If you are driving a lot of servos from a power supply that dips a lot when the servos move, n * 100uF where n is the number of servos is a good place to start - eg 470uF or more for 5 servos. Since its so dependent on servo current draw, the torque on each motor, and what power supply, there is no "one magic capacitor value" we can suggest which is why we don't include a capacitor in the kit.
Most servos come with a standard 3-pin female connector that will plug directly into the headers on the Servo Driver. Be sure to align the plug with the ground wire (usually black or brown) with the bottom row and the signal wire (usually yellow or white) on the top.

adafruit_products_Servo_bb-1024.jpg

Up to 16 servos can be attached to one board. If you need to control more than 16 servos, additional boards can be chained as described on the next page.

adafruit_products_AllServos_bb-1024.jpg

Public Member Functions

Detailed Description

Class that stores state and functions for interacting with PCA9685 PWM chip.

Читайте также: