पाठ 13 / 25
Driving Motors and Relays Safely
Transistors, MOSFETs, flyback diodes and motor drivers.
GPIO pins signal, they do not power
A GPIO pin can supply only a few tens of milliamps (on the ATmega328P, 40 mA absolute maximum per pin, less in practice). A small DC motor can draw hundreds of milliamps and far more when starting or stalled, and a relay coil often needs more than a pin can safely give. Never power a motor, relay coil or solenoid directly from a GPIO pin. Use the pin to switch a transistor: an NPN BJT with a base resistor, or better a logic-level N-channel MOSFET whose gate fully turns on at your logic voltage (check the datasheet; many MOSFETs need 10 V on the gate). Inductive loads (motors, relays, solenoids) produce a voltage spike when switched off, so put a flyback diode across the load, reverse-biased. For direction and speed control, use an H-bridge motor driver module rated for your motor's voltage and current.
Moving things without frying the board
GPIO pins signal; transistors, drivers and a proper supply do the heavy lifting.
Low-side MOSFET switch for a DC motor
Wiring description; component choices are examples, verify ratings in each datasheet.
Motor supply + (e.g. 6-12 V, separate from the board) -> motor terminal A
Motor terminal B -> MOSFET drain
MOSFET source -> GND (shared with Arduino GND)
Arduino PWM pin -> ~100-220 Ohm resistor -> MOSFET gate
MOSFET gate -> ~10 kOhm resistor -> GND (keeps motor off during reset)
Flyback diode across the motor: cathode to supply +, anode to drain
Decoupling: capacitor near the motor supply input
Never: motor between a GPIO pin and GNDPWM speed control through the MOSFET
Same code style as LED dimming; the MOSFET carries the current. Uno R3 pin 9.
const uint8_t MOTOR_PIN = 9; // PWM pin driving the MOSFET gate
void setup() {
pinMode(MOTOR_PIN, OUTPUT);
analogWrite(MOTOR_PIN, 0); // start stopped
}
void loop() {
int pot = analogRead(A0); // 0..1023
analogWrite(MOTOR_PIN, pot / 4); // map to 0..255 duty
}Relay modules and mains
Prefer ready-made relay modules with a driver transistor and diode. Do not work on mains voltage unless you are qualified; use low-voltage loads for learning.
त्वरित जाँच: Why is a flyback diode placed across a DC motor or relay coil?
- To make the motor spin faster
- To safely absorb the voltage spike produced when the inductive load is switched off
- To convert PWM into true analog voltage
- To raise the logic level from 3.3 V to 5 V
Answer
To safely absorb the voltage spike produced when the inductive load is switched off — Inductors resist sudden current changes and generate a reverse spike that can destroy the transistor.