0Pricing
Arduino & IoT Academy · Lección

La biblioteca Servo y attach

Incluya Servo y vincúlela a un pin.

La biblioteca Servo y attach es una lección gratuita de Arduino & IoT Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Arduino & IoT Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Arduino & IoT Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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. 🔧

Preguntas frecuentes

¿La lección «La biblioteca Servo y attach» es gratis?

Sí — el texto completo de «La biblioteca Servo y attach» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Arduino & IoT Academy, actualiza a CoddyKit PRO. El curso de Arduino & IoT Academy incluye 4 lecciones en total.

¿Qué aprenderé en «La biblioteca Servo y attach»?

Incluya Servo y vincúlela a un pin. Practicas Arduino & IoT Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Arduino & IoT Academy?

No se requiere experiencia previa. Arduino & IoT Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «La biblioteca Servo y attach»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Arduino & IoT Academy?

Sí. Cada lección de Arduino & IoT Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Cómo conoce un servomotor su ángulo
  2. La biblioteca Servo y attach
  3. Barrido de 0 a 180
  4. Servomotor controlado con un potenciómetro
← Volver a Arduino & IoT Academy