Biblioteka Servo i attach
Dołączyć Servo i przypisać je do pinu.
Biblioteka Servo i attach to bezpłatna lekcja Arduino & IoT Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Arduino & IoT Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🔧
Często zadawane pytania
Czy lekcja „Biblioteka Servo i attach” jest bezpłatna?
Tak — pełny tekst „Biblioteka Servo i attach” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Arduino & IoT Academy, przejdź na CoddyKit PRO. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Biblioteka Servo i attach”?
Dołączyć Servo i przypisać je do pinu. Ćwiczysz Arduino & IoT Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Arduino & IoT Academy?
Nie wymagamy żadnego doświadczenia. Arduino & IoT Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Biblioteka Servo i attach”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Arduino & IoT Academy?
Tak. Każda lekcja Arduino & IoT Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Jak serwomechanizm zna swój kąt
- Biblioteka Servo i attach
- Przemiatanie od 0 do 180
- Serwomechanizm sterowany pokrętłem