Chapterبەش 5 · Lectureوانە 5.6
Actuators: DC Motors, Encoders and Servosجووڵێنەرەکان: ماتۆڕەکانی DC، کۆدکەرەکانی هەنگاوبەهەنگاو و ماتۆڕەکانی سێرڤۆ
Actuators turn commands into action. A DC motor needs a driver (an H-bridge) and its own supply, never a pin; an encoder measures its turning so the board can close the loop; a positional servo goes to any angle set by the width of a pulse every 20 ms; a continuous-rotation servo turns speed instead of angle and needs limit switches. Ends with choosing a motor for the smart cage's cover.
فەرمان دەکەن بە کار. پێویستی بە () و خۆی هەیە، هەرگیز پین نا؛ سووڕانەوەکەی دەپێوێت تا بۆردەکە خولەکە دابخات؛ شوێنی دەچێتە سەر هەر گۆشەیەک کە پانایی لە هەموو 20 msـدا دیاری دەکات؛ لە جیاتی گۆشە خێرایی وەردەگرێت و پێویستی بە هەیە. بە هەڵبژاردنی بۆ بەرگی قەفەزە هۆشمەندەکە کۆتایی دێت.
What you will learnچی فێر دەبیت
- Explain what an actuator is and name the common ones, keeping mains loads away from boards.
- Explain why a DC motor needs a driver, how an H-bridge reverses it and how PWM sets its speed.
- Use an encoder's pulses to measure distance, speed and direction, and stop a motor at a target.
- Set a positional servo to any angle with the Servo library and relate the angle to the pulse width.
- Control a continuous-rotation servo and choose a motor for a real job.
- ڕوونی بکەیتەوە چییە، باوەکانیان ناو ببەیت و بارەکانی کارەبای ماڵ لە بۆرد دوور بخەیتەوە.
- ڕوونی بکەیتەوە بۆچی پێویستی بە هەیە، چۆن پێچەوانەی دەکاتەوە و چۆن خێراییەکەی دیاری دەکات.
- بەکار بهێنیت بۆ پێوانی دووری، خێرایی و ئاراستە و لە ئامانجێکدا ڕابگریت.
- شوێنی بە Servo بخەیتە سەر هەر گۆشەیەک و گۆشەکە بە پانایی ببەستیتەوە.
- کۆنترۆڵ بکەیت و بۆ کارێکی ڕاستەقینە هەڵبژێریت.
From sensing to doingلە هەستکردنەوە بۆ کردن
Sensors bring the world into the board; actuators let the board change the world. An actuator turns an electrical command into a physical action: a turn, a push, a sound, light, heat. The smart cage at the end of this course needs one to pull a cover over the birds at dusk and back at dawn. This lesson compares the motors that can do it: DC motors with a driver, encoders that report the turning, and servos.
جیهان دەهێننە ناو بۆردەکەوە؛ ڕێگە بە بۆردەکە دەدەن جیهان بگۆڕێت. فەرمانێکی کارەبایی دەکات بە کارێکی جەستەیی: سووڕانەوەیەک، پاڵنانێک، دەنگێک، ڕووناکی، گەرمی. قەفەزە هۆشمەندەکەی کۆتایی ئەم کۆرسە پێویستی بە یەکێکیانە تا لە ئێوارەدا بەرگێک بەسەر باڵندەکاندا بکێشێت و لە بەرەبەیاندا لایبدات. ئەم وانەیە ئەو دەتوانن ئەمە بکەن بەراورد دەکات: لەگەڵ ، کە سووڕانەوەکە ڕاپۆرت دەکات و .
The pair sensor–actuator first appears in Sensors measure, actuators move; the AI course meets it in agents and robots (Actuators).
جووتی – یەکەم جار لە هەستەوەرەکان دەپێون، جووڵێنەرەکان دەجووڵێنن دەردەکەوێت؛ کۆرسی لە بریکار و ڕۆبۆتدا دەیبینێت (جووڵێنەرەکان).
Actuators you will meetئەو جووڵێنەرانەی دەیانبینیت
Small IoT projects mostly use these: a DC gear motor that spins (wheels, fans, the string of a cover); a servo motor that goes to an angle (a flap, a valve); a stepper motor that moves in exact steps (3D printers, camera sliders); a water pump; a relay that switches another circuit; and simple output devices such as a buzzer or an LED. Drones use brushless motors driven by an ESC, which this course does not repeat (Brushless motors, The ESC).
پڕۆژە بچووکەکانی IoT زۆرتر ئەمانە بەکار دەهێنن: گێڕدار کە دەسووڕێت (چەرخ، پانکە، پەتی بەرگێک)؛ کە دەچێتە سەر گۆشەیەک (دەرچەیەک، زمانەیەک)؛ کە بە هەنگاوی ورد دەجووڵێت (چاپەری سێڕەهەندی، سڵایدەری کامێرا)؛ پەمپی ئاو؛ کە تر هەڵ و دەکوژێنێتەوە؛ و ئامێری دەرچوونی سادەی وەک یان . بەکار دەهێنن کە دەیانجووڵێنێت. ئەم کۆرسە ئەوانە دووپاتە ناکاتەوە (ماتۆڕە بێفڵچەکان، کۆنترۆڵکەری خێرایی).
The DC motor: it spins while current flowsماتۆڕی DC: تا تەزوو هەبێت دەسووڕێت
A DC motor has fixed magnets and a rotor with coils. Brushes and a split ring (the commutator) keep switching the current in the coils, so the magnets always pull the rotor further round. More voltage, more speed; swap the two wires and it turns the other way. Left to itself, a DC motor turns freely: it does not know where it is and stops only when the current stops.
موگناتیسی جێگیر و بەشێکی سووڕاوی بە پێچی کارەبایی هەیە. فڵچەکان و ئەڵقەیەکی لەتکراو (گۆڕەرەکە) بەردەوام ناو پێچەکان دەگۆڕن، بۆیە موگناتیسەکان هەمیشە بەشە سووڕاوەکە زیاتر دەسووڕێنن. زیاتر، خێرایی زیاتر؛ دوو وایەرەکەی ئاڵوگۆڕ بکە و بە پێچەوانەوە دەسووڕێت. ئەگەر بە خۆی بهێڵرێتەوە، بە ئازادی دەسووڕێت: نازانێت لە کوێیە و تەنیا کاتێک دەوەستێت کە دەبڕێت.
Small motors spin fast but weakly, so a gearbox is added: a gear motor trades speed for turning force (torque). A yellow TT motor, for example, has a 1:48 gearbox and turns its shaft about 200 times a minute at 6 V. In the cage project a gear motor with an encoder can wind the cover's string to remove and replace the cover.
بچووکەکان خێرا بەڵام لاواز دەسووڕێن، بۆیە گێڕبۆکسێک زیاد دەکرێت: گێڕدار خێرایی دەدات بە هێزی سووڕاندن (). بۆ نموونە TTـی زەرد گێڕبۆکسێکی 1:48ـی هەیە و بە 6 V تەوەرەکەی نزیکەی 200 جار لە خولەکێکدا دەسووڕێنێت. لە پڕۆژەی قەفەزەکەدا گێڕدار بە دەتوانێت پەتی بەرگەکە بپێچێتەوە تا بەرگەکە لابدات و دایبداتەوە.
Never drive a motor straight from a pinهەرگیز ماتۆڕێک ڕاستەوخۆ لە پینێکەوە مەجووڵێنە
A pin can give a few milliamps: about 20 mA on an UNO R3, 8 mA on an UNO R4, 7 mA on the MKR WAN 1310 (5 V boards and 3.3 V boards). A small TT gear motor takes about 150 mA when it runs free and over 1 A when it starts or is blocked. A motor coil also kicks back a voltage spike when it is switched off, and its brushes make electrical noise.
پینێک دەتوانێت چەند میلیئەمپێرێک بدات: نزیکەی 20 mA لەسەر UNO R3، 8 mA لەسەر UNO R4، 7 mA لەسەر MKR WAN 1310 (بۆردەکانی 5 V و بۆردەکانی 3.3 V). گێڕداری بچووکی TT کاتێک بە ئازادی دەسووڕێت نزیکەی 150 mA وەردەگرێت و کاتێک دەست پێدەکات یان دەگیرێت زیاتر لە 1 A. هەروەها پێچی کاتێک دەکوژێنرێتەوە بڕێک بەرز دەداتەوە و فڵچەکانی ژاوەژاوی کارەبایی دروست دەکەن.
The H-bridge: forward, backward, stopپردی H: پێشەوە، دواوە، وەستان
To reverse a motor without swapping wires by hand, a driver uses an H-bridge: four electronic switches (transistors) around the motor, drawn like the letter H. Close the top-left and bottom-right switches and the current crosses the motor from left to right; close the other diagonal and it crosses from right to left, so the motor turns the other way. Close both lower switches and the motor's ends are joined: it brakes fast. Never close both switches on one side: that joins the supply straight to GND, a short circuit — driver chips are built so this cannot happen.
بۆ پێچەوانەکردنەوەی بەبێ ئاڵوگۆڕی وایەر بە دەست، بەکار دەهێنێت: چوار ئەلیکترۆنی () لە دەوری ، کە وەک پیتی H دەکێشرێن. سەرەوەی چەپ و خوارەوەی ڕاست دابخە و لە چەپەوە بۆ ڕاست بە تێدەپەڕێت؛ لارەکەی تر دابخە و لە ڕاستەوە بۆ چەپ تێدەپەڕێت، بۆیە بە پێچەوانەوە دەسووڕێت. هەردوو خوارەوە دابخە و دوو سەری پێکەوە دەبەسترێن: بە خێرایی دەوەستێت. هەرگیز هەردوو یەک لا دامەخە: ئەوە سەرچاوەکە ڕاستەوخۆ بە GNDـەوە دەبەستێت، — چیپەکانی وا دروست کراون کە ئەمە ڕوو نەدات.


The H-bridge: forward, backward, stopپردی H: پێشەوە، دواوە، وەستان
Driver boards: L298N and TB6612FNGبۆردی درایڤەری ماتۆڕ: L298N و TB6612FNG
A motor driver chip holds two H-bridges and the logic around them. From the board it takes small signals: two direction inputs per motor (IN1, IN2) and a speed input fed with PWM (PWM: on and off, faster than the eye); the motor current comes from the motor's own supply (VM). The old L298N handles up to 46 V and 2 A per bridge, but its transistors lose 1.8–3.2 V at 1 A, so a 6 V battery gives the motor only about 3–4 V. The TB6612FNG uses MOSFETs: 1.2 A per motor (3.2 A for short peaks), a motor supply up to 13.5 V and logic from 2.7 to 5.5 V.
چیپێکی دوو و لۆجیکی دەوروبەریان هەڵدەگرێت. لە بۆردەکەوە بچووک وەردەگرێت: بۆ هەر دوو ئاراستە (IN1، IN2) و خێرایی کە بە پڕ دەکرێتەوە (مۆدیولەیشنی پانایی تڕپە: هەڵکردن و کوژاندنەوە، لە چاو خێراتر)؛ لە خودی دێت (VM). L298Nـی کۆن تا 46 V و 2 A بۆ هەر پردێک دەگرێت، بەڵام لە 1 Aـدا 1.8–3.2 V بەفیڕۆ دەدەن، بۆیە پاترییەکی 6 V تەنیا نزیکەی 3–4 V دەداتە . TB6612FNG MOSFET بەکار دەهێنێت: 1.2 A بۆ هەر (3.2 A بۆ لووتکەی کورت)، تا 13.5 V و لۆجیک لە 2.7 تا 5.5 V.
| IN1پینی IN1 | IN2پینی IN2 | TB6612FNG doesTB6612FNG چی دەکات |
|---|---|---|
| HIGH | LOW | turns one way (CW), speed from PWMبە یەک ئاراستە دەسووڕێت (CW)، خێرایی لە |
| LOW | HIGH | turns the other way (CCW)بە ئاراستەکەی تر دەسووڕێت (CCW) |
| HIGH | HIGH | short brake: stops fastڕاگرتنی خێرا: بە خێرایی دەوەستێت |
| LOW | LOW | stop (with PWM HIGH): the motor is let go and coastsوەستان (بە HIGH): بەرەڵا دەکرێت و بە خۆی دەوەستێت |
The H-bridge: forward, backward, stopپردی H: پێشەوە، دواوە، وەستان
Code: forward, brake, backwardکۆد: پێشەوە، ڕاگرتن، دواوە
The sketch wraps the truth table in three small functions. STBY goes HIGH once to wake the driver; then the motor runs forward at speed 200 for 2 s, brakes for 1 s, runs backward at 120 for 2 s and brakes again.
لە سێ بچووکدا دادەنێت. STBY جارێک دەبێتە HIGH تا بەئاگا بێت؛ پاشان 2 چرکە بە خێرایی 200 بەرەو پێشەوە دەسووڕێت، 1 چرکە ڕادەگیرێت، 2 چرکە بە 120 بەرەو دواوە دەسووڕێت و دیسان ڕادەگیرێت.
// A DC gear motor through a TB6612FNG driver (Arduino UNO): forward, brake, backward
const int ain1 = 7; // direction input 1
const int ain2 = 8; // direction input 2
const int pwma = 5; // speed: a PWM pin (~5)
const int stby = 4; // HIGH = the driver is awake
void setup() {
Serial.begin(9600);
pinMode(ain1, OUTPUT);
pinMode(ain2, OUTPUT);
pinMode(pwma, OUTPUT);
pinMode(stby, OUTPUT);
digitalWrite(stby, HIGH);
}
void forward(int speed) { // speed 0-255
digitalWrite(ain1, HIGH);
digitalWrite(ain2, LOW);
analogWrite(pwma, speed);
}
void backward(int speed) {
digitalWrite(ain1, LOW);
digitalWrite(ain2, HIGH);
analogWrite(pwma, speed);
}
void brake() { // both inputs HIGH: short brake
digitalWrite(ain1, HIGH);
digitalWrite(ain2, HIGH);
analogWrite(pwma, 0);
}
void loop() {
Serial.println("forward, speed 200");
forward(200);
delay(2000);
Serial.println("brake");
brake();
delay(1000);
Serial.println("backward, speed 120");
backward(120);
delay(2000);
Serial.println("brake");
brake();
delay(1000);
}forward, speed 200 brake backward, speed 120 brake forward, speed 200
CêIT Arduino checker, 6.5 s. Pins: 7 HIGH, 8 LOW and PWM 200 (78 %) at 0 s; 8 HIGH and PWM 0 at 2 s; 7 LOW and PWM 120 (47 %) at 3 s; 7 HIGH and PWM 0 at 5 s.
پشکنەری Arduinoـی CêIT، 6.5 چرکە. پینەکان: 7 HIGH، 8 LOW و PWM 200 (78%) لە چرکەی 0؛ 8 HIGH و PWM 0 لە چرکەی 2؛ 7 LOW و PWM 120 (47%) لە چرکەی 3؛ 7 HIGH و PWM 0 لە چرکەی 5.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 13
STBY HIGH: the driver is awake.
STBY HIGH: بەئاگایە.
- Lineدێڕی 16-20
Forward: IN1 HIGH, IN2 LOW, speed as a PWM value 0–255.
پێشەوە: IN1 HIGH، IN2 LOW، خێرایی وەک نرخێکی 0–255.
- Lineدێڕی 22-26
Backward: the two inputs swapped.
دواوە: دوو ئاڵوگۆڕ کراون.
- Lineدێڕی 28-32
Both HIGH: a short brake.
هەردووکیان HIGH: ڕاگرتنی خێرا.
Encoders: the motor reports backکۆدکەری هەنگاوبەهەنگاو: ماتۆڕەکە ڕاپۆرت دەداتەوە
A DC motor alone cannot say how far it has turned. An encoder on its shaft can: in a gear motor it is usually an incremental encoder, a magnet ring (or a slotted disc) with two small sensors, A and B, that give a pulse for every small step of rotation. The encoder only measures. It does not choose the direction or stop the motor — the driver does that, on the board's command.
بە تەنیا ناتوانێت بڵێت چەندی سووڕاوە. لەسەر تەوەرەکەی دەتوانێت: لە گێڕداردا زۆرجار ، ئەڵقەیەکی موگناتیس (یان قرسێکی کونکراو) بە دوو بچووکی A و B کە بۆ هەر هەنگاوێکی بچووکی سووڕانەوە دەدەن. تەنیا دەپێوێت. ئاراستە هەڵنابژێرێت و ڕاناگرێت — ئەمە دەکات، بە فەرمانی بۆردەکە.
- Count the pulses → how far the shaft has turned (the position).
- Pulses per second → how fast it turns.
- Which of A and B rises first → which way it turns: A and B are a quarter of a pulse apart (quadrature).
- بژمێرە ← تەوەرەکە چەندی سووڕاوە (شوێن).
- لە چرکەیەکدا ← چەند خێرا دەسووڕێت.
- کامیان لە A و B یەکەم بەرز دەبێتەوە ← بە کام ئاراستەدا دەسووڕێت: A و B چارەکێکی لە یەکتر دوورن.
After power-up it knows nothing: the count starts at 0 wherever the shaft is. So a project first drives to a known place, such as a limit switch at one end (homing), and counts from there.
دوای هەڵکردن هیچ نازانێت: ژماردنەکە لە 0ـەوە دەست پێدەکات لە هەر شوێنێکدا تەوەرەکە بێت. بۆیە پڕۆژەیەک سەرەتا دەچێتە شوێنێکی ناسراو (وەک لە سەرێکدا) و لەوێوە دەژمێرێت.
Encoders: the motor reports backکۆدکەری هەنگاوبەهەنگاو: ماتۆڕەکە ڕاپۆرت دەداتەوە
An encoder gear motor's six wiresشەش وایەری ماتۆڕی DCـێکی گێڕدار بە کۆدکەری هەنگاوبەهەنگاوەوە
A gear motor with an encoder usually has six wires: two for the motor (to the driver's outputs, never to the board) and four for the encoder: VCC and GND (3.3–5 V from the board), A and B (to two input pins). Most motor encoders use Hall sensors with open outputs, so set the pins to INPUT_PULLUP or add pull-up resistors (INPUT_PULLUP). The number of pulses per turn is printed on the motor's data sheet: count pulses per turn of the output shaft, which is the motor's figure times the gear ratio.
گێڕدار بە زۆرجار شەش وایەری هەیە: دووان بۆ (بۆ دەرچوونەکانی ، هەرگیز بۆ بۆردەکە) و چواریان بۆ : VCC و GND (3.3–5 V لە بۆردەکەوە)، A و B (بۆ دوو پینی ). زۆربەی ماتۆڕ Hall بە دەرچوونی کراوە بەکار دەهێنن، بۆیە پینەکان بکە بە INPUT_PULLUP یان زیاد بکە (INPUT_PULLUP). ژمارەی بۆ هەر سووڕانەوەیەک لە نووسراوە: بۆ هەر سووڕانەوەیەکی تەوەرەی دەرچوون بژمێرە، کە ژمارەکەی جارانی ڕێژەی گێڕەکان.
Encoders: the motor reports backکۆدکەری هەنگاوبەهەنگاو: ماتۆڕەکە ڕاپۆرت دەداتەوە
Closed loop: count, compare, correctخولی داخراو: بژمێرە، بەراورد بکە، ڕاستی بکەرەوە
With an encoder the board can close the loop (Closed-loop control): it sets a target, runs the motor, counts the pulses and brakes when the count reaches the target. This sketch counts rising edges of channel A by checking the pin on every pass of loop(); real projects count with an interrupt (attachInterrupt()) so no pulse is ever missed, even when the sketch is busy.
بە بۆردەکە دەتوانێت خولەکە دابخات (کۆنترۆڵی خولی داخراو): ئامانجێک دادەنێت، دەسووڕێنێت، دەژمێرێت و کاتێک ژمارەکە گەیشتە ئامانجەکە ڕایدەگرێت. ئەم بە پشکنینی پینەکە لە هەر خولێکی loop()ـدا کەناڵی A دەژمێرێت؛ پڕۆژە ڕاستەقینەکان بە (attachInterrupt()) دەژمێرن تا هیچ لەدەست نەچێت، تەنانەت کاتێک سەرقاڵە.
// Closed loop: run the motor until the encoder has sent 30 pulses, then brake
const int ain1 = 7;
const int ain2 = 8;
const int pwma = 5;
const int stby = 4;
const int encoderA = 2; // channel A of the motor's encoder
const long target = 30; // pulses to travel
long count = 0;
int lastA = HIGH;
bool moving = true;
void setup() {
Serial.begin(9600);
pinMode(ain1, OUTPUT);
pinMode(ain2, OUTPUT);
pinMode(pwma, OUTPUT);
pinMode(stby, OUTPUT);
pinMode(encoderA, INPUT_PULLUP);
digitalWrite(stby, HIGH);
digitalWrite(ain1, HIGH); // forward
digitalWrite(ain2, LOW);
analogWrite(pwma, 150);
Serial.println("motor on");
}
void loop() {
int a = digitalRead(encoderA);
if (a == HIGH && lastA == LOW) { // a rising edge: one more pulse
count++;
if (count % 10 == 0) {
Serial.print(count);
Serial.println(" pulses");
}
}
lastA = a;
if (moving && count >= target) { // compare with the set point
digitalWrite(ain1, HIGH); // brake
digitalWrite(ain2, HIGH);
analogWrite(pwma, 0);
moving = false;
Serial.print("target reached at ");
Serial.print(millis());
Serial.println(" ms: brake");
}
}motor on 10 pulses 20 pulses 30 pulses target reached at 925 ms: brake
CêIT Arduino checker: the scenario sent a pulse on pin 2 every 30 ms. The driver's AIN2 (pin 8) went HIGH and the PWM to 0 at 925 ms: a short brake.
پشکنەری Arduinoـی CêIT: سیناریۆکە هەموو 30 ms لەسەر پینی 2 نارد. AIN2ـی (پینی 8) لە 925 msـدا بوو بە HIGH و PWM بوو بە 0: ڕاگرتنی خێرا.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 29-30
A rising edge (LOW before, HIGH now) is one more pulse.
(پێشتر LOW، ئێستا HIGH) ترە.
- Lineدێڕی 38-42
Compare with the target; brake once it is reached.
بەراورد لەگەڵ ئامانجەکەدا؛ کە گەیشتێ، ڕایبگرە.
The servo: a motor that goes to an angleماتۆڕی سێرڤۆ: ماتۆڕێک کە دەچێتە سەر گۆشەیەک
A hobby servo motor (such as the 9 g SG90) is a small DC motor, a gearbox, a position sensor (a variable resistor on the output shaft) and a control board in one box: its own closed loop. You tell it an angle; it turns until its sensor reports that angle and then holds it. A positional (standard) servo can be set to any angle in its range, usually about 0–180°, not only to 0°, 90° and 180°. It has three wires: brown (or black) GND, red 5 V, and orange (or yellow) for the signal.
هۆبی (وەک SG90ـی 9 g) بچووک، گێڕبۆکسێک، شوێن ( لەسەر تەوەرەی دەرچوون) و بۆردێکی کۆنترۆڵە لە یەک سندووقدا: خولی داخراوی خۆی. گۆشەیەکی پێ دەڵێیت؛ دەسووڕێت تا ئەو گۆشەیە ڕاپۆرت دەکات و پاشان ڕایدەگرێت. شوێنی (ئاسایی) دەتوانرێت بخرێتە سەر هەر گۆشەیەک لە نێوان سنوورەکانیدا، زۆرجار نزیکەی 0–180°، نەک تەنیا 0°، 90° و 180°. سێ وایەری هەیە: قاوەیی (یان ڕەش) GND، سوور 5 V و پرتەقاڵی (یان زەرد) بۆ .
The servo: a motor that goes to an angleماتۆڕی سێرڤۆ: ماتۆڕێک کە دەچێتە سەر گۆشەیەک
Pulse width sets the angleپانایی تڕپە گۆشەکە دیاری دەکات
The signal wire gets one pulse about every 20 ms (50 times a second); the width of the pulse is the angle. The SG90's data sheet shows a 20 ms period and pulses of about 0.5 to 2.4 ms across its whole turn, with the middle near 1.45–1.5 ms; older copies of the sheet give the classic 1–2 ms of RC servos. Arduino's Servo library sends 544 µs for 0° and 2400 µs for 180° by default:
وایەری نزیکەی هەموو 20 ms جارێک وەردەگرێت (50 جار لە چرکەیەکدا)؛ پانایی گۆشەکەیە. SG90 20 ms و نزیکەی 0.5 تا 2.4 ms بۆ تەواوی سووڕانەوەکەی پیشان دەدات، ناوەڕاستەکەی نزیکەی 1.45–1.5 msـە؛ کۆپیی کۆنتری پەڕەکە 1–2 msـی کلاسیکی سێرڤۆکانی RC دەدات. Servoـی Arduino بە شێوەی بنەڕەت 544 µs بۆ 0° و 2400 µs بۆ 180° دەنێرێت:


The servo: a motor that goes to an angleماتۆڕی سێرڤۆ: ماتۆڕێک کە دەچێتە سەر گۆشەیەک
Code: move a servo to four anglesکۆد: ماتۆڕی سێرڤۆیەک بۆ چوار گۆشە ببە
The Servo library hides the pulses: attach(pin) starts them, write(angle) sets the width, and the servo holds the angle until the next write. On a servo whose full turn needs other pulse widths, give your own limits: attach(9, 500, 2400) for an SG90 that follows its data sheet.
Servo دەشارێتەوە: attach(pin) دەستیان پێ دەکات، write(angle) پاناییەکە دیاری دەکات و گۆشەکە ڕادەگرێت تا write()ـی داهاتوو. لەسەر کە تەواوی سووڕانەوەکەی پانایی تری دەوێت، سنووری خۆت بدە: attach(9, 500, 2400) بۆ SG90ـێک کە بەدوای دەڕوات.
// A positional servo (SG90) on pin 9 of an UNO: four angles, one per second
#include <Servo.h>
Servo flap; // the servo that moves a small flap
const int angles[] = {0, 90, 180, 45};
void setup() {
Serial.begin(9600);
flap.attach(9); // default pulses: 544 us = 0°, 2400 us = 180°
}
void loop() {
for (int i = 0; i < 4; i++) {
flap.write(angles[i]); // go to this angle and hold it
Serial.print("write(");
Serial.print(angles[i]);
Serial.print("): pulse ");
Serial.print(map(angles[i], 0, 180, 544, 2400));
Serial.println(" us every 20 ms");
delay(1000);
}
}write(0): pulse 544 us every 20 ms write(90): pulse 1472 us every 20 ms write(180): pulse 2400 us every 20 ms write(45): pulse 1008 us every 20 ms
CêIT Arduino checker: the servo stand-in logged write(0), write(90), write(180) and write(45), one second apart.
پشکنەری Arduinoـی CêIT: شوێنگرەوەی write(0)، write(90)، write(180) و write(45)ـی تۆمار کرد، بە یەک چرکە جیاوازی.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 9
attach(): pulses start, 544–2400 µs for 0–180°.
attach(): دەست پێدەکەن، 544–2400 µs بۆ 0–180°.
- Lineدێڕی 14
write(): the servo turns to the angle and holds it.
write(): بۆ گۆشەکە دەسووڕێت و ڕایدەگرێت.
- Lineدێڕی 18
map() shows the pulse width the library sends for each angle.
map() ئەو پانایی پیشان دەدات کە بۆ هەر گۆشەیەک دەینێرێت.
The servo: a motor that goes to an angleماتۆڕی سێرڤۆ: ماتۆڕێک کە دەچێتە سەر گۆشەیەک
Powering a servo, and the pins it borrowsوزەدان بە ماتۆڕی سێرڤۆ و ئەو پینانەی بە قەرز دەیانبات
One small servo such as the SG90 (4.8–5 V) can take its power from the UNO's 5V pin. Two or more, or bigger ones, need a separate 5 V supply, with its GND joined to the board's GND. One more thing to know: on the UNO R3 and other small AVR boards (not the Mega), the Servo library takes over a timer, so analogWrite() (PWM) on pins 9 and 10 stops working while servos are attached — even if no servo is on those pins. Choose other PWM pins for LEDs or motor speed.
بچووکی وەک SG90 (4.8–5 V) دەتوانێت وزەکەی لە پینی 5Vـی UNOـەوە وەربگرێت. دووان یان زیاتر، یان گەورەتر، 5 Vـی جیایان دەوێت، بە GNDـەکەی بەستراو بە GNDـی بۆردەکەوە. شتێکی تریش: لەسەر UNO R3 و بۆردە بچووکەکانی تری AVR (نەک Mega)، Servo دەگرێتە دەست، بۆیە تا بەستراون analogWrite() () لەسەر پینەکانی 9 و 10 کار ناکات — تەنانەت ئەگەر هیچ لەسەر ئەو پینانە نەبێت. بۆ یان خێرایی پینی تر هەڵبژێرە.
Continuous-rotation servos: speed instead of angleسێرڤۆی خولانەوەی بەردەوام: خێرایی لە جیاتی گۆشە
A continuous-rotation servo looks like a positional one but spins round and round. The same pulses now set speed and direction: write(90) (about 1.5 ms) stops it; below 90 it turns one way, above 90 the other, faster the further from 90. Many have a small trim screw so that write(90) really stops them. They are sold ready-made (for example the FS90R); you do not have to modify a servo. A positional servo can be modified — the end stop removed and its position sensor fixed in the middle — but the result is the same thing: it no longer knows where it is.
وەک شوێنییەکە دەردەکەوێت بەڵام بە بەردەوامی دەخولێتەوە. هەمان ئێستا خێرایی و ئاراستە دیاری دەکەن: write(90) (نزیکەی 1.5 ms) ڕایدەگرێت؛ لە خوار 90 بە ئاراستەیەک دەسووڕێت، لە سەرووی 90 بە ئەوی تر، تا دوورتر لە 90 خێراتر. زۆربەیان بورغییەکی بچووکی ڕێکخستنیان هەیە تا بە write(90) ڕێک بوەستن. بە ئامادەیی دەفرۆشرێن (بۆ نموونە FS90R)؛ پێویست ناکات دەستکاری بکەیت. شوێنی دەتوانرێت دەستکاری بکرێت — ڕێگرەکەی کۆتایی لادەبرێت و شوێنەکەی لە ناوەڕاستدا جێگیر دەکرێت — بەڵام ئەنجامەکە هەمان شتە: ئیتر نازانێت لە کوێیە.
// A continuous-rotation servo (for example an FS90R) on pin 9: speed, not angle
#include <Servo.h>
Servo wheel;
void setup() {
Serial.begin(9600);
wheel.attach(9);
}
void loop() {
Serial.println("write(90): stop");
wheel.write(90);
delay(1000);
Serial.println("write(70): slow, one way");
wheel.write(70);
delay(2000);
Serial.println("write(0): full speed, the same way");
wheel.write(0);
delay(2000);
Serial.println("write(180): full speed, the other way");
wheel.write(180);
delay(2000);
}write(90): stop write(70): slow, one way write(0): full speed, the same way write(180): full speed, the other way
CêIT Arduino checker: write(70) at 1 s, write(0) at 3 s, write(180) at 5 s. Which way counts as "one way" depends on the servo model.
پشکنەری Arduinoـی CêIT: write(70) لە چرکەی 1، write(0) لە چرکەی 3، write(180) لە چرکەی 5. کام ئاراستە «یەکەم ئاراستە»یە بە جۆری دەبەستێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 13
90: stop (if the trim is set right).
90: وەستان (ئەگەر ڕێکخستنەکە دروست بێت).
- Lineدێڕی 16
70: slowly one way.
70: بە هێواشی بە ئاراستەیەک.
- Lineدێڕی 19-22
0 and 180: full speed in opposite directions.
0 و 180: تەواوی خێرایی بە دوو ئاراستەی پێچەوانە.
Continuous-rotation servos: speed instead of angleسێرڤۆی خولانەوەی بەردەوام: خێرایی لە جیاتی گۆشە
Stopping in the right place: a limit switchوەستان لە شوێنی دروست: سویچی سنوور
A continuous servo cannot say where the cover is, so a limit switch at the end of its travel does: the moving cover presses it, the pin reads LOW (with INPUT_PULLUP), and the sketch stops the servo at once. The cage's cover needs two, one at the open end and one at the closed end.
ناتوانێت بڵێت بەرگەکە لە کوێیە، بۆیە لە کۆتایی ڕێگاکەیدا دەیڵێت: بەرگە جووڵاوەکە دەیپەستێنێت، پینەکە LOW دەخوێنێتەوە (بە INPUT_PULLUP) و یەکسەر ڕادەگرێت. بەرگی قەفەزەکە دووانی دەوێت، یەکێک لە کۆتایی کراوە و یەکێک لە کۆتایی داخراو.
// Close a cover with a continuous servo; a limit switch says where to stop
#include <Servo.h>
Servo cover;
const int closedSwitch = 3; // limit switch to GND: pressed = LOW
bool closing = true;
void setup() {
Serial.begin(9600);
pinMode(closedSwitch, INPUT_PULLUP);
cover.attach(9);
cover.write(70); // turn slowly to pull the cover closed
Serial.println("closing the cover");
}
void loop() {
if (closing && digitalRead(closedSwitch) == LOW) { // the end is reached
cover.write(90); // stop
closing = false;
Serial.print("switch pressed at ");
Serial.print(millis());
Serial.println(" ms: cover closed, servo stopped");
}
}closing the cover switch pressed at 2300 ms: cover closed, servo stopped
CêIT Arduino checker: the switch was pressed at 2.3 s; the servo got write(90) at that moment.
پشکنەری Arduinoـی CêIT: لە چرکەی 2.3 پەستێنرا؛ لەو ساتەدا write(90)ـی وەرگرت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
The switch joins pin 3 to GND when pressed: INPUT_PULLUP reads LOW.
کە پەستێنرا پینی 3 بە GNDـەوە دەبەستێت: INPUT_PULLUP LOW دەخوێنێتەوە.
- Lineدێڕی 17-18
Pressed while closing: write(90) stops the servo.
لە کاتی داخستندا پەستێنرا: write(90) ڕادەگرێت.
Choosing a motor for the cage coverهەڵبژاردنی ماتۆڕێک بۆ بەرگی قەفەزەکە
The cover hangs from a rail and is pulled by a string around a pulley. Four candidates, compared:
بەرگەکە لە ڕێڕەوێکەوە هەڵواسراوە و بە پەتێک بە دەوری دەکێشرێت. چوار هەڵبژاردە، بەراورد کراون:
| Motor | Goodباش | Watch outئاگادار بە |
|---|---|---|
| positional servo شوێنی | simple; knows its angleسادە؛ گۆشەکەی دەزانێت | only about half a turn: fine for a flap, too short for a long stringتەنیا نزیکەی نیو سووڕانەوە: بۆ دەرچەیەک باشە، بۆ پەتێکی درێژ کورتە |
| continuous servo | cheap, simple, no driver neededهەرزان، سادە، ناوێت | no position: needs two limit switchesبێ شوێن: پێویستی بە دوو هەیە |
| DC gear motor + encoder + driver گێڕدار + + | knows distance and speed; strongدووری و خێرایی دەزانێت؛ بەهێز | most parts and code; still needs a home switchزۆرترین پارچە و کۆد؛ هێشتا سویچێکی سەرەتای دەوێت |
| stepper motor + driver + | exact steps without an encoderهەنگاوی ورد بەبێ | draws current even when still; can skip steps if overloadedتەنانەت لە کاتی وەستانیشدا دەکێشێت؛ ئەگەر بارگرانی لەسەر بێت دەتوانێت هەنگاو بپەڕێنێت |
For a student build, a continuous servo with two limit switches is the simplest reliable choice; a DC gear motor with an encoder gives the most control, as the project report suggests. Either way, wait until it has been dark (or light) for some minutes before moving, so a cloud cannot fool the cover (No flicker at dusk). The whole design comes together in the project, in Closing and opening the cover.
بۆ دروستکردنێکی قوتابی، بە دوو سادەترین هەڵبژاردەی جێی متمانەیە؛ گێڕدار بە زۆرترین کۆنترۆڵ دەدات، وەک ڕاپۆرتی پڕۆژەکە پێشنیاری دەکات. بە هەر شێوەیەک، پێش جووڵاندن چاوەڕێ بکە تا چەند خولەکێک تاریک (یان ڕووناک) بێت، تا هەورێک نەتوانێت بەرگەکە هەڵبخەڵەتێنێت (بێ لەرزین لە ئێوارەدا). هەموو دیزاینەکە لە پڕۆژەکەدا کۆ دەبێتەوە، لە داخستن و کردنەوەی بەرگەکە.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | Betterباشتر |
|---|---|
| A motor straight on a pin. ڕاستەوخۆ لەسەر پینێک. | A motor driver and the motor's own supply. و خودی . |
| The motor supply and the board with separate grounds. و بۆرد بە زەویی جیا. | Join the GNDs.GNDـەکان پێکەوە ببەستە. |
| Expecting the encoder to set the direction.چاوەڕوانیی ئەوە کە ئاراستە دیاری بکات. | The driver sets it; the encoder measures. دیاری دەکات؛ دەپێوێت. |
| Believing a positional servo only goes to 0°, 90° and 180°.باوەڕکردن بەوەی شوێنی تەنیا دەچێتە سەر 0°، 90° و 180°. | Any angle: write(37) works.هەر گۆشەیەک: write(37) کار دەکات. |
| Modifying a servo to make it spin.دەستکاریکردنی بۆ ئەوەی بخولێتەوە. | Buy a continuous-rotation servo. بکڕە. |
| A continuous servo stopped by timing alone. کە تەنیا بە کات ڕادەگیرێت. | Limit switches (or an encoder) at the ends. (یان ) لە کۆتاییەکاندا. |
| analogWrite() on pins 9 or 10 while a servo is attached.analogWrite() لەسەر پینی 9 یان 10 لە کاتێکدا بەستراوە. | Use other PWM pins (UNO: 3, 5, 6, 11).پینی تر بەکار بهێنە (UNO: 3، 5، 6، 11). |
| Several servos on the board's 5V pin.چەند لەسەر پینی 5Vـی بۆرد. | A separate 5 V supply, grounds joined. 5 Vـی جیا، زەوییەکان بەستراو. |
| A mains heater wired to the board.گەرمکەرەوەیەکی کارەبای ماڵ بەستراو بە بۆردەوە. | Never: a certified device; LEDs stand in.هەرگیز: ئامێرێکی بڕوانامەدار؛ جێیان دەگرنەوە. |
Exercisesڕاهێنانەکان
With the Servo library's default limits, how long is the pulse for write(120)?
بە سنوورە بنەڕەتییەکانی Servo، write(120) چەند درێژە؟
Show answerوەڵامەکە پیشان بدە
The cover's string winds on a pulley 3 cm across. The encoder gives 20 pulses per turn of the output shaft. How many pulses move the cover 60 cm?
پەتی بەرگەکە بە دەوری 3 cm پاندا دەپێچرێتەوە. بۆ هەر سووڕانەوەیەکی تەوەرەی دەرچوون 20 دەدات. چەند بەرگەکە 60 cm دەجووڵێنێت؟
Hintئاماژە
One turn of the pulley moves the string by its circumference, π × diameter.
یەک سووڕانەوەی پەتەکە بەقەدەر چێوەکەی دەجووڵێنێت، π × تیرە.
Show answerوەڵامەکە پیشان بدە
With a TB6612FNG, what is the difference between IN1 = IN2 = HIGH and IN1 = IN2 = LOW? Which would you use to stop the cover exactly at a limit switch?
لەگەڵ TB6612FNG، جیاوازیی IN1 = IN2 = HIGH و IN1 = IN2 = LOW چییە؟ کامیان بەکار دەهێنیت بۆ ڕاگرتنی بەرگەکە ڕێک لە ؟
Show answerوەڵامەکە پیشان بدە
Both HIGH is a short brake: the motor's ends are joined and it stops fast. Both LOW lets the motor go, and it coasts on. At a limit switch, brake.
هەردووکیان HIGH ڕاگرتنی خێرایە: دوو سەری پێکەوە دەبەسترێن و بە خێرایی دەوەستێت. هەردووکیان LOW بەرەڵا دەکات و بە خۆی بەردەوام دەبێت. لە ، ڕایبگرە.
Extend the limit-switch sketch: close the cover until the switch on pin 3, wait 2 s, then open it until a second switch on pin 4.
درێژ بکەرەوە: بەرگەکە دابخە تا سویچی پینی 3، 2 چرکە چاوەڕێ بکە، پاشان بیکەرەوە تا سویچێکی دووەم لەسەر پینی 4.
Hintئاماژە
Use a variable for the step and millis() for the wait, so loop() keeps checking the switches.
بۆ هەنگاوەکە و millis() بۆ چاوەڕوانییەکە بەکار بهێنە، تا loop() بەردەوام سویچەکان بپشکنێت.
Show answerوەڵامەکە پیشان بدە
// Close the cover, wait, then open it again: one limit switch at each end
#include <Servo.h>
Servo cover; // a continuous-rotation servo
const int closedSwitch = 3; // pressed = LOW (INPUT_PULLUP)
const int openSwitch = 4; // pressed = LOW (INPUT_PULLUP)
int state = 0; // 0 closing, 1 waiting, 2 opening, 3 done
unsigned long waitStart = 0;
void setup() {
Serial.begin(9600);
pinMode(closedSwitch, INPUT_PULLUP);
pinMode(openSwitch, INPUT_PULLUP);
cover.attach(9);
cover.write(70); // one way: closing
Serial.println("closing");
}
void loop() {
if (state == 0 && digitalRead(closedSwitch) == LOW) {
cover.write(90); // stop
state = 1;
waitStart = millis();
Serial.println("closed: waiting 2 s");
} else if (state == 1 && millis() - waitStart >= 2000) {
cover.write(110); // the other way: opening
state = 2;
Serial.println("opening");
} else if (state == 2 && digitalRead(openSwitch) == LOW) {
cover.write(90); // stop
state = 3;
Serial.println("open: stopped");
}
}closing closed: waiting 2 s opening open: stopped
CêIT Arduino checker: the closed-end switch at 1.8 s, the open-end switch at 6.1 s. The servo got write(90) at 1.8 s, write(110) at 3.8 s and write(90) at 6.1 s.
پشکنەری Arduinoـی CêIT: سویچی کۆتایی داخراو لە چرکەی 1.8، سویچی کۆتایی کراوە لە چرکەی 6.1. لە چرکەی 1.8 write(90)، لە 3.8 write(110) و لە 6.1 write(90)ـی وەرگرت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7
0 closing, 1 waiting, 2 opening, 3 done: one variable remembers the step.
0 داخستن، 1 چاوەڕوانی، 2 کردنەوە، 3 تەواو: یەک هەنگاوەکە لەبیر دەکات.
Summaryپوختە
- Actuators turn commands into action: motors, pumps, relays, buzzers, LEDs. Mains loads never connect to a board.
- A DC motor turns while current flows, reverses when the current reverses, and needs a motor driver (an H-bridge) and its own supply — never a pin.
- An incremental encoder measures how far, how fast and which way; the driver sets direction and speed. Together they close the loop.
- A positional servo goes to any angle (about 0–180°), set by a pulse every 20 ms: Servo library 544–2400 µs, SG90 about 0.5–2.4 ms.
- A continuous-rotation servo (sold ready-made) turns speed and direction from the same pulses, write(90) ≈ stop, but it needs limit switches to know where it is.
- فەرمان دەکەن بە کار: ، پەمپ، ، ، . بارەکانی کارەبای ماڵ هەرگیز بە بۆردەوە نابەسترێن.
- تا هەبێت دەسووڕێت، کە پێچەوانە بوو پێچەوانە دەبێتەوە و پێویستی بە () و خۆی هەیە — هەرگیز پین نا.
- دەپێوێت چەند، چەند خێرا و بە کام ئاراستەدا؛ ئاراستە و خێرایی دیاری دەکات. پێکەوە خولەکە دادەخەن.
- شوێنی دەچێتە سەر هەر گۆشەیەک (نزیکەی 0–180°)، کە هەموو 20 ms دیاری دەکات: Servo 544–2400 µs، SG90 نزیکەی 0.5–2.4 ms.
- (بە ئامادەیی دەفرۆشرێت) لە هەمان خێرایی و ئاراستە وەردەگرێت، write(90) ≈ وەستان، بەڵام بۆ زانینی شوێنی خۆی پێویستی بە هەیە.
Why can a board not run a motor from a pin?بۆچی بۆرد ناتوانێت ماتۆڕێک لە پینێکەوە بجووڵێنێت؟
A pin gives 7–20 mA; even a small motor wants about 150 mA running and over 1 A when blocked, and its coil kicks back voltage spikes.
پینێک 7–20 mA دەدات؛ تەنانەت بچووک لە کاردا نزیکەی 150 mA و کاتێک دەگیرێت زیاتر لە 1 Aـی دەوێت و پێچەکەی بەرز دەداتەوە.
What is the difference between a positional and a continuous-rotation servo?جیاوازیی نێوان ماتۆڕی سێرڤۆی شوێنی و سێرڤۆی خولانەوەی بەردەوام چییە؟
The same pulse means an angle to the first and a speed and direction to the second; only the positional servo knows where it is.
هەمان بۆ یەکەمیان واتای گۆشەیەک دەدات و بۆ دووەمیان خێرایی و ئاراستە؛ تەنیا شوێنی دەزانێت لە کوێیە.
Which motor would you choose for the cage cover?کام ماتۆڕ بۆ بەرگی قەفەزەکە هەڵدەبژێریت؟
A continuous servo with two limit switches for a simple build, or a DC gear motor with an encoder, a driver and a home switch for the most control.
بە دوو بۆ دروستکردنێکی سادە، یان گێڕدار بە ، و سویچێکی سەرەتاوە بۆ زۆرترین کۆنترۆڵ.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
From Smoke Signals to Device-to-Cloudلە نیشانەی دووکەڵەوە تا پەیوەندیی ئامێر بۆ هەور
Related lecturesوانە پەیوەندیدارەکان
- Inside an IoT System: Building Blocks and the Data Pathناو سیستەمێکی IoT: پێکهاتە سەرەکییەکان و ڕێڕەوی دراوە
- Embedded Systems at Work: a Bike Computer and a Car Engineسیستەمە چەسپێنراوەکان لە کاردا: کۆمپیوتەری پاسکیل و بزوێنەری ئۆتۆمبێل
- Sensing Light with an LDRهەستکردن بە ڕووناکی بە LDR
- Temperature and Humidity: the DHT11 and a 16x2 LCDپلەی گەرمی و شێ: DHT11 و LCDـێکی 16x2
- Project: a Smart Bird-Cage Coverپرۆژە: بەرگی هۆشمەندی قەفەزی باڵندە
- Arduino Boards and the Arduino IDEبۆردەکانی Arduino و Arduino IDE
- Agents, Sensors and Actuators; Rational Agentsئەیجێنتەکان، هەستەوەرەکان و جووڵێنەرەکان؛ ئەیجێنتە عەقڵانییەکان
- Inside a Drone: Frame, Motors, ESCs and Propellersناو درۆن: چوارچێوەی درۆن، ماتۆڕەکان، کۆنترۆڵکەرەکانی خێرایی و پەروانەکان
- Autonomous Drones, Vehicles and Robotsدرۆن، ئۆتۆمبێل و ڕۆبۆتی سەربەخۆ
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Arduino Servo library: API (attach min/max 544/2400 µs, write for standard and continuous servos, writeMicroseconds, detach)
- Arduino Servo library: overview (up to 12 servos, PWM on pins 9 and 10 disabled, power and grounds)
- TowerPro SG90 9 g micro servo data sheet (20 ms period, 0.5–2.4 ms, 4.8 V, 1.8 kgf·cm)
- SparkFun: TB6612FNG Hookup Guide (truth table, 1.2 A / 3.2 A, supplies)
- STMicroelectronics L298 dual full-bridge driver data sheet (46 V, total drop 1.8–3.2 V at 1 A)
- Adafruit: DC Gearbox (TT) Motor, 200 rpm, 3–6 V (no-load and stall current)
- Pololu: FeeTech FS90R continuous rotation micro servo (1.5 ms rest point, trim)
- Wikipedia: Incremental encoder (quadrature outputs A and B, homing)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.