We use a transistor to control a DC motor because the motor needs more power than the Arduino can provide directly. The transistor acts like a switch that turns the motor on and off safely.
DC motor with transistor driver in Arduino
int motorPin = 9; // Arduino pin connected to transistor base void setup() { pinMode(motorPin, OUTPUT); // Set pin as output } void loop() { digitalWrite(motorPin, HIGH); // Turn motor ON delay(1000); // Wait 1 second digitalWrite(motorPin, LOW); // Turn motor OFF delay(1000); // Wait 1 second }
The transistor base is connected to an Arduino digital pin through a resistor.
Use a diode across the motor terminals to protect from voltage spikes.
digitalWrite(motorPin, HIGH); // Motor ON digitalWrite(motorPin, LOW); // Motor OFF
analogWrite(motorPin, 128); // Motor runs at half speed (PWM)pinMode(motorPin, OUTPUT); // Set pin as output before controlling motorThis program turns the motor on for 2 seconds, then off for 2 seconds, repeatedly. The transistor acts as a switch controlled by the Arduino pin.
int motorPin = 9; // Pin connected to transistor base void setup() { pinMode(motorPin, OUTPUT); // Set motorPin as output } void loop() { digitalWrite(motorPin, HIGH); // Turn motor ON delay(2000); // Run motor for 2 seconds digitalWrite(motorPin, LOW); // Turn motor OFF delay(2000); // Wait for 2 seconds }
Always connect a diode (called a flyback diode) across the motor terminals to protect the transistor from voltage spikes.
Use a resistor (around 1k ohm) between Arduino pin and transistor base to limit current.
Make sure your transistor can handle the motor's current.
A transistor lets Arduino safely control a DC motor by acting as a switch.
Use digitalWrite to turn the motor on or off through the transistor.
Protect your circuit with a diode and resistor for safe operation.
