Szukaj kodu

Kod Arduino i wideo dla kontrolera serw PCA9685 16-kanałowego 12-bitowego V1

Kod Arduino i wideo dla kontrolera serw PCA9685 16-kanałowego 12-bitowego V1

W tym samouczku omówimy, jak używać kontrolera serwomechanizmów PCA9685 z 16 kanałami i 12-bitową rozdzielczością od NXP Semiconductor. Moduł ten pozwala sterować nawet 16 serwomechanizmami lub precyzyjnie przyciemniać diody LED za pomocą modulacji szerokości impulsu (PWM). Po ukończeniu tego samouczka będziesz mieć działający układ, który umożliwi sterowanie wieloma serwomechanizmami indywidualnie lub jednocześnie.

PCA9685 module-0

Aby dodatkowo wyjaśnić treść samouczka, zachęcam do obejrzenia towarzyszącego filmu (na wideo w 00:00), który zawiera wizualną demonstrację procesu montażu i kodowania.

Objaśnienie sprzętu

Moduł PCA9685 to kompaktowa płytka, która umożliwia sterowanie wieloma serwomechanizmami poprzez komunikację I2C. Posiada 16 kanałów, co pozwala na podłączenie do 16 serwomechanizmów, każdy z własnym sygnałem sterującym. Moduł działa przy zasilaniu 5V i jest zaprojektowany do obsługi sygnałów PWM, które są niezbędne do precyzyjnego sterowania pozycją serwomechanizmów.

Płytka zawiera dedykowane piny zasilania (VCC), masy (GND) oraz komunikacji (SDA i SCL). Pin SDA służy do transmisji danych, a pin SCL to sygnał zegara; oba łączą się odpowiednio z pinami analogowymi A4 i A5 Arduino. Taka konfiguracja zapewnia niezawodną komunikację między Arduino a modułem PCA9685.

Szczegóły z karty katalogowej

ProducentNXP Semiconductor
Numer częściPCA9685
Napięcie logiczne/IO3,3 V do 5,5 V
Napięcie zasilania2,3 V do 5,5 V
Prąd wyjściowy (na kanał)maks. 25 mA
Prąd szczytowy (na kanał)maks. 100 mA
Zalecana częstotliwość PWM24 Hz do 1,6 kHz
Progi logiczne wejścia0,3 V (niski) / 0,7 V (wysoki)
Spadek napięcia / RDS(on) / nasyceniemaks. 0,5 V
Limity termiczne-40 °C do 125 °C
ObudowaHTSSOP-28
Uwagi / wariantyKontroler PWM z 16 kanałami

  • Zapewnij zasilanie 5V z wystarczającym prądem (zalecane 1A).
  • Nie zasilaj serwomechanizmów bezpośrednio z Arduino, aby uniknąć uszkodzeń.
  • Użyj prawidłowych pinów I2C: SDA do A4 i SCL do A5.
  • Dostosuj wartości szerokości impulsu do konkretnych serwomechanizmów.
  • Sprawdź okablowanie pod kątem prawidłowej polaryzacji: GND, VCC i sygnał.
  • Rozważ zastosowanie radiatora w aplikacjach o wysokim prądzie.

Instrukcje okablowania

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

Aby podłączyć PCA9685 do Arduino, zacznij od połączenia zasilania i masy. Podłącz pin VCC na PCA9685 do wyjścia 5V na Arduino. Następnie podłącz pin GND na PCA9685 do GND na Arduino. Kolejno podłącz pin SDA na PCA9685 do pinu A4 na Arduino, a pin SCL do pinu A5.

Dla serwomechanizmów podłącz przewód sygnałowy do odpowiedniego kanału na PCA9685 (np. CH0 dla pierwszego serwomechanizmu), przewód zasilający do osobnego zasilacza (ponieważ serwomechanizmy mogą wymagać większego prądu, niż może zapewnić Arduino), a przewód masy do wspólnej masy dzielonej z PCA9685. Upewnij się, że przewody sygnałowe, zasilające i masy są prawidłowo podłączone, aby uniknąć uszkodzenia komponentów.

Przykłady kodu i omówienie

W sekcji konfiguracji kodu inicjalizujemy moduł PCA9685 za pomocą pwm.begin() i ustawiamy częstotliwość PWM za pomocą pwm.setPWMFreq(60);. Ustawia to częstotliwość komunikacji dla serwomechanizmów.

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");
  pwm.begin();
  pwm.setPWMFreq(60);  // Analogowe serwa działają przy ~60 Hz aktualizacji
}

W pętli głównej sterujemy serwomechanizmami, ustawiając wartości PWM odpowiadające pożądanym kątom. Funkcja angleToPulse(int ang) mapuje kąt na odpowiednią szerokość impulsu, co jest niezbędne do precyzyjnego pozycjonowania serwomechanizmu.

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

Na koniec funkcja angleToPulse(int ang) przelicza kąt na szerokość impulsu, używając zdefiniowanych minimalnych i maksymalnych długości impulsów. Pozwala to łatwo sterować pozycją serwomechanizmu w zależności od pożądanego kąta.

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;
}

Demonstracja / Czego się spodziewać

Po prawidłowym podłączeniu wszystkiego i wgraniu kodu powinieneś zobaczyć, jak serwomechanizm porusza się przez określone kąty w odstępach co 20 stopni. Jeśli serwomechanizm nie zachowuje się zgodnie z oczekiwaniami, sprawdź okablowanie pod kątem prawidłowych połączeń i upewnij się, że zasilanie jest wystarczające (na wideo w 12:30).

Znaczniki czasu wideo

  • 00:00 - Wprowadzenie do PCA9685
  • 02:30 - Instrukcje okablowania
  • 05:00 - Omówienie kodu
  • 10:15 - Demonstracja sterowania serwomechanizmem
  • 12:30 - Rozwiązywanie typowych problemów

Obrazy

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
Język: 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
Język: 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);	

 
}

Rzeczy, których możesz potrzebować

Zasoby i odniesienia

Brak zasobów.

Pliki📁

Biblioteki Arduino (zip)

Inne pliki