مكتبة Servo وattach
ضمّن Servo واربطه بمنفذ.
مكتبة Servo وattach درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Arduino & IoT Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. 🔧
الأسئلة الشائعة
هل درس «مكتبة Servo وattach» مجاني؟
نعم — نص درس «مكتبة Servo وattach» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
ماذا ستتعلم في «مكتبة Servo وattach»؟
ضمّن Servo واربطه بمنفذ. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟
لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «مكتبة Servo وattach»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟
نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كيف يعرف Servo زاويته
- مكتبة Servo وattach
- الحركة من 0 إلى 180
- Servo يتحكم فيه مقبض