0Pricing
Arduino & IoT Academy · Leçon

La bibliothèque Servo et attach

Incluez Servo et associez-la à une broche.

La bibliothèque Servo et attach est une leçon Arduino & IoT Academy gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Arduino & IoT Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Arduino & IoT Academy comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Let a Library Help

Generating precise pulses by hand is painful. The built-in Servo library does the timing for you, so you just say the angle you want. 😌

Include It First

Bring the library into your sketch with an #include at the very top. That one line unlocks the Servo tools.

#include <Servo.h>

Make a Servo Object

Next, create a Servo object. Think of it as your remote control: you give it a name, then send commands through it.

Servo myServo;

Bind It to a Pin

Tell the object which pin the signal wire uses with attach(). After this, that pin starts sending servo pulses.

myServo.attach(9);

Where attach Goes

Call attach() inside setup(), since you only need to bind the pin once when the board starts up.

void setup() {
  myServo.attach(9);
}

Pick a PWM Pin

The Servo library can drive almost any pin, but a PWM-capable pin like 9 or 10 is the safe, conventional choice on an Uno.

Move with write()

Command an angle using write(). Pass a number from 0 to 180 and the servo heads straight to that position.

myServo.write(90);

It Stays Attached

Once attached, the object keeps holding the last angle you wrote. You only call write() again when you want it to move somewhere new.

Custom Pulse Limits

If your servo undershoots or overshoots, attach() can take min and max pulse widths in microseconds to fine-tune its travel.

myServo.attach(9, 600, 2400);

Free the Pin

Call detach() to stop sending pulses and release the pin. Handy when you want the servo to go limp and save power.

myServo.detach();

One Object Each

Driving several servos? Create a separate Servo object for each one and attach them to different pins. They run independently.

Quick Check

Let's test how you connect a servo to a pin in code.

Recap

Include Servo.h, make an object, attach it to a pin in setup, then move it with write(). That's the whole setup pattern. 🔧

Questions Fréquemment Posées

La leçon « La bibliothèque Servo et attach » est-elle gratuite ?

Oui — le texte complet de « La bibliothèque Servo et attach » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Arduino & IoT Academy, passe à CoddyKit PRO. Le cours Arduino & IoT Academy comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « La bibliothèque Servo et attach » ?

Incluez Servo et associez-la à une broche. Tu pratiques Arduino & IoT Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Arduino & IoT Academy ?

Aucune expérience préalable n'est requise. Arduino & IoT Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.

Combien de temps prend la leçon « La bibliothèque Servo et attach » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Arduino & IoT Academy ?

Oui. Chaque leçon Arduino & IoT Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Comment un servomoteur connaît son angle
  2. La bibliothèque Servo et attach
  3. Balayer de 0 à 180
  4. Servomoteur commandé par un bouton
← Retour à Arduino & IoT Academy