A biblioteca Servo e attach
Inclua Servo e associe-o a um pino.
A biblioteca Servo e attach é uma aula grátis de Arduino & IoT Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Arduino & IoT Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Arduino & IoT Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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. 🔧
Perguntas Frequentes
A aula “A biblioteca Servo e attach” é grátis?
Sim — o texto completo de “A biblioteca Servo e attach” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Arduino & IoT Academy, atualize para CoddyKit PRO. O curso de Arduino & IoT Academy inclui 4 aulas no total.
O que vou aprender em “A biblioteca Servo e attach”?
Inclua Servo e associe-o a um pino. Você pratica Arduino & IoT Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Arduino & IoT Academy?
Nenhuma experiência prévia é necessária. Arduino & IoT Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “A biblioteca Servo e attach”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Arduino & IoT Academy?
Sim. Cada aula de Arduino & IoT Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Como um servo conhece seu ângulo
- A biblioteca Servo e attach
- Percorra de 0 a 180
- Servo controlado por botão giratório