Arduino > Servomoteur

1.  Description

Un servomoteur est un moteur conçu pour générer un mouvement précis selon une commande externe. D'habitude, son mouvement est limité à un arc de 180 degrés.

La logithèque Servo  permet de contrôler plusieurs servomoteurs simultanément.

Si le servomoteur est de plus grande puissance, il est une charge lourde et doit avoir sa propre alimentation stabilisée entre 5V et 6V.

Il est important de noter que le PWM des broches 9 et 10 sont désactivées par la logithèque Servo.

Un servomoteur possède trois broches:

Servomoteurs Spikenzielabs


sparkfun_servomotor.pdf

1.1  Servomoteur à rotation continue

Il est possible d'acheter  ou de modifier  des servomoteurs pour qu'ils effectuent des rotations continues. Lorsqu'un moteur est modifier pour effectuer une rotation continue, on ne peut plus contrôler sa position mais uniquement sa vitesse.

2.  Circuit

Le branchement suivant requiert une alimentation stabilisée.

3.  Exemple : position aléatoire du servomoteur à chaque seconde

// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
#include <Chrono.h>

// Instanciate a Chrono object.
Chrono myChrono;

// INCLUDE SERVO LIBRARY.
#include <Servo.h>

// INSTANTION A SERVO OBJECT.
Servo myServo;

void setup() {
        // ATTACH THE SERVO TO PIN 9
        myServo.attach(9);
}

void loop() {
        // Use Chrono as a metronome with an interval of 1000 ms :
        if (myChrono.hasPassed(1000) ) { // elapsed(1000) returns 1 if 1000 ms have passed.
                myChrono.restart();  // restart the Chrono

                int randomAngle = random(0,180);
                myServo.write(randomAngle);

                // ALTERNATIVE: USE MICROSECONDS FOR MORE PRECISION.
                // int randomUs = random(1500,2500);
                // myServo.writeMicroseconds(randomUs);
        }
}

4.  Exemple: contrôler un servomoteur avec un potentiomètre

/*
Controlling a servo position using a potentiometer (variable resistor).
The Chrono library is used to wait till the servo gets to its position.
*/

// INCLUDE SERVO.
#include <Servo.h>

// INSTANTION A SERVO OBJECT.
Servo myServo;

// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
#include <Chrono.h>

// Instanciate a Chrono object.
Chrono myChrono;

// Analog pin used to connect the potentiometer.
int potpin = A0;


void setup() {
        myServo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
        // THIS BLOCK OF CODE WILL RUN EVERY 15 MS.
        if (myChrono.hasPassed(15) ) { // elapsed(15) returns 1 (true) if 15 ms have passed.
                myChrono.restart();  // restart the Chrono

                int val = analogRead(potpin);        // reads the value of the potentiometer (value between 0 and 1023)
                val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
                myServo.write(val);                  // sets the servo position according to the scaled value
        }

}

5.  Autres exemples