Moteur à 5 fils :
Remarque : l’alimentation du moteur est fournie par un bloc 5 v 0,7A, et les masses de l’alim et de la carte Arduino et la carte de contrôle moteur reliées entre elles.
Détails techniques : http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino
Code pour que le moteur tourne dans un sens puis dans l’autre :
int Pin0 = 8;
int Pin1 = 9;
int Pin2 = 10;
int Pin3 = 11;
int _step = 0;
int tour=0;
boolean dir = false;// true=anti clockwise ; false = clockwise
void setup()
{
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
// initialize the serial port:
Serial.begin(9600);
}
void loop()
{
switch(_step){
case 0:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
case 1:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
break;
case 2:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 3:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 4:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 5:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 6:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 7:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
default:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
}
if(dir){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){ _step=7; } delay(1); tour++; if (tour>3000)
{tour=0;
if(dir){
dir=false;
}else{
dir=true;
}
}
Serial.print(tour);
Serial.print(" ");
}
Servo :
Le mode de commande d’un servomoteur est standardisé : on envoie sur son fil de commande une impulsion dont la durée correspond à l’angle désiré. Pour commander des servos il est nécessaire d’utiliser la librairie #include
Servo myservo : création d’un objet de type Servo, appelé ici myservo
myservo.attach(9) : Le fil de commande de ce servo sera connecté au PIN 9
myservo.write(90) : myservo.write(Angle) : Demander au servomoteur de se déplacer à l’angle désiré, soit de façon absolue en lui indiquant une valeur entière (90°) dans le premier cas, soit en lui passant le contenu d’une variable (Angle) compris entre 0 et 180, ce qui peut être utile par exemple pour donner une progressivité au déplacement en faisant varier l’angle d’un pas fixe (quelques degrés) par une boucle
myservo.read(Angle) : Pour lire la valeur de l’angle du servomoteur
Cas d’un servo à rotation continue :
La valeur d’angle permet de faire varier la vitesse du servo.
Normalement 90° = stop de 90° à 180° = dans un sens (vitesse proportionnelle au °) de 90° à 0° = dans le sens inverse
Pour tester ces valeurs utilisez le test servo sweep
Si le servo ne réponds pas aux commandes tester les valeurs des impulsions (qui doivent être entre 1000 et 2000µs <=> 1000 = 0° ; 2000 = 180°) tester grâce au sketch suivant :
#include <Servo.h>
int temps = 1500; //censé être à mi-chemin entre 1000 et 2000
Servo monServo;
void setup()
{
Serial.begin(9600);
Serial.println("Test impulsions Servo");
monServo.attach(2);
monServo.writeMicroseconds(temps);
}
void loop()
{ if (Serial.available())
{
char commande = Serial.read();
if (commande == 'a')
temps += 10; //ajout de 10µs au temps HAUT
else if (commande == 'd')
temps -= 10; //retrait de 10µs au temps HAUT
//on modifie la consigne du servo
monServo.writeMicroseconds(temps);
Serial.println(temps, DEC);
}
}
https://www.arduino.cc/en/Tutorial/Sweep
Fil du servo POWER (Orange ou Rouge) : connecté au +5v Arduino
Fil servo GROUND (Marron ou Noir) : Connecté à la masse de l’arduino
Fil servo SIGNAL (Jaune ou Blanc) : Connecté à un PIN DIGITAL de l’Arduino
// Servo 0 to 180
#include
Servo myservo; // create servo object to control a servo
void setup()
{
myservo.attach(9); // attaches the servo on digital pin 9
}
void loop()
{myservo.write(0); // tell servo to go to position 0
delay(1000); // waits 1s for the servo to reach the position
myservo.write(180); // tell servo to go to position 180
delay(1000); // waits 1s for the servo to reach the position
}
Gestion de l’arrêt sur une position :
// Servo 0 to 180
// Stop 5 sec
#include
Servo myservo; // create servo object to control a servo
int led = 13;
void setup()
{
myservo.attach(7); // attaches the servo on digital pin 7
pinMode(led, OUTPUT);
}
void loop()
{myservo.write(0); // tell servo to go to position 0
delay(1000); // waits 1s for the servo to reach the position
myservo.write(180); // tell servo to go to position 180
delay(1000); // waits 1s for the servo to reach the position
myservo.detach(); //Stops Servo
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5000);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
myservo.attach(7); // attaches the servo on digital pin 7
}
Autre librairie intéressante qui permet de faire varier la vitesse du servo :
Votre commentaire