AdaFruit Motor Shield Utiliser

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche

Premièrement: Installer la librairie Arduino

avant de pouvoir utiliser le Shield Motor (Motor shield), vous devez installer la librairie AF_Motor pour Arduino - cette librairie indique à Arduino comment communiquer vec le motor shield. Ce n'est pas une option mais une nécessité!

  1. Premièrement, téléchargez la librairie depuis le GitHub d'AdaFruit
  2. Décompressez l'archive (fichier Zip) sur votre bureau
  3. Renommer le répertoire décompressé en "AFMotor"
  4. Assurez vous que le répertoire AFMotor contienne bien les fichier AFMotor.cpp et AFMotor.h . Sinon vérifiez les étapes précédentes.
  5. Déplacez le répertoire AFMotor folder dans le répertoire des librairies Arduino (répertoire_arduinos_ketch/libraries). Pour Windows, cela doit être quelque-chose comme "Mes Documents/Arduino/libraries", pour Mac quelque chose comme "Documents/arduino/libraries". Si c'est la première fois que vous installez une librairie, vous devrez certainement créer le répertoire "libraries". Assurez-vous de l'appeler exactement "libraries", pas de capitale, pas d'autre nom.
  6. A l'intérieur du répertorie "librairies", il doit y avoir le répertoire "AFMotor", et qu'à l'intérieur de "AFMotor" il y ait AFMotor.cpp, AFMotor.h et quelques autres fichiers
  7. Quittez Arduino IDE et redémarrez le. Vous devriez voir un sous-menu appelé File->Examples->AFMotor->MotorParty

Alimenter un moteur continu, exigences en tension et courant

Les moteurs ont besoin de beaucoup d'énergie, plus particulièrement les moteurs meilleurs marchés puisqu'ils sont moins performant. La première chose importante à connaître est la tension d'alimentation que le moteur va utiliser. Si vous avez de la chance, votre moteur dispose d'une fiche technique.
Certain moteurs hobbyistes (de jouet) ne sont prévus que pour une tension de 1.5V, mais il est courant pour un moteur continu ("DC Motor" en anglais) d'avoir une tension de fonctionnement 6 à 12V. Ce shield est conçu pour contrôler des moteurs de 4.5V à 25V.

Exigence en courant

La seconde chose à connaître est la quantité de courant dont votre moteur aura besoin. Le circuit intégré L293D contrôlant le moteur ("Motor driver" en anglais) sont conçu pour fournir 600mA per moteur avec un pic de courant à 1.2 Ampère.
Notez qu'une fois que le courant tend vers 1A vous aurez probablement besoin d'équiper le "motor driver" d'un dissipateur de chaleur, sinon vous subirez des défaillances thermiques, voir griller votre circuit intégré.

Utiliser un SN754410

Certaines personnes utilise un circuit intégré SN754410 pour contrôler les moteurs. Ils utilise le SN754410 à la place du L293D parce que le brochage est identique ("Pin Compatible" en anglais), dispose de diodes de protections en sortie et supporte 1A par moteur avec un pic à 2A.

Après une lecture attentive de la fiche technique et quelques discussions avec des techniciens, il apparait que ces diodes de protections sont conçues pour fournir uniquement un protection contre les décharges électrostatiques (ESD Protection). Les utiliser comme diodes de protections en roule-libre est du domaine du Hack et les performances ne sont absolument pas garantie... le risque pour le pont-H est bien réel.

C'est pour cette raison que le kit n'est pas livré avec des SN754410 mais avec des L293D qui intègrent des vraie diodes de protection "en roue libre" ("kickback-protection diodes" en anglais). Si vous désirez quand même prendre le risque d'utiliser des SN754410 parce que vous avez besoin de plus de courant, vous pouvez maintenant acheter ces composants et remplacer mes L293D en connaissant les risques potentiels.

Besoin de plus de puissance?

Vous pouvez acheter d'autres L293D et les souder pile au dessus des premiers déjà présent sur la carte ('piggyback' en anglais).

Voila, comment vous pouvez doubler la courant supporté par le L293D! Vous pouvez empiler plus de deux L293D mais cela ne devrait pas apporter beaucoup plus de bénéfice.

PiggyBack-componant.jpg
Source: tinyenormous.com

Restez néanmoins vigilant avec les courants mis en œuvre car si vous disposez de deux fois plus de courant, vous avez aussi besoin de dissiper au total plus de chaleur qu'avec un seul L293D.
Tout le monde sait qu'il fait beaucoup plus chaud à deux sous la couette que tout seul. Ce principe s'applique aussi au L293D empilés ;-) .

Les moteurs et les piles de 9v

Vous ne pouvez pas faire fonctionner efficacement des moteurs avec des piles de 9 volts, ne gaspillez donc pas vos piles de 9v et votre temps!

Utilisez des piles de type Plomb Acide (Lead Acid) ou accumilateur NiMH.

99% des 'problèmes moteur incompréhensible' (bizarres) sont causés par des parasites (bruit) du circuit de puissance qui passe dans le circuit d'alimentation Arduino (1*) et/ou d'une alimentation pas suffisamment puissante!

1*: Cela signifie qu'une alimentation est partagée (commune) entre le circuit de commande (Arduino) et le circuit de puissance (les moteurs).

Alimentation moteur: Comment configurer Arduino + Shield

Les Servos sont alimentés par le régulateur 5V de la carte Arduino. C'est parfait pour les moteurs hobbyistes suggérés. Si vous avez besoin de quelque-chose de plus costaud, sectionnez la piste qui va à la broche + des connecteurs servo et branchez votre propre alimentation 5-6V!

Les moteurs continu (DC) sont alimentés depuis 'de circuit d'alimentation haute tension' (high voltage supply) et NON depuis le régulateur 5V d'Arduino.

Il y a deux endroits où vous pouvez obtenir votre 'alimentation haute tension' pour les moteurs. La première est la prise jack de votre Arduino (surlaquelle est raccordé votre bloc d'alimentation) et la deuxième est bornier à 2 vis sur le shield (celui portant le libellé EXT_PWR sur la carte).

La prise Jack de votre Arduino dispose d'une diode de protection, il n'est donc pas possible de faire une erreur fatale si vous utilisez un bloc d'alimentation mal polarisé. Cependant, le bornier EXT_PWR ne dispose pas de diode de protection (pour une vrai bonne raison). Soyez extrêmement prudent et n'inversez pas la polarité sinon vous allez détruire le shield moteur et/ou votre Arduino!

Voici comment cela fonctionne:

ADF-MOTOR-SHIELD-USE-ALIM.jpg

If you would like to have a single DC power supply for the Arduino and motors, simply plug it into the DC jack on the Arduino or the 2-pin PWR_EXT block on the shield. Place the power jumper on the motor shield. If you have a Diecimila Arduino, set the Arduino power source jumper to EXT. Note that you may have problems with Arduino resets if the battery supply is not able to provide constant power, and it is not a suggested way of powering your motor project

If you would like to have the Arduino powered off of USB and the motors powered off of a DC power supply, plug in the USB cable. Then connect the motor supply to the PWR_EXT block on the shield. Do not place the jumper on the shield. This is a suggested method of powering your motor project (If you have a Diecimila Arduino, don't forget to set the Arduino power jumper to USB. If you have a Diecimila, you can alternately do the following: plug the DC power supply into the Arduino, and place the jumper on the motor shield.)

If you would like to have 2 seperate DC power supplies for the Arduino and motors. Plug in the supply for the Arduino into the DC jack, and connect the motor supply to the PWR_EXT block. Make sure the jumper is removed from the motor shield. If you have a Diecimila Arduino, set the Arduino jumper to EXT. This is a suggested method of powering your motor project

Either way, if you want to use the DC motor/Stepper system the motor shield LED should be lit indicating good motor power

Servo Moteur

ADF-MOTOR-SHIELD-USE-SERVO.jpg

Hobby servos are the easiest way to get going with motor control. They have a 3-pin 0.1" female header connection with +5V, ground and signal inputs. The motor shield simply brings out the 16bit PWM output lines to 2 3-pin headers so that its easy to plug in and go. They can take a lot of power so a 9V battery wont last more than a few minutes!

The nice thing about using the onboard PWM is that its very precise and goes about its business in the background. You can use the built in Servo library

Using the servos is easy, please read the official Arduino documentation for how to use them and see the example Servo sketches in the IDE

Moteur Continu

ADF-MOTOR-SHIELD-USE-MOTEURCC.jpg

DC motors are used for all sort of robotic projects. The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary!

Note that the H-bridge chip is not really meant for driving loads over 0.6A or that peak over 1.2A so this is for small motors. Check the datasheet for information about the motor to verify its OK.

To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4. Then follow these steps in your sketch


  1. Make sure you include <AFMotor.h>
  2. Create the AF_DCMotor object with AF_DCMotor(motor#, frequency), to setup the motor H-bridge and latches. The constructor takes two arguments.
    The first is which port the motor is connected to, 1, 2, 3 or 4.
    frequency is how fast the speed controlling signal is.
    For motors 1 and 2 you can choose MOTOR12_64KHZ, MOTOR12_8KHZ, MOTOR12_2KHZ, or MOTOR12_1KHZ. A high speed like 64KHz wont be audible but a low speed like 1KHz will use less power. Motors 3 & 4 are only possible to run at 1KHz and will ignore any setting given
  3. Then you can set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
  4. To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.
#include <AFMotor.h>

AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor.setSpeed(200);     // set the speed to 200/255
}

void loop() {
  Serial.print("tick");
  
  motor.run(FORWARD);      // turn it on going forward
  delay(1000);

  Serial.print("tock");
  motor.run(BACKWARD);     // the other way
  delay(1000);
  
  Serial.print("tack");
  motor.run(RELEASE);      // stopped
  delay(1000);
}

Moteur pas-à-pas

Voici un exemple avec un moteur pas-à-pas bipolaire.

ADF-MOTOR-SHIELD-USE-BIPOLAR-STEPPER.jpg

Stepper motors are great for (semi-)precise control, perfect for many robot and CNC projects. This motor shield supports up to 2 stepper motors. The library works identically for bi-polar and uni-polar motors

For unipolar motors: to connect up the stepper, first figure out which pins connected to which coil, and which pins are the center taps. If its a 5-wire motor then there will be 1 that is the center tap for both coils. Theres plenty of tutorials online on how to reverse engineer the coils pinout. The center taps should both be connected together to the GND terminal on the motor shield output block. then coil 1 should connect to one motor port (say M1 or M3) and coil 2 should connect to the other motor port (M2 or M4).

For bipolar motors: its just like unipolar motors except theres no 5th wire to connect to ground. The code is exactly the same.

Running a stepper is a little more intricate than running a DC motor but its still very easy


  1. Make sure you include <AFMotor.h>
  2. Create the stepper motor object with AF_Stepper(steps, stepper#) to setup the motor H-bridge and latches. Steps indicates how many steps per revolution the motor has. a 7.5degree/step motor has 360/7.5 = 48 steps. Stepper# is which port it is connected to. If you're using M1 and M2, its port 1. If you're using M3 and M4 its port 2
  3. Set the speed of the motor using setSpeed(rpm) where rpm is how many revolutions per minute you want the stepper to turn.
  4. Then every time you want the motor to move, call the step(#steps, direction, steptype) procedure. #steps is how many steps you'd like it to take. direction is either FORWARD or BACKWARD and the step type is SINGLE, DOUBLE. INTERLEAVE or MICROSTEP.
    "Single" means single-coil activation, "double" means 2 coils are activated at once (for higher torque) and "interleave" means that it alternates between single and double to get twice the resolution (but of course its half the speed). "Microstepping" is a method where the coils are PWM'd to create smooth motion between steps. Theres tons of information about the pros and cons of these different stepping methods in the resources page.
    You can use whichever stepping method you want, changing it "on the fly" to as you may want minimum power, more torque, or more precision.
  5. By default, the motor will 'hold' the position after its done stepping. If you want to release all the coils, so that it can spin freely, call release()
  6. The stepping commands are 'blocking' and will return once the steps have finished. If someone wants to be awesome and write a version of the library that does background stepping that would be cool! :)
#include <AFMotor.h>

AF_Stepper motor(48, 2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(10);  // 10 rpm   

  motor.step(100, FORWARD, SINGLE); 
  motor.release();
  delay(1000);
}

void loop() {
  motor.step(100, FORWARD, SINGLE); 
  motor.step(100, BACKWARD, SINGLE); 

  motor.step(100, FORWARD, DOUBLE); 
  motor.step(100, BACKWARD, DOUBLE);

  motor.step(100, FORWARD, INTERLEAVE); 
  motor.step(100, BACKWARD, INTERLEAVE); 

  motor.step(100, FORWARD, MICROSTEP); 
  motor.step(100, BACKWARD, MICROSTEP); 
}

If you want two stepper motors to step at once you'll need to write something like this:

void doublestep (int steps, int direction, int style) {
  while (steps--) {
    motor1.step(1, direction, style); 
    motor2.step(1, direction, style); 
  }
}


Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.