Ce tutoriel fait partie de: Commande de 16 ou 32 servomoteurs avec PCA9685
Cette collection de tutoriels vidéo vous aide à contrôler 32 servomoteurs ou plus à l'aide d'Arduino UNO, Nano, Mini ou ESP32. Tous les codes sont fournis.
Contrôlez 16 servomoteurs à l'aide d'un module PCA9685 et du croquis Arduino V2 #1 : Un par un
Dans ce tutoriel, nous allons apprendre à contrôler jusqu'à 16 servomoteurs en utilisant un module PCA9685 et un Arduino. Le PCA9685 est un contrôleur PWM à 16 canaux et 12 bits qui permet un contrôle précis des servomoteurs. En suivant ce guide, vous pourrez contrôler individuellement chaque servomoteur et les régler à des angles spécifiques, réalisant ainsi une variété de mouvements robotiques.
Nous commencerons par discuter des composants matériels dont vous aurez besoin pour ce projet, suivis d'instructions de câblage détaillées. Ensuite, nous passerons à travers le code étape par étape, en mettant en évidence les identifiants clés et leurs rôles dans le contrôle des servomoteurs. Pour une compréhension plus claire, vous voudrez peut-être vous référer à la vidéo accompagnante (dans la vidéo à :00).
Matériel expliqué
Le composant principal de ce projet est le module PCA9685, qui est responsable de la génération de signaux PWM pour contrôler les servos. Chaque servo est connecté à l'un des 16 canaux du PCA9685, permettant un contrôle indépendant. Le module communique avec l'Arduino en utilisant le protocole I2C, nécessitant seulement deux fils : SDA et SCL.
En plus du PCA9685, vous aurez besoin d'une carte Arduino, de 16 servomoteurs et d'une alimentation externe. L'alimentation externe est cruciale car l'Arduino seul peut ne pas fournir suffisamment de courant pour alimenter tous les servos simultanément. Chaque servo fonctionne généralement à 5V, donc assurez-vous que votre alimentation correspond à cette exigence.
Détails de la fiche technique
| Fabricant | NXP Semiconducteurs |
|---|---|
| Numéro de pièce | PCA9685 |
| Tension logique/IO | 2,3 - 5,5 V |
| Tension d'alimentation | 5 V |
| Courant de sortie (par canal) | 25 mA |
| Directives sur la fréquence PWM | 60 Hz |
| Seuils logiques d'entrée | 0,3VCC (bas) / 0,7VCC (haut) |
| Chute de tension / RDS(on)/ saturation | - |
| Limites thermiques | - |
| Colis | TSSOP-28 / VQFN-28 |
| Remarques / variantes | - |
- Connectez une alimentation externe de 5V pour les servos.
- Utilisez I2C pour la communication, en connectant SDA à A4 et SCL à A5 sur l'Arduino.
- Assurez-vous que tous les masses sont communes entre l'Arduino et le PCA9685.
- Ajustez les largeurs d'impulsion en fonction des servomoteurs spécifiques utilisés.
- Assurez-vous d'un refroidissement adéquat pour le PCA9685 si vous alimentez de nombreux servos simultanément.
Instructions de câblage
Pour connecter le module PCA9685 à l'Arduino et aux servomoteurs, commencez par relier l'alimentation externe 5V au terminal V+ du PCA9685. Connectez la masse de l'alimentation au terminal de masse du PCA9685 ainsi qu'à la masse de l'Arduino.
Ensuite, connectez les broches SDA et SCL du PCA9685 aux broches correspondantes sur l'Arduino (SDA sur A4 et SCL sur A5). Chaque moteur servo aura trois fils : la terre (généralement noir ou brun), VCC (typiquement rouge) et le signal (souvent jaune ou blanc). Connectez le fil de terre de chaque servo à la borne de terre du PCA9685, le fil VCC à la borne V+ et le fil de signal aux canaux respectifs (0-15) sur le PCA9685. Assurez-vous de connecter le fil de signal dans le bon ordre pour chaque servo.
Exemples de code et guide étape par étape
Nous allons maintenant passer en revue le code qui contrôle les servomoteurs un par un. Le code commence par importer les bibliothèques nécessaires pour la communication I2C et le module PCA9685. L'extrait suivant initialise l'objet PCA9685 :
#include
#include
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // Initialize PCA9685 Dans la fonction de configuration, nous initialisons le moniteur série et définissons la fréquence PWM pour les servos :
void setup() {
Serial.begin(9600); // Start serial communication
pwm.begin(); // Initialize PCA9685
pwm.setPWMFreq(60); // Set frequency to 60 Hz for servos
}La boucle principale contient deux boucles imbriquées : la boucle externe itère à travers chacun des 16 servos, tandis que la boucle interne change progressivement l'angle de 0 à 180 degrés :
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
}Cette structure permet à chaque servo de se déplacer vers son angle désigné par des incréments de 10 degrés, offrant des transitions fluides. La fonctionangleToPulse(int ang)convertit les valeurs d'angle en largeurs d'impulsions appropriées pour les servos :
int angleToPulse(int ang) {
int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX); // Map angle to pulse width
return pulse; // Return pulse width
}Cette fonction est essentielle pour traduire l'angle désiré en un signal PWM que le PCA9685 peut comprendre et transmettre aux servos. Pour plus de détails, n'oubliez pas que le code complet se charge en dessous de l'article.
Démonstration / À quoi s'attendre
Une fois que tout est correctement câblé et que le code est téléchargé, vous devriez voir chaque servo se déplacer vers ses angles respectifs un après l'autre. Si vous rencontrez des problèmes, vérifiez vos connexions et assurez-vous d'avoir une source d'alimentation stable pour les servos. Si les servos ne réagissent pas comme prévu, vérifiez les signaux PWM envoyés via le PCA9685.
Horodatages vidéo
- Détails du module avec la puce PCA9685
- 06:14 Ajout de la bibliothèque nécessaire pour le PCA9685
- 07:14 Chargement du code d'exemple
- 07:35 Code expliqué
- 11:31 Code Arduino simplifié pour PCA9685
- 12:00 Trouver la valeur minimale et maximale pour votre servo
- 18:27 Cartographie de l'angle d'impulsion à la largeur d'impulsion
- 20:05 Création d'une méthode séparée pour le mapping
- 20:55 Utilisation d'une boucle for pour tester tous les angles de cartographie
Ce tutoriel fait partie de: Commande de 16 ou 32 servomoteurs avec PCA9685
- Code Arduino et vidéo pour le contrôleur de servomoteurs PCA9685 16 canaux 12 bits V1
- Contrôler 16 moteurs servo à l'aide d'un module PCA9685 et d'un croquis Arduino V2 : Contrôle individuel des servos
- Controlling 16 Servo Motors Using a PCA9685 Module and Arduino V2 Sketch #3: All Servos Together
- Contrôler un moteur servo 32 en utilisant un module PCA9685 et un croquis Arduino V3 #1 : Tous les servos ensemble
- Contrôler un moteur servo 32 à l'aide d'un module PCA9685 et d'un ESP32 V4
- Contrôlez 32 servomoteurs via Wi-Fi en utilisant ESP32 et PCA9685 depuis un ordinateur de bureau ou un téléphone mobile V5
/*
* 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;
}
/*
* 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;
}
Ressources et références
Aucune ressource pour le moment.
Fichiers📁
Aucun fichier disponible.