Driving a servo and a one-way DC motor with a MOSFET
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Two motors, two kinds of pulse. A hobby servo reads the width of a pulse sent every 20 ms as an angle and holds it with its own feedback; the Arduino Servo library makes those pulses with a hardware timer. A plain DC motor reads nothing: an IRLZ44N MOSFET switches its 6 V supply with PWM, the motor spins at the average, and a flyback diode protects the MOSFET at every off-edge. Target board: Arduino Uno R3. “PWM: dimming an LED” explains duty cycle and analogWrite, and “Pin current limits and driving loads with a transistor” explains why a pin needs a MOSFET for big loads; here we use both.
Inside a hobby servo
A hobby servo hides a DC motor, gears, a position sensor and a small control circuit in one box. It has three wires: power, ground and signal. You never drive its motor yourself: you send an angle, and it turns there and holds it.
hobby servo motor + gears + position sensor + control circuit
wires power (red), ground (black or brown),
signal (yellow, orange or white)
feedback it turns until the sensor reads the angle you asked forاقرأ الدرس خطوة بخطوة
Inside a hobby servo
A hobby servo hides a DC motor, gears, a position sensor and a small control circuit in one box. It has three wires: power, ground and signal. You never drive its motor yourself: you send an angle, and it turns there and holds it.
hobby servo motor + gears + position sensor + control circuit wires power (red), ground (black or brown), signal (yellow, orange or white) feedback it turns until the sensor reads the angle you asked forThe angle is a pulse width
The signal wire carries a short pulse about every 20 ms. Its width is the message. Standard servos read 1000 µs as one end, 1500 µs as the middle and 2000 µs as the other end. The servo turns until its sensor agrees.
frame one pulse about every 20 ms (50 per second) width the message: 1000 µs one end 1500 µs the middle 2000 µs the other end the duty cycle is not the message; the width isservo.attach(9) starts the pulses
servo.attach(9) hands pin 9 to the Servo library. On the Uno it takes over Timer1, a hardware timer, which makes the pulses in the background while loop() runs. It starts at 1500 µs, the middle. Side effect: analogWrite stops working on pins 9 and 10.
line 1 servo.attach(9); library Timer1 counts and interrupts: no delay(), no busy loop start 1500 µs (DEFAULT_PULSE_WIDTH) cost analogWrite() no longer works on pins 9 and 10servo.write(90): 1472 µs
servo.write(90) maps 0 to 180 degrees onto the library's 544 to 2400 µs: 90 becomes 1472 µs. Every 20 ms pin 9 now goes high for 1472 µs. The servo sees a target near the middle and holds it there.
line 2 servo.write(90); map 90 * (2400 - 544) / 180 + 544 = 1472 µs pin 9 high 1472 µs, then low, every 20 ms servo target: the middleservo.write(180): 2400 µs
servo.write(180) widens the pulse to 2400 µs, so the horn swings to the other end. The pin only sends the message. The servo's motor takes its current from a separate 5 V supply that shares ground with the board.
line 3 servo.write(180); map 180 -> 2400 µs (MAX_PULSE_WIDTH) servo new target: the far end, the horn swings power from its own 5 V supply, grounds joined pin 9 only the signal, almost no currentA motor needs a switch
Now a plain DC motor on its own 6 V supply. A pin gives at most 40 mA: not enough for most motors. So an IRLZ44N MOSFET switches the motor on the low side, as in “Pin current limits and driving loads with a transistor”.
pin limit 40 mA, not enough for most motors motor on its own 6 V supply MOSFET IRLZ44N, logic-level: full on at 5 V on the gate 0.025 Ω on, rated 55 V low side supply -> motor -> MOSFET -> groundanalogWrite(3, 191)
analogWrite(3, 191) makes pin 3 a PWM signal: high for 191 of every 255 parts of each cycle, about 490 cycles a second. Through its 220 Ω resistor the pin drives the MOSFET's gate, so the MOSFET switches the motor's 6 V just as fast.
line 4 analogWrite(3, 191); PWM 191 / 255 = 75 % duty, about 490 Hz cycle 2.04 ms: high 1.53 ms, low 0.51 ms gate 5 V / 0 V through 220 Ω -> MOSFET on / offThe motor feels the average
The motor's spinning mass cannot follow 490 pulses a second, so it responds to the average: 191 ÷ 255 × 6 V ≈ 4.5 V. A bigger number means a faster motor. The current loops supply, motor, MOSFET, ground, never through the pin.
average 191 / 255 × 6 V = 4.49 V ≈ 4.5 V speed rises with the duty cycle loop + supply -> motor -> drain -> source -> ground pin only charges the gateSwitching off a coil
A motor is a coil, which resists sudden changes in its current. At each off-edge of the PWM the MOSFET cuts that current. With nowhere to go, the drain voltage can jump far above 6 V and destroy the MOSFET, rated 55 V.
coil v = L · di/dt: a fast cut makes a big voltage when every off-edge, about 490 times a second drain can leap far above 6 V MOSFET rated 55 V: a big enough spike destroys itA flyback diode
A flyback diode across the motor gives that current a way round: out of the motor, through the diode and back in, fading as heat. While the MOSFET is on, the diode points the wrong way and carries nothing. Its stripe faces +6 V.
diode 1N4001 across the motor, stripe to +6 V MOSFET on diode reverse-biased: no current MOSFET off coil current: motor -> diode -> motor drain held about one diode drop above the supplySpeed, but one way only
analogWrite(3, 0) holds pin 3 LOW and the motor coasts to a stop. This circuit sets speed, never direction: current only ever flows one way through the motor. Reversing it needs an H-bridge, in “H-bridges, motor direction and stepper motors”.
line 5 analogWrite(3, 0); pin 3 0 -> held LOW, MOSFET off, motor coasts one way current always + -> motor -> MOSFET reverse needs an H-bridge (four switches) servo still holds 2400 µs: Timer1 keeps pulsing
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What tells a hobby servo which angle to turn to?
The servo reads the high time of each pulse, sent about every 20 ms: on standard servos 1000 µs is one end, 1500 µs the middle and 2000 µs the other end. Its own control circuit then turns the motor until its position sensor agrees.
02With the Servo library's default range (544 to 2400 µs), what pulse width does servo.write(90) send?
write() maps 0 to 180 degrees onto 544 to 2400 µs: 90 × (2400 − 544) ÷ 180 + 544 = 1472 µs.
03On an Uno, after servo.attach(9), why put the motor's PWM on pin 3 rather than pin 10?
On the Uno the Servo library uses Timer1, and its docs say this disables analogWrite() on pins 9 and 10. Pin 3's PWM comes from Timer2, about 490 Hz.
04analogWrite(3, 191) switches a 6 V motor through a MOSFET. About what average voltage does the motor see?
The duty cycle is 191 ÷ 255 ≈ 75 %, and the motor responds to the average: 75 % of 6 V ≈ 4.5 V. Its spinning mass is too slow to follow each 490 Hz pulse.
05What does the flyback diode across the motor do?
A coil resists sudden changes in current. At each off-edge the current circles motor → diode → motor and fades, instead of driving the drain far above 6 V. While the MOSFET is on, the diode is reverse-biased and carries nothing. It cannot reverse the motor: that needs an H-bridge.
المسرد
- Hobby servo
- A motor, gearbox, position sensor and control circuit in one box. It turns to the angle named by its input pulse and holds it.
- Pulse width
- How long a pulse stays high, in microseconds (µs). For a servo, the width is the message: about 1000 to 2000 µs from one end to the other.
- Servo library
- The Arduino library that makes servo pulses with a hardware timer. attach(pin) starts the pulses; write(angle) sets their width.
- Duty cycle
- The share of each PWM cycle that the pin is high. analogWrite(pin, 191) is 191 ÷ 255, about 75 %.
- Low-side switch
- A transistor between the load and ground. It turns the load on and off but cannot reverse the current through it.
- Logic-level MOSFET
- A MOSFET that turns fully on with only 5 V (or less) on its gate, so a microcontroller pin can drive it directly.
- Flyback diode
- A diode across a coil (a motor, relay or solenoid). It carries the coil's current when the switch opens, so no voltage spike damages the switch.
- H-bridge
- Four switches around a motor that can drive current through it either way, so the motor can run forwards and backwards.
الملاحظات والمصادر
- Servos differ. The Arduino library maps 0 to 180 degrees onto 544 to 2400 µs by default, while many servos reach their ends nearer 1000 and 2000 µs; servo.attach(pin, min, max) sets your servo's own range. The figure turns the horn on the library's scale.
- The Servo library uses Timer1 on the Uno, so the motor is on pin 3, whose PWM comes from Timer2 at about 490 Hz. Pins 5 and 6 run at about 980 Hz but give higher-than-expected duty cycles.
- 191 ÷ 255 × 6 V is the ideal average. The motor's real speed also depends on its load and on small drops in the MOSFET and the wiring, so this circuit sets speed only roughly: closing the loop is the Control lesson on DC motor speed.
- The figure slows one off-moment right down. In the real circuit it happens at every off-edge, about 490 times a second, and the coil current keeps the motor turning through each off part of the cycle.
- Arduino Servo library: Servo.h (pulse widths and refresh interval)
- Arduino Servo library: avr/Servo.cpp (write() and map())
- Arduino Servo library: avr/ServoTimers.h (Timer1 on the Uno)
- Arduino Servo library documentation: overview (wires, pins 9 and 10, power)
- Arduino Servo library documentation: API (attach, write, writeMicroseconds)
- SparkFun: Hobby Servo Tutorial
- Arduino language reference: analogWrite() (source)
- Arduino AVR core: wiring_analog.c (analogWrite 0 and 255)
- Arduino AVR core: variants/standard/pins_arduino.h (pin to timer map)
- Arduino docs: Digital Pins (source)
- Arduino docs: Transistor Motor Control (source)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 12.6 DC Motor Interface with PWM (UT Austin)
- Infineon IRLZ44NPbF datasheet
- Flyback diode (Wikipedia)
اختبار يتاح بعد الخطوة الأخيرة.