Suchcode

Steuere 16 Servomotoren mit einem PCA9685-Modul und Arduino V2 Sketch #1: Eins nach dem anderen

Steuere 16 Servomotoren mit einem PCA9685-Modul und Arduino V2 Sketch #1: Eins nach dem anderen

In diesem Tutorial lernen wir, wie man bis zu 16 Servomotoren mit einem PCA9685-Modul und einem Arduino steuert. Der PCA9685 ist ein 16-Kanal, 12-Bit PWM-Controller, der eine präzise Steuerung von Servomotoren ermöglicht. Indem Sie diese Anleitung befolgen, können Sie jeden Servomotor einzeln steuern und auf bestimmte Winkel einstellen, um eine Vielzahl von robotischen Bewegungen zu erreichen.

Wir werden zunächst die Hardwarekomponenten besprechen, die Sie für dieses Projekt benötigen, gefolgt von detaillierten Verdrahtungsanweisungen. Danach werden wir den Code Schritt für Schritt durchgehen und wichtige Bezeichner sowie ihre Rollen bei der Steuerung der Servomotoren hervorheben. Für ein besseres Verständnis möchten Sie vielleicht auf das begleitende Video (im Video bei :00) verweisen.

Hardware erklärt

Die primäre Komponente in diesem Projekt ist das PCA9685-Modul, das für die Erzeugung von PWM-Signalen zur Steuerung der Servos verantwortlich ist. Jeder Servo ist mit einem der 16 Kanäle des PCA9685 verbunden, was eine unabhängige Steuerung ermöglicht. Das Modul kommuniziert mit dem Arduino über das I2C-Protokoll und benötigt nur zwei Kabel: SDA und SCL.

Neben dem PCA9685 benötigen Sie ein Arduino-Board, 16 Servomotoren und ein externes Netzteil. Das externe Netzteil ist entscheidend, da das Arduino allein möglicherweise nicht genügend Strom bereitstellen kann, um alle Servos gleichzeitig zu betreiben. Jeder Servo arbeitet typischerweise bei 5V, stellen Sie also sicher, dass Ihr Netzteil diese Anforderung erfüllt.

Datenblattdetails

HerstellerNXP Halbleiter
TeilenummerPCA9685
Logik/IO-Spannung2,3 - 5,5 V
Versorgungsspannung5 V
Ausgangsstrom (pro Kanal)25 mA
PWM-Frequenzanleitung60 Hz
Eingangslogikschwellen0,3VCC (niedrig) / 0,7VCC (hoch)
Spannungsabfall / RDS(on)/ Sättigung-
Thermische Grenzen-
PaketTSSOP-28 / VQFN-28
Hinweise / Varianten-
  • Schließen Sie ein externes 5V-Netzteil für die Servos an.
  • Verwende I2C zur Kommunikation, indem du SDA mit A4 und SCL mit A5 auf dem Arduino verbindest.
  • Stellen Sie sicher, dass alle Erdungen zwischen dem Arduino und dem PCA9685 gemeinsam sind.
  • Passen Sie die Pulsweiten entsprechend den verwendeten Servos an.
  • Halten Sie die richtige Kühlung für den PCA9685 aufrecht, wenn Sie viele Servos gleichzeitig betreiben.

Verdrahtungsanweisungen

Um das PCA9685-Modul an das Arduino und die Servomotoren anzuschließen, beginnen Sie damit, das externe 5V-Netzteil an den V+-Anschluss des PCA9685 anzuschließen. Schließen Sie die Masse des Netzteils an den Masseanschluss des PCA9685 sowie an die Masse des Arduino an.

Als nächstes verbinden Sie die SDA- und SCL-Pins des PCA9685 mit den entsprechenden Pins des Arduino (SDA mit A4 und SCL mit A5). Jeder Servomotor hat drei Drähte: Masse (normalerweise schwarz oder braun), VCC (typischerweise rot) und Signal (häufig gelb oder weiß). Verbinden Sie den Massedraht jedes Servos mit dem Masseanschluss des PCA9685, den VCC-Draht mit dem V+-Anschluss und den Signaldraht mit den jeweiligen Kanälen (0-15) des PCA9685. Stellen Sie sicher, dass Sie den Signaldraht in der richtigen Reihenfolge für jeden Servo anschließen.

Codebeispiele und Anleitung

Wir werden nun den Code, der die Servomotoren steuert, eins nach dem anderen durchgehen. Der Code beginnt mit dem Import der notwendigen Bibliotheken für die I2C-Kommunikation und das PCA9685-Modul. Der folgende Auszug initialisiert das PCA9685-Objekt:

#include 
#include 

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // Initialize PCA9685

Im Setup-Function initialisieren wir den seriellen Monitor und setzen die PWM-Frequenz für die Servos:

void setup() {
  Serial.begin(9600); // Start serial communication
  pwm.begin(); // Initialize PCA9685
  pwm.setPWMFreq(60); // Set frequency to 60 Hz for servos
}

Die Hauptschleife enthält zwei verschachtelte Schleifen: Die äußere Schleife iteriert über jeden der 16 Servos, während die innere Schleife allmählich den Winkel von 0 bis 180 Grad ändert.

void loop() {
  for(int i=0; i<16; i++) {
    for(int angle = 0; angle<181; angle += 10) {
      delay(50); // Wait for servo to move
      pwm.setPWM(i, 0, angleToPulse(angle)); // Set servo position
    }
  }
  delay(1000); // Wait before repeating
}

Diese Struktur ermöglicht es jedem Servo, in Schritten von 10 Grad zu seinem vorgesehenen Winkel zu bewegen, was sanfte Übergänge gewährleistet. Die FunktionangleToPulse(int ang)wandelt Winkelwerte in Pulsbreiten um, die für die Servos geeignet sind:

int angleToPulse(int ang) {
   int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX); // Map angle to pulse width
   return pulse; // Return pulse width
}

Diese Funktion ist entscheidend dafür, den gewünschten Winkel in ein PWM-Signal zu übersetzen, das der PCA9685 verstehen und an die Servos übertragen kann. Für weitere Details denken Sie daran, dass der vollständige Code unter dem Artikel geladen wird.

Demonstration / Was zu erwarten ist

Sobald alles korrekt verkabelt ist und der Code hochgeladen wurde, sollten Sie sehen, wie jeder Servo nacheinander zu seinen jeweiligen Winkeln bewegt wird. Wenn Sie auf Probleme stoßen, überprüfen Sie Ihre Verbindungen und stellen Sie sicher, dass Sie eine stabile Stromversorgung für die Servos haben. Wenn die Servos nicht wie erwartet reagieren, überprüfen Sie die PWM-Signale, die über den PCA9685 gesendet werden.

Videountertitel

  • 00:00 Einzelheiten des Moduls mit dem Chip PCA9685
  • 06:14 Hinzufügen der benötigten Bibliothek für den PCA9685
  • 07:14 Beispielcode wird geladen
  • 07:35 Code erklärt
  • 11:31 Vereinfachter Arduino-Code für PCA9685
  • 12:00 Bestimmung des Minimal- und Maximalwerts für Ihren Servoantrieb
  • 18:27 Pulswinkel auf Pulsbreite abbilden
  • 20:05 Erstellung einer separaten Methode für das Mapping
  • 20:55 Verwendung einer Schleife, um alle Winkel für die Zuordnung zu testen
267-PCA9685 Video V2, Arduino Code-1 to run servo one by one all servos from 0 to 180°
Sprache: C++
/*
 * Original source: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * PCA9685 Video V2, Arduino Code-1 
 * This is the Arduino code PAC6985 16 channel servo controller
 * watch the video for details (V1) and demo http://youtu.be/y8X9X10Tn1k
 *  This code is #1 for V2 Video Watch the video :https://youtu.be/bal2STaoQ1M
get this code and wiring from https://robojax.com/RTJ243
 *  I have got 3 codes as follow:
   #1-Arduino Code to run one by one all servos from 0 to 180°   
   #2-Arduino Code to control specific servos with specific angle
   #3-Arduino Code to run 2 or all servos at together
   
 * Written/updated by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
 * Date: Dec 16, 2017, in Ajax, Ontario, Canada

 * Watch 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 Servo  https://youtu.be/y8X9X10Tn1k

 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * this code has been downloaded from https://robojax.com


or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/*************************************************** 
  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!
// Watch video V1 to understand the two lines below: http://youtu.be/y8X9X10Tn1k
#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() {

//watch video for details: https://youtu.be/bal2STaoQ1M
for(int i=0; i<16; i++)
  {
    for( int angle =0; angle<181; angle +=10){
      delay(50);
        pwm.setPWM(i, 0, angleToPulse(angle) );
        // see YouTube video for details (robojax)
       
    }
 
  }
 
  // robojax PCA9865 16 channel Servo control
  delay(1000);// wait for 1 second
 
}

/*
/* angleToPulse(int ang)
 * @brief gets angle in degree and returns the pulse width
 * @param "ang" is integer representing angle from 0 to 180
 * @return returns integer pulse width
 * Usage to use 65 degree: angleToPulse(65);
 * Written by Ahmad Shamshiri on Sep 17, 2019. 
 * in Ajax, Ontario, Canada
 * www.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;
}
268-PCA9685 Video V2, Arduino Code-2 to control specific servos with specific angles (one or more servos)
Sprache: C++
/*
 * Original sourse: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * PCA9685 Video V2, Arduino Code-2 
 * This is the Arduino code PAC6985 16 channel servo controller
 * watch the video for details (V1) and demo http://youtu.be/y8X9X10Tn1k
 *  This code is #2 for V2 Video Watch the video :https://youtu.be/bal2STaoQ1M
get codes and wring from https://robojax.com/RTJ243 
 *  I have got 3 codes as follow:
   #1-Arduino Code to run one by one all servos from 0 to 180°   
   #2-Arduino Code to control specific servos with specific angle
   #3-Arduino Code to run 2 or all servos at together
 * 
 * Written/updated by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
 * Date: Dec 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 purpose only.
 * this code has been downloaded from https://robojax.com

 * Watch 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 Servo  https://youtu.be/y8X9X10Tn1k
 
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

****************************
Get early access to my videos via Patreon and have  your name mentioned at end of very 
videos I publish on YouTube here: http://robojax.com/L/?id=63 (watch until end of this video to list of my Patrons)
****************************

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#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!
// Watch video V1 to understand the two lines below: http://youtu.be/y8X9X10Tn1k
#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() {
//watch video for details: https://youtu.be/bal2STaoQ1M
for(int i=0; i<16; i++)
  {
    for( int angle =0; angle<181; angle +=10){
      delay(50);
        pwm.setPWM(5, 0, angleToPulse(angle) );
        pwm.setPWM(8, 0, angleToPulse(angle) ); 
        pwm.setPWM(15, 0, angleToPulse(angle) );         
    }
 
  }
 
// robojax PCA9865 16 channel Servo control
  delay(1000);
 
}

/*
 * angleToPulse(int ang)
 * gets angle in degree and returns the pulse width
 * also prints the value on seial 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;
}

Ressourcen & Referenzen

Noch keine Ressourcen vorhanden.

Dateien📁

Keine Dateien verfügbar.