用Arduino類庫驅動舵機並非一件難事,若是須要驅動不少電機,就須要要佔用更多的引腳,也會影響到Arduino的處理能力。專門的舵機驅動板很好的解決了這個問題。redis
此舵機驅動板使用PCA9685芯片,是16通道12bit PWM舵機驅動,用2個引腳經過I2C就能夠驅動16個舵機。不只如此,你還能夠經過級聯的方式最多級聯62個驅動板,總共能夠驅動992個舵機!oop
大多數的舵機設計電壓都是在5~6V,尤爲在多個舵機同時運行時,跟須要有大功率的電源供電。若是直接使用Arduino 5V引腳直接爲舵機供電,會出現一些難以預測的問題,因此咱們建議你能有個合適的外部電源爲驅動板供電。ui
驅動板與Arduino鏈接this
此PWM驅動板採用I2C方式,因此只須要4根線就能夠鏈接到Arduino設備:spa
爲驅動板分配地址設計
級聯的每一個驅動板都須要有一個惟一的訪問地址。每一個驅動板的初始I2C地址是0×40,能夠經過右上角的跳線修改I2C地址。用焊錫將一個跳線連上就表示一個二進制數字「1」。code
按如下方式鏈接blog
Arduino uno PCA9685
+5v -> VCC
GND -> GND
Analog 4 -> SDAAnalog 5 -> SCL
外接電源ci
爲驅動板分配地址it
級聯的每一個驅動板都須要有一個惟一的訪問地址。每一個驅動板的初始I2C地址是0×40,能夠經過右上角的跳線修改I2C地址。用焊錫將一個跳線連上就表示一個二進制數字「1」。
使用自帶的例程(只接2個舵機):
/***************************************************
This is an example for our Adafruit 16-channel PWM & Servo driver
Servo test - this will drive 16 servos, one after the other
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These displays use I2C to communicate, 2 pins are required to
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096)
// our servo # counter
//uint8_t servonum = 0;
void setup()
{
Serial.begin(9600);
Serial.println("16 channel Servo test!!!!");
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
}
// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
void setServoPulse(uint8_t n, double pulse)
{
double pulselength;
pulselength = 1000000; // 1,000,000 us per second
pulselength /= 60; // 60 Hz
Serial.print(pulselength); Serial.println(" us per period");
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(" us per bit");
pulse *= 1000;
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop()
{
// Drive each servo one at a time
//Serial.println(servonum);
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++)
{
pwm.setPWM(0, 0, pulselen);
pwm.setPWM(1, 0, pulselen);
}
delay(500);
for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--)
{
pwm.setPWM(0, 0, pulselen);
pwm.setPWM(1, 0, pulselen);
}
delay(500);
}