Search Code

Msimbo wa Arduino na Video kwa Kidhibiti cha Servo cha PCA9685 cha Idhaa 16 cha 12-Bit V1

Msimbo wa Arduino na Video kwa Kidhibiti cha Servo cha PCA9685 cha Idhaa 16 cha 12-Bit V1

Katika mafunzo haya, tutachunguza jinsi ya kutumia kidhibiti cha servo cha PCA9685 cha chaneli 16 na biti 12 kutoka NXP Semiconductor. Moduli hii hukuruhusu kudhibiti hadi servo 16 au kufifisha kundi la LED kwa usahihi kwa kutumia moduli ya upana wa pigo (PWM). Kufikia mwisho wa mafunzo haya, utakuwa na mpangilio unaofanya kazi ambao unaweza kudhibiti servo nyingi kivyake au kwa wakati mmoja.

PCA9685 module-0

Ili kufafanua maudhui ya mafunzo zaidi, nakuhimiza utazame video inayoandamana (katika video saa 00:00) kwa maonyesho ya kuona ya mpangilio na mchakato wa kuandika msimbo.

Vifaa Vimeelezwa

Moduli ya PCA9685 ni bodi ndogo inayoweza kudhibiti servo nyingi kupitia mawasiliano ya I2C. Ina chaneli 16, inayokuruhusu kuunganisha hadi servo 16, kila moja ikiwa na ishara yake ya kudhibiti. Moduli hii inafanya kazi kwa usambazaji wa nishati ya 5V na imeundwa kushughulikia ishara za PWM, ambazo ni muhimu kwa kudhibiti nafasi ya servo kwa usahihi.

Bodi inajumuisha pini maalum za nishati (VCC), ardhi (GND), na mawasiliano (SDA na SCL). Pini ya SDA hutumiwa kwa usafirishaji wa data, wakati pini ya SCL ni ishara ya saa, zote mbili zinaunganishwa kwenye pini za analogi za Arduino A4 na A5, mtawalia. Mpangilio huu unahakikisha mawasiliano ya kuaminika kati ya Arduino na moduli ya PCA9685.

Maelezo ya Datasheet

MtengenezajiNXP Semiconductor
Nambari ya sehemuPCA9685
Voltage ya Logic/IO3.3 V hadi 5.5 V
Voltage ya usambazaji2.3 V hadi 5.5 V
Mkondo wa pato (kwa kila chaneli)25 mA max
Mkondo wa kilele (kwa kila chaneli)100 mA max
Mwongozo wa mzunguko wa PWM24 Hz hadi 1.6 kHz
Viwango vya kizingiti cha mantiki ya kuingiza0.3 V (chini) / 0.7 V (juu)
Kushuka kwa voltage / RDS(on) / kueneza0.5 V max
Mipaka ya joto-40 °C hadi 125 °C
KifurushiHTSSOP-28
Vidokezo / lahajaKidhibiti cha PWM cha chaneli 16

  • Hakikisha usambazaji wa nishati ya 5V na mkondo wa kutosha (1A inapendekezwa).
  • Usiweke nishati kwenye servo moja kwa moja kutoka Arduino ili kuepuka uharibifu.
  • Tumia pini sahihi za I2C: SDA kwa A4 na SCL kwa A5.
  • Rekebisha maadili ya upana wa pigo kulingana na servo zako maalum.
  • Angalia wiring kwa polarity sahihi: GND, VCC, na ishara.
  • Zingatia kupoeza kwa joto kwa matumizi ya mkondo wa juu.

Maagizo ya Wiring

Arduino wiring for PCA9685 to control 16 servo motors
Arduino wiring for PCA9685 to control 16 servo motors

Ili kuunganisha PCA9685 kwenye Arduino yako, anza kwa kuunganisha nishati na ardhi. Unganisha pini ya VCC kwenye PCA9685 kwenye pato la 5V kwenye Arduino. Kisha unganisha pini ya GND kwenye PCA9685 kwenye GND kwenye Arduino. Ifuatayo, unganisha pini ya SDA kwenye PCA9685 kwenye pini A4 kwenye Arduino, na pini ya SCL kwenye pini A5.

Kwa servo, unganisha waya wa ishara kwenye chaneli inayolingana kwenye PCA9685 (kwa mfano, CH0 kwa servo ya kwanza), waya wa nishati kwenye usambazaji wa nishati tofauti (kwani servo zinaweza kuhitaji mkondo zaidi kuliko Arduino inavyoweza kutoa), na waya wa ardhi kwenye ardhi ya pamoja inayoshirikiwa na PCA9685. Hakikisha kwamba waya za ishara, nishati, na ardhi zimepangwa kwa usahihi ili kuepuka kuharibu vipengele vyako.

Mifano ya Msimbo na Mwongozo

Katika sehemu ya usanidi wa msimbo, tunaanzisha moduli ya PCA9685 kwa pwm.begin() na kuweka mzunguko wa PWM kwa pwm.setPWMFreq(60);. Hii inaweka mzunguko wa mawasiliano kwa servo.

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
}

Ndani ya kitanzi, tunadhibiti servo kwa kuweka maadili ya PWM yanayolingana na pembe zinazohitajika. Kazi angleToPulse(int ang) inapanga pembe kwenye upana wa pigo unaofaa, ambayo ni muhimu kwa nafasi sahihi ya servo.

void loop() {
  for( int angle =0; angle<181; angle +=20){
    delay(500);
    pwm.setPWM(0, 0, angleToPulse(angle) );
  }
}

Hatimaye, kazi angleToPulse(int ang) inabadilisha pembe kuwa upana wa pigo kwa kutumia urefu wa chini na wa juu wa pigo uliofafanuliwa. Hii inakuruhusu kudhibiti nafasi ya servo kwa urahisi kulingana na pembe unayotaka kufikia.

int angleToPulse(int ang){
   int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);
   Serial.print("Angle: ");Serial.print(ang);
   Serial.print(" pulse: ");Serial.println(pulse);
   return pulse;
}

Maonyesho / Nini cha Kutarajia

Mara tu kila kitu kimeunganishwa kwa usahihi na msimbo umepakiwa, unapaswa kuona servo ikisonga kupitia pembe zilizobainishwa kwa nyongeza za digrii 20. Ikiwa servo haifanyi kazi kama inavyotarajiwa, angalia wiring kwa miunganisho sahihi na uhakikishe kwamba usambazaji wa nishati unatosha (katika video saa 12:30).

Vidokezo vya Muda wa Video

  • 00:00 - Utangulizi wa PCA9685
  • 02:30 - Maagizo ya wiring
  • 05:00 - Mwongozo wa msimbo
  • 10:15 - Maonyesho ya udhibiti wa servo
  • 12:30 - Kutatua matatizo ya kawaida

Bilder

PCA9685 module-0
PCA9685 module-0
PCA9685 module-1
PCA9685 module-1
PCA9685 module-2
PCA9685 module-2
PCA9685 module-3
PCA9685 module-3
PCA9685 module
PCA9685 module
Arduino wiring for PCA9685 to control 16 servo motors
Arduino wiring for PCA9685 to control 16 servo motors
36-PCA9685 video V1: Arduino Code to control 16 servo motor, Basic
Språk: C++
/*
 * Original source: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * This is the Arduino code for the PCA9685 16-channel servo controller.
 * Watch the video for details and demo: http://youtu.be/y8X9X10Tn1k
 *  get code and wiring diagram from http://robojax.com/RTJ27
 
 * Watch the video for this code: 
 * 
 * Related Videos:
V5 video of PCA9685 32 Servo with ESP32 with WiFi: https://youtu.be/bvqfv-FrrLM
V4 video of PCA9685 32 Servo with ESP32 (no WiFi): https://youtu.be/JFdXB8Za5Os
V3 video of PCA9685: how to control 32 servo motors: https://youtu.be/6P21wG7N6t4
V2 Video of PCA9685: 3 different ways to control servo motors: https://youtu.be/bal2STaoQ1M
V1 Video introduction to PCA9685 to control 16 servos: https://youtu.be/y8X9X10Tn1k

 * Written by Ahmad Shamshiri for Robojax Video channel: www.Robojax.com
 * Date: December 16, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code, given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * This code has been downloaded from https://robojax.com
 * 
 */
/*************************************************** 
  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, that's 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  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 // 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

  //yield();
}

// the code inside loop() has been updated by Robojax
void loop() {


  for( int angle =0; angle<181; angle +=20){
    delay(500);
    pwm.setPWM(0, 0, angleToPulse(angle) );
  }

  delay(1000);
 
}

/*
 * angleToPulse(int ang)
 * gets angle in degrees and returns the pulse width.
 * Also prints the value on the serial monitor.
 * Written by Ahmad Shamshiri for Robojax, Robojax.com
 */
int angleToPulse(int ang){
   int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max 
   Serial.print("Angle: ");Serial.print(ang);
   Serial.print(" pulse: ");Serial.println(pulse);
   return pulse;
}
37-PCA9685 video V1: Arduino Code with mapping PWM
Språk: C++
/*
 * Original source: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * This is the Arduino code for the PCA9685 16-channel servo controller.
we have simple code with mapping PWM for simplicity explained in the video at 18:27

 * Watch the video for details and demo: http://youtu.be/y8X9X10Tn1k
 get this code and wiring from for this video:  http://robojax.com/RJT27

 * Written by Ahmad Nejrabi for Robojax Video channel: www.Robojax.com
 * Date: December 15, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code, given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * 
 */
/*************************************************** 
  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, that's 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  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 // 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

  //yield();
}

// the code inside loop() has been updated by Robojax
void loop() {

    pwm.setPWM(0, 0, 125 );
	delay(500);
    pwm.setPWM(0, 0, 255 );
	delay(500);
    pwm.setPWM(0, 0, 450 );
	delay(500);
    pwm.setPWM(0, 0, 575 );
	delay(500);	

 
}

Tänker att du kanske behöver

Resurser och referenser

Inga resurser än.

Filer📁

Arduino-bibliotek (zip)

Andra filer