Кодты іздеу

Ардуино коды және PCA9685 16-арналы 12-биттік серво контроллері V1 бейнежазбасы

Ардуино коды және PCA9685 16-арналы 12-биттік серво контроллері V1 бейнежазбасы

Бұл оқулықта біз NXP Semiconductor компаниясының PCA9685 16-арналы 12-биттік серво контроллерін қалай пайдалану керектігін қарастырамыз. Бұл модуль импульстік ені модуляциясын (PWM) қолдана отырып, 16 сервоны басқаруға немесе LED шоғырын дәлдікпен күңгірттеуге мүмкіндік береді. Осы оқулықтың соңында сізде бірнеше сервоны жеке немесе бір уақытта басқара алатын жұмыс істейтін қондырғы болады.

PCA9685 module-0

Оқулық мазмұнын одан әрі нақтылау үшін, қондырғы мен кодтау процесінің көрнекі демонстрациясын көру үшін ілеспе бейнені (00:00-дегі бейнеде) көруге кеңес беремін.

Аппараттық құрал түсіндірмесі

PCA9685 модулі - I2C байланысы арқылы бірнеше сервоны басқара алатын ықшам тақта. Ол 16 арнадан тұрады, бұл әрқайсысының өз басқару сигналы бар 16 сервоны қосуға мүмкіндік береді. Модуль 5В қуат көзімен жұмыс істейді және серволардың орнын дәл басқару үшін маңызды PWM сигналдарын өңдеуге арналған.

Тақтада қуат (VCC), жер (GND) және байланыс (SDA және SCL) үшін арнайы түйреуіштер бар. SDA түйреуіші деректерді беру үшін қолданылады, ал SCL түйреуіші сағат сигналы болып табылады, екеуі де Arduino-ның аналогтық A4 және A5 түйреуіштеріне сәйкесінше қосылады. Бұл қондырғы Arduino мен PCA9685 модулі арасында сенімді байланысты қамтамасыз етеді.

Деректер парағының мәліметтері

ӨндірушіNXP Semiconductor
Бөлшек нөміріPCA9685
Логика/IO кернеуі3.3 В - 5.5 В
Қуат кернеуі2.3 В - 5.5 В
Шығыс тогы (әр арна)25 мА макс
Шың тогы (әр арна)100 мА макс
PWM жиілігі бойынша нұсқау24 Гц - 1.6 кГц
Кіріс логикалық шектері0.3 В (төмен) / 0.7 В (жоғары)
Кернеу төмендеуі / RDS(on) / қанығу0.5 В макс
Жылулық шектер-40 °C - 125 °C
ҚаптамаHTSSOP-28
Ескертулер / нұсқалар16-арналы PWM контроллері

  • Жеткілікті токпен (1А ұсынылады) 5В қуат көзін қамтамасыз етіңіз.
  • Зақымдануды болдырмау үшін серволарды тікелей Arduino-дан қуаттандырмаңыз.
  • Дұрыс I2C түйреуіштерін қолданыңыз: SDA-ны A4-ке, SCL-ді A5-ке жалғаңыз.
  • Импульс ені мәндерін өз серволарыңызға сәйкес реттеңіз.
  • Сымдардың полярлығын тексеріңіз: GND, VCC және сигнал.
  • Жоғары ток қолданатын қолданбалар үшін жылу таратқышты қарастырыңыз.

Сымдарды жалғау нұсқаулары

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

PCA9685-ті Arduino-ға қосу үшін алдымен қуат пен жерді жалғаудан бастаңыз. PCA9685-тегі VCC түйреуішін Arduino-дағы 5V шығысына қосыңыз. Содан кейін PCA9685-тегі GND түйреуішін Arduino-дағы GND-ге қосыңыз. Әрі қарай, PCA9685-тегі SDA түйреуішін Arduino-дағы A4 түйреуішіне, ал SCL түйреуішін A5-ке қосыңыз.

Серволар үшін сигнал сымын PCA9685-тегі сәйкес арнаға қосыңыз (мысалы, бірінші серво үшін CH0), қуат сымын бөлек қуат көзіне (серволар Arduino бере алатыннан көбірек ток қажет етуі мүмкін), ал жер сымын PCA9685-пен ортақ жерге қосыңыз. Компоненттеріңізді зақымдамау үшін сигнал, қуат және жер сымдарының дұрыс тураланғанына көз жеткізіңіз.

Код мысалдары мен түсіндірме

Кодтың баптау бөлімінде біз PCA9685 модулін pwm.begin() арқылы инициализациялаймыз және PWM жиілігін pwm.setPWMFreq(60); арқылы орнатамыз. Бұл серволар үшін байланыс жиілігін орнатады.

void setup() {
  Serial.begin(9600);
  Serial.println("16 арналы Серво тесті!");
  pwm.begin();
  pwm.setPWMFreq(60);  // Аналогтық серволар ~60 Гц жаңартумен жұмыс істейді
}

Цикл ішінде біз қажетті бұрыштарға сәйкес PWM мәндерін орнату арқылы серволарды басқарамыз. angleToPulse(int ang) функциясы бұрышты тиісті импульс еніне келтіреді, бұл сервоның дәл позициялануы үшін маңызды.

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

Соңында, angleToPulse(int ang) функциясы анықталған минималды және максималды импульс ұзақтықтарын қолдана отырып, бұрышты импульс еніне түрлендіреді. Бұл қол жеткізгіңіз келетін бұрышқа негізделген сервоның орнын оңай басқаруға мүмкіндік береді.

int angleToPulse(int ang){
   int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);
   Serial.print("Бұрыш: ");Serial.print(ang);
   Serial.print(" импульс: ");Serial.println(pulse);
   return pulse;
}

Демонстрация / Не күтуге болады

Барлығы дұрыс жалғанып, код жүктелгеннен кейін, сервоның 20 градус қадамдармен көрсетілген бұрыштар арқылы қозғалатынын көруіңіз керек. Егер серво күтілгендей әрекет етпесе, сымдардың дұрыс жалғануын тексеріңіз және қуат көзінің жеткілікті екеніне көз жеткізіңіз (12:30-дағы бейнеде).

Бейне уақыт белгілері

  • 00:00 - PCA9685-ке кіріспе
  • 02:30 - Сымдарды жалғау нұсқаулары
  • 05:00 - Кодты түсіндіру
  • 10:15 - Серво басқару демонстрациясы
  • 12:30 - Жиі кездесетін мәселелерді шешу

Суреттер

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
Тіл: 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
Тіл: 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);	

 
}

Сізге қажет болуы мүмкін нәрселер

Ресурстар мен сілтемелер

Әлі ресурстар жоқ.

Файлдар📁

Arduino кітапханалары (zip)

Басқа файлдар